1- const words : Record < string , { plural : string ; variants ?: string [ ] } > = {
1+ const words : Record <
2+ string ,
3+ { plural : string ; pluralWithoutNumber ?: string ; variants ?: string [ ] }
4+ > = {
25 Ei : {
36 plural : "Eier" ,
47 } ,
@@ -12,23 +15,52 @@ const words: Record<string, { plural: string; variants?: string[] }> = {
1215 Schüssel : {
1316 plural : "Schüsseln" ,
1417 } ,
18+ "Weiße Zuckerschrift" : {
19+ plural : "Weiße Zuckerschrift" ,
20+ variants : [ "weißer Zuckerschrift" ] ,
21+ } ,
22+ Eigelb : {
23+ plural : "Eigelb" ,
24+ pluralWithoutNumber : "Eigelbe" ,
25+ } ,
1526} ;
1627
17- export function pluralize ( word : string , count : number | string ) {
28+ function normalize ( word : string ) {
29+ return (
30+ Object . entries ( words ) . find (
31+ ( [ , entry ] ) =>
32+ entry . plural === word ||
33+ entry . variants ?. some (
34+ ( variant ) => variant . toLowerCase ( ) === word . toLowerCase ( )
35+ )
36+ ) ?. [ 0 ] ?? word
37+ ) ;
38+ }
39+
40+ export function pluralize (
41+ word : string ,
42+ count : number | string ,
43+ options : { withoutNumber ?: boolean } = { }
44+ ) {
45+ word = normalize ( word ) ;
1846 if ( typeof count !== "number" ) {
1947 return word ;
2048 }
2149 if ( count === 1 ) {
2250 return word ;
2351 }
24- return getPlural ( word ) ;
52+ return getPlural ( word , { withoutNumber : options . withoutNumber } ) ;
2553}
2654
27- export function getPlural ( word : string ) {
28- return (
29- words [ word ] ?. plural ??
30- Object . values ( words ) . find ( ( entry ) => entry . variants ?. includes ( word ) )
31- ?. plural ??
32- word
33- ) ;
55+ export function getPlural (
56+ word : string ,
57+ options : { withoutNumber ?: boolean } = { }
58+ ) {
59+ const match = words [ normalize ( word ) ] ;
60+ if ( ! match ) {
61+ return word ;
62+ }
63+ return options . withoutNumber
64+ ? match . pluralWithoutNumber ?? match . plural
65+ : match . plural ;
3466}
0 commit comments