committed by
GitHub
2 changed files with 38 additions and 1 deletions
@ -0,0 +1,31 @@ |
|||||
|
interface PluralForms { |
||||
|
one: string; |
||||
|
other: string; |
||||
|
[key: string]: string; |
||||
|
} |
||||
|
|
||||
|
interface FormatOptions { |
||||
|
locale?: string; |
||||
|
pluralRules?: Intl.PluralRulesOptions; |
||||
|
numberFormat?: Intl.NumberFormatOptions; |
||||
|
} |
||||
|
|
||||
|
export function formatQuantity( |
||||
|
value: number, |
||||
|
forms: PluralForms, |
||||
|
options: FormatOptions = {}, |
||||
|
) { |
||||
|
const { |
||||
|
locale = "en-US", |
||||
|
pluralRules: pluralOptions = { type: "cardinal" }, |
||||
|
numberFormat: numberOptions = {}, |
||||
|
} = options; |
||||
|
|
||||
|
const pluralRules = new Intl.PluralRules(locale, pluralOptions); |
||||
|
const numberFormat = new Intl.NumberFormat(locale, numberOptions); |
||||
|
|
||||
|
const pluralCategory = pluralRules.select(value); |
||||
|
const word = forms[pluralCategory]; |
||||
|
|
||||
|
return `${numberFormat.format(value)} ${word}`; |
||||
|
} |
||||
Loading…
Reference in new issue