fix: pluralize type helper to account for uncountable nouns and special rules (#10011)
This commit is contained in:
1 parent
1cab15b209
commit
e228088f8d
2 files changed
+188
-30
No files matched your search
@@ -0,0 +1,44 @@
|
|||||||
|
import { expectTypeOf } from "expect-type"
|
||||||
|
import { Pluralize } from "../common"
|
||||||
|
|
||||||
|
describe("Pluralize", () => {
|
||||||
|
test("pluralize uncountable nouns", () => {
|
||||||
|
expectTypeOf<Pluralize<"media">>().toEqualTypeOf<"media">()
|
||||||
|
expectTypeOf<Pluralize<"Media">>().toEqualTypeOf<"Media">()
|
||||||
|
expectTypeOf<Pluralize<"you">>().toEqualTypeOf<"you">()
|
||||||
|
expectTypeOf<Pluralize<"sheep">>().toEqualTypeOf<"sheep">()
|
||||||
|
expectTypeOf<Pluralize<"series">>().toEqualTypeOf<"series">()
|
||||||
|
expectTypeOf<Pluralize<"species">>().toEqualTypeOf<"species">()
|
||||||
|
expectTypeOf<Pluralize<"deer">>().toEqualTypeOf<"deer">()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("pluralize words ending with fe", () => {
|
||||||
|
expectTypeOf<Pluralize<"wife">>().toEqualTypeOf<"wives">()
|
||||||
|
expectTypeOf<Pluralize<"knife">>().toEqualTypeOf<"knives">()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("pluralize words ending with o", () => {
|
||||||
|
expectTypeOf<Pluralize<"hero">>().toEqualTypeOf<"heroes">()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("pluralize words ending with ch", () => {
|
||||||
|
expectTypeOf<Pluralize<"watch">>().toEqualTypeOf<"watches">()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("pluralize words ending with z", () => {
|
||||||
|
expectTypeOf<Pluralize<"fiz">>().toEqualTypeOf<"fizes">()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("pluralize words ending with y", () => {
|
||||||
|
expectTypeOf<Pluralize<"puppy">>().toEqualTypeOf<"puppies">()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("pluralize words with special rules", () => {
|
||||||
|
expectTypeOf<Pluralize<"person">>().toEqualTypeOf<"people">()
|
||||||
|
expectTypeOf<Pluralize<"child">>().toEqualTypeOf<"children">()
|
||||||
|
expectTypeOf<Pluralize<"man">>().toEqualTypeOf<"men">()
|
||||||
|
expectTypeOf<Pluralize<"criterion">>().toEqualTypeOf<"criteria">()
|
||||||
|
expectTypeOf<Pluralize<"tooth">>().toEqualTypeOf<"teeth">()
|
||||||
|
expectTypeOf<Pluralize<"foot">>().toEqualTypeOf<"feet">()
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -240,39 +240,153 @@ export interface NumericalComparisonOperator {
|
|||||||
lte?: number
|
lte?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The keywords that does not have a plural form
|
||||||
|
*/
|
||||||
|
type UncountableRules =
|
||||||
|
| "adulthood"
|
||||||
|
| "advice"
|
||||||
|
| "agenda"
|
||||||
|
| "aid"
|
||||||
|
| "aircraft"
|
||||||
|
| "alcohol"
|
||||||
|
| "ammo"
|
||||||
|
| "analytics"
|
||||||
|
| "anime"
|
||||||
|
| "athletics"
|
||||||
|
| "audio"
|
||||||
|
| "bison"
|
||||||
|
| "blood"
|
||||||
|
| "bream"
|
||||||
|
| "buffalo"
|
||||||
|
| "butter"
|
||||||
|
| "carp"
|
||||||
|
| "cash"
|
||||||
|
| "chassis"
|
||||||
|
| "chess"
|
||||||
|
| "clothing"
|
||||||
|
| "cod"
|
||||||
|
| "commerce"
|
||||||
|
| "cooperation"
|
||||||
|
| "corps"
|
||||||
|
| "debris"
|
||||||
|
| "diabetes"
|
||||||
|
| "digestion"
|
||||||
|
| "elk"
|
||||||
|
| "energy"
|
||||||
|
| "equipment"
|
||||||
|
| "excretion"
|
||||||
|
| "expertise"
|
||||||
|
| "firmware"
|
||||||
|
| "flounder"
|
||||||
|
| "fun"
|
||||||
|
| "gallows"
|
||||||
|
| "garbage"
|
||||||
|
| "graffiti"
|
||||||
|
| "hardware"
|
||||||
|
| "headquarters"
|
||||||
|
| "health"
|
||||||
|
| "herpes"
|
||||||
|
| "highjinks"
|
||||||
|
| "homework"
|
||||||
|
| "housework"
|
||||||
|
| "information"
|
||||||
|
| "jeans"
|
||||||
|
| "justice"
|
||||||
|
| "kudos"
|
||||||
|
| "labour"
|
||||||
|
| "literature"
|
||||||
|
| "machinery"
|
||||||
|
| "mackerel"
|
||||||
|
| "mail"
|
||||||
|
| "media"
|
||||||
|
| "mews"
|
||||||
|
| "moose"
|
||||||
|
| "music"
|
||||||
|
| "mud"
|
||||||
|
| "manga"
|
||||||
|
| "news"
|
||||||
|
| "only"
|
||||||
|
| "personnel"
|
||||||
|
| "pike"
|
||||||
|
| "plankton"
|
||||||
|
| "pliers"
|
||||||
|
| "police"
|
||||||
|
| "pollution"
|
||||||
|
| "premises"
|
||||||
|
| "rain"
|
||||||
|
| "research"
|
||||||
|
| "rice"
|
||||||
|
| "salmon"
|
||||||
|
| "scissors"
|
||||||
|
| "series"
|
||||||
|
| "sewage"
|
||||||
|
| "shambles"
|
||||||
|
| "shrimp"
|
||||||
|
| "software"
|
||||||
|
| "staff"
|
||||||
|
| "swine"
|
||||||
|
| "tennis"
|
||||||
|
| "traffic"
|
||||||
|
| "transportation"
|
||||||
|
| "trout"
|
||||||
|
| "tuna"
|
||||||
|
| "wealth"
|
||||||
|
| "welfare"
|
||||||
|
| "whiting"
|
||||||
|
| "wildebeest"
|
||||||
|
| "wildlife"
|
||||||
|
| "you"
|
||||||
|
| "deer"
|
||||||
|
| "sheep"
|
||||||
|
|
||||||
|
type PluralizationSpecialRules = {
|
||||||
|
person: "people"
|
||||||
|
child: "children"
|
||||||
|
man: "men"
|
||||||
|
criterion: "criteria"
|
||||||
|
tooth: "teeth"
|
||||||
|
foot: "feet"
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ignore
|
* @ignore
|
||||||
*/
|
*/
|
||||||
export type Pluralize<Singular extends string> = Singular extends `${infer R}ss`
|
export type Pluralize<Singular extends string> =
|
||||||
? `${Singular}es`
|
Lowercase<Singular> extends keyof PluralizationSpecialRules
|
||||||
: Singular extends `${infer R}sis`
|
? PluralizationSpecialRules[Lowercase<Singular>]
|
||||||
? `${R}ses`
|
: Lowercase<Singular> extends UncountableRules
|
||||||
: Singular extends `${infer R}is`
|
? Singular
|
||||||
? `${R}ises`
|
: Singular extends `${infer R}ss`
|
||||||
: Singular extends `${infer R}s`
|
? `${Singular}es`
|
||||||
? `${Singular}`
|
: Singular extends `${infer R}sis`
|
||||||
: Singular extends `${infer R}ey`
|
? `${R}ses`
|
||||||
? `${R}eys`
|
: Singular extends `${infer R}is`
|
||||||
: Singular extends `${infer R}y`
|
? `${R}ises`
|
||||||
? `${R}ies`
|
: Singular extends `${infer R}s`
|
||||||
: Singular extends `${infer R}es`
|
? `${Singular}`
|
||||||
? `${Singular}`
|
: Singular extends `${infer R}ey`
|
||||||
: Singular extends
|
? `${R}eys`
|
||||||
| `${infer R}sh`
|
: Singular extends `${infer R}y`
|
||||||
| `${infer R}ch`
|
? `${R}ies`
|
||||||
| `${infer R}x`
|
: Singular extends `${infer R}es`
|
||||||
| `${infer R}z`
|
? `${Singular}`
|
||||||
| `${infer R}o`
|
: Singular extends
|
||||||
? `${Singular}es`
|
| `${infer R}sh`
|
||||||
: Singular extends `${infer R}fe`
|
| `${infer R}ch`
|
||||||
? `${R}ves`
|
| `${infer R}x`
|
||||||
: Singular extends `${infer R}ex` | `${infer R}ix`
|
| `${infer R}z`
|
||||||
? `${R}ices`
|
| `${infer R}o`
|
||||||
: Singular extends `${infer R}eau`
|
? `${Singular}es`
|
||||||
? `${R}eaux`
|
: Singular extends `${infer R}fe`
|
||||||
: Singular extends `${infer R}ieu`
|
? `${R}ves`
|
||||||
? `${R}ieux`
|
: Singular extends `${infer R}ex` | `${infer R}ix`
|
||||||
: `${Singular}s`
|
? `${R}ices`
|
||||||
|
: Singular extends `${infer R}eau`
|
||||||
|
? `${R}eaux`
|
||||||
|
: Singular extends `${infer R}ieu`
|
||||||
|
? `${R}ieux`
|
||||||
|
: `${Singular}s`
|
||||||
|
|
||||||
export type SnakeCase<S extends string> =
|
export type SnakeCase<S extends string> =
|
||||||
S extends `${infer T}${infer U}${infer V}`
|
S extends `${infer T}${infer U}${infer V}`
|
||||||
|
|||||||
Reference in new issue
Block a user