@pushwoosh/dumb-components 2.8.2 → 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -72,6 +72,35 @@ describe('filters', () => {
|
|
|
72
72
|
expect(filters.pluralize('not a number', [scalar('a'), scalar('b')], createContext())).toBe('not a number');
|
|
73
73
|
});
|
|
74
74
|
});
|
|
75
|
+
describe('pluralize with CLDR forms', () => {
|
|
76
|
+
const arabic = ['صفر', 'صف', 'صفان', 'صفوف', 'صفًا', 'صف*'].map(scalar);
|
|
77
|
+
test('picks each of the six Arabic forms', () => {
|
|
78
|
+
const ctx = createContext('ar');
|
|
79
|
+
expect(filters.pluralize(0, arabic, ctx)).toBe('صفر');
|
|
80
|
+
expect(filters.pluralize(1, arabic, ctx)).toBe('صف');
|
|
81
|
+
expect(filters.pluralize(2, arabic, ctx)).toBe('صفان');
|
|
82
|
+
expect(filters.pluralize(3, arabic, ctx)).toBe('صفوف');
|
|
83
|
+
expect(filters.pluralize(10, arabic, ctx)).toBe('صفوف');
|
|
84
|
+
expect(filters.pluralize(11, arabic, ctx)).toBe('صفًا');
|
|
85
|
+
expect(filters.pluralize(99, arabic, ctx)).toBe('صفًا');
|
|
86
|
+
expect(filters.pluralize(100, arabic, ctx)).toBe('صف*');
|
|
87
|
+
expect(filters.pluralize(103, arabic, ctx)).toBe('صفوف');
|
|
88
|
+
});
|
|
89
|
+
test('keeps the legacy one/other split when fewer forms are given', () => {
|
|
90
|
+
const ctx = createContext('ar');
|
|
91
|
+
expect(filters.pluralize(1, [scalar('صف'), scalar('صفوف')], ctx)).toBe('صف');
|
|
92
|
+
expect(filters.pluralize(0, [scalar('صف'), scalar('صفوف')], ctx)).toBe('صفوف');
|
|
93
|
+
expect(filters.pluralize(2, [scalar('صف'), scalar('صفوف')], ctx)).toBe('صفوف');
|
|
94
|
+
});
|
|
95
|
+
test('Russian three-form rule matches CLDR one/few/many for integers', () => {
|
|
96
|
+
const ctx = createContext('ru');
|
|
97
|
+
const forms = [scalar('one'), scalar('few'), scalar('many')];
|
|
98
|
+
const rules = new Intl.PluralRules('ru');
|
|
99
|
+
for (let n = 0; n <= 1000; n += 1) {
|
|
100
|
+
expect(filters.pluralize(n, forms, ctx)).toBe(rules.select(n));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
75
104
|
describe('formatNumber', () => {
|
|
76
105
|
test('formats integer with spaces', () => {
|
|
77
106
|
expect(filters.formatNumber('1000000', [], createContext())).toBe('1 000 000');
|
|
@@ -15,6 +15,20 @@ const pluralizers = {
|
|
|
15
15
|
de: universalPluralize,
|
|
16
16
|
es: universalPluralize
|
|
17
17
|
};
|
|
18
|
+
const CLDR_CATEGORY_ORDER = ['zero', 'one', 'two', 'few', 'many', 'other'];
|
|
19
|
+
const cldrPluralize = (language, num, plurals) => {
|
|
20
|
+
let rules;
|
|
21
|
+
try {
|
|
22
|
+
rules = new Intl.PluralRules(language);
|
|
23
|
+
} catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const categories = CLDR_CATEGORY_ORDER.filter(c => rules.resolvedOptions().pluralCategories.includes(c));
|
|
27
|
+
if (categories.length < 2 || plurals.length < categories.length) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return plurals[categories.indexOf(rules.select(num))];
|
|
31
|
+
};
|
|
18
32
|
export const filters = {
|
|
19
33
|
capitalize: value => typeof value === 'string' ? value.charAt(0).toUpperCase() + value.slice(1) : value,
|
|
20
34
|
upperCase: value => typeof value === 'string' ? value.toUpperCase() : value,
|
|
@@ -24,6 +38,10 @@ export const filters = {
|
|
|
24
38
|
return value;
|
|
25
39
|
}
|
|
26
40
|
const variants = args.map(arg => unpackScalarOrValue(arg, context));
|
|
41
|
+
const cldr = cldrPluralize(context.language, value, variants);
|
|
42
|
+
if (cldr !== undefined) {
|
|
43
|
+
return cldr;
|
|
44
|
+
}
|
|
27
45
|
const pluralizer = pluralizers[context.language] || universalPluralize;
|
|
28
46
|
return pluralizer(value, variants);
|
|
29
47
|
},
|