This repository was archived by the owner on Apr 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathextract-numbers.js
62 lines (52 loc) · 2.11 KB
/
extract-numbers.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
let getParentLocale = require('./locales').getParentLocale;
let hasNumbersFields = require('./locales').hasNumbersFields;
let normalizeLocale = require('./locales').normalizeLocale;
module.exports = function extractNumbersFields(locales) {
let cache = {};
// Loads and caches the numbers fields for a given `locale` because loading
// and transforming the data is expensive.
function getNumbers(locale) {
let numbers = cache[locale];
if (numbers) {
return numbers;
}
if (hasNumbersFields(locale)) {
numbers = cache[locale] = loadNumbers(locale);
return numbers;
}
}
// This will traverse the hierarchy for the
// given `locale` until it finds an ancestor with numbers fields.
function findLocaleWithNumbersFields(locale) {
// The "root" locale is the top level ancestor.
if (locale === 'root') {
return 'root';
}
if (hasNumbersFields(locale)) {
return locale;
}
// When the `locale` doesn't have numbers fields, we need to traverse up
// its hierarchy to find suitable numbers fields data.
return findLocaleWithNumbersFields(getParentLocale(locale));
}
return locales.reduce((numbers, locale) => {
locale = normalizeLocale(locale);
// Walk the `locale`'s hierarchy to look for suitable ancestor with the
// date calendars. If no ancestor is found, the given
// `locale` will be returned.
let resolvedLocale = findLocaleWithNumbersFields(locale);
// Add an entry for the `locale`, which might be an ancestor. If the
// locale doesn't have relative fields, then we fallback to the "root"
// locale's fields.
numbers[locale] = {
numbers: getNumbers(resolvedLocale)
};
return numbers;
}, {});
};
function loadNumbers(locale) {
return Object.assign(
require('cldr-numbers-full/main/' + locale + '/numbers.json').main[locale].numbers,
require('cldr-numbers-full/main/' + locale + '/currencies.json').main[locale].numbers
);
}