-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ResolvePlural.ts
47 lines (46 loc) · 1.24 KB
/
ResolvePlural.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {
Type,
invariant,
PluralRulesInternal,
LDMLPluralRule,
FormatNumericToString,
} from '@formatjs/ecma402-abstract'
import {GetOperands, OperandsRecord} from './GetOperands'
/**
* http://ecma-international.org/ecma-402/7.0/index.html#sec-resolveplural
* @param pl
* @param n
* @param PluralRuleSelect Has to pass in bc it's implementation-specific
*/
export function ResolvePlural(
pl: Intl.PluralRules,
n: number,
{
getInternalSlots,
PluralRuleSelect,
}: {
getInternalSlots(pl: Intl.PluralRules): PluralRulesInternal
PluralRuleSelect: (
locale: string,
type: 'cardinal' | 'ordinal',
n: number,
operands: OperandsRecord
) => LDMLPluralRule
}
): LDMLPluralRule {
const internalSlots = getInternalSlots(pl)
invariant(Type(internalSlots) === 'Object', 'pl has to be an object')
invariant(
'initializedPluralRules' in internalSlots,
'pluralrules must be initialized'
)
invariant(Type(n) === 'Number', 'n must be a number')
if (!isFinite(n)) {
return 'other'
}
const {locale, type} = internalSlots
const res = FormatNumericToString(internalSlots, n)
const s = res.formattedString
const operands = GetOperands(s)
return PluralRuleSelect(locale, type, n, operands)
}