@formatjs/intl-getcanonicallocales 1.9.2 → 2.0.3

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.
Files changed (74) hide show
  1. package/BUILD +116 -0
  2. package/CHANGELOG.md +290 -0
  3. package/LICENSE.md +0 -0
  4. package/README.md +0 -0
  5. package/index.ts +43 -0
  6. package/package.json +3 -3
  7. package/polyfill.ts +23 -0
  8. package/scripts/aliases.ts +70 -0
  9. package/scripts/likely-subtags.ts +20 -0
  10. package/should-polyfill.ts +10 -0
  11. package/src/aliases.generated.ts +1137 -0
  12. package/src/canonicalizer.ts +272 -0
  13. package/src/emitter.ts +39 -0
  14. package/src/likelySubtags.generated.ts +1870 -0
  15. package/src/parser.ts +259 -0
  16. package/src/types.ts +63 -0
  17. package/tests/index.test.ts +24 -0
  18. package/tests/parser.test.ts +204 -0
  19. package/tsconfig.json +5 -0
  20. package/index.d.ts +0 -6
  21. package/index.d.ts.map +0 -1
  22. package/index.js +0 -42
  23. package/lib/index.d.ts +0 -6
  24. package/lib/index.d.ts.map +0 -1
  25. package/lib/index.js +0 -31
  26. package/lib/polyfill.d.ts +0 -2
  27. package/lib/polyfill.d.ts.map +0 -1
  28. package/lib/polyfill.js +0 -24
  29. package/lib/should-polyfill.d.ts +0 -2
  30. package/lib/should-polyfill.d.ts.map +0 -1
  31. package/lib/should-polyfill.js +0 -8
  32. package/lib/src/aliases.generated.d.ts +0 -5
  33. package/lib/src/aliases.generated.d.ts.map +0 -1
  34. package/lib/src/aliases.generated.js +0 -1137
  35. package/lib/src/canonicalizer.d.ts +0 -20
  36. package/lib/src/canonicalizer.d.ts.map +0 -1
  37. package/lib/src/canonicalizer.js +0 -219
  38. package/lib/src/emitter.d.ts +0 -4
  39. package/lib/src/emitter.d.ts.map +0 -1
  40. package/lib/src/emitter.js +0 -28
  41. package/lib/src/likelySubtags.generated.d.ts +0 -2
  42. package/lib/src/likelySubtags.generated.d.ts.map +0 -1
  43. package/lib/src/likelySubtags.generated.js +0 -1870
  44. package/lib/src/parser.d.ts +0 -10
  45. package/lib/src/parser.d.ts.map +0 -1
  46. package/lib/src/parser.js +0 -233
  47. package/lib/src/types.d.ts +0 -33
  48. package/lib/src/types.d.ts.map +0 -1
  49. package/lib/src/types.js +0 -1
  50. package/polyfill.d.ts +0 -2
  51. package/polyfill.d.ts.map +0 -1
  52. package/polyfill.iife.js +0 -3526
  53. package/polyfill.js +0 -26
  54. package/should-polyfill.d.ts +0 -2
  55. package/should-polyfill.d.ts.map +0 -1
  56. package/should-polyfill.js +0 -12
  57. package/src/aliases.generated.d.ts +0 -5
  58. package/src/aliases.generated.d.ts.map +0 -1
  59. package/src/aliases.generated.js +0 -1140
  60. package/src/canonicalizer.d.ts +0 -20
  61. package/src/canonicalizer.d.ts.map +0 -1
  62. package/src/canonicalizer.js +0 -224
  63. package/src/emitter.d.ts +0 -4
  64. package/src/emitter.d.ts.map +0 -1
  65. package/src/emitter.js +0 -33
  66. package/src/likelySubtags.generated.d.ts +0 -2
  67. package/src/likelySubtags.generated.d.ts.map +0 -1
  68. package/src/likelySubtags.generated.js +0 -1873
  69. package/src/parser.d.ts +0 -10
  70. package/src/parser.d.ts.map +0 -1
  71. package/src/parser.js +0 -243
  72. package/src/types.d.ts +0 -33
  73. package/src/types.d.ts.map +0 -1
  74. package/src/types.js +0 -2
@@ -1,20 +0,0 @@
1
- import { UnicodeLocaleId, UnicodeLanguageId } from './types';
2
- /**
3
- * CAVEAT: We don't do this section in the spec bc they have no JSON data
4
- * Use the bcp47 data to replace keys, types, tfields, and tvalues by their canonical forms. See Section 3.6.4 U Extension Data Files) and Section 3.7.1 T Extension Data Files. The aliases are in the alias attribute value, while the canonical is in the name attribute value. For example,
5
- Because of the following bcp47 data:
6
- <key name="ms"…>…<type name="uksystem" … alias="imperial" … />…</key>
7
- We get the following transformation:
8
- en-u-ms-imperial ⇒ en-u-ms-uksystem
9
- * @param lang
10
- */
11
- export declare function canonicalizeUnicodeLanguageId(unicodeLanguageId: UnicodeLanguageId): UnicodeLanguageId;
12
- /**
13
- * Canonicalize based on
14
- * https://www.unicode.org/reports/tr35/tr35.html#Canonical_Unicode_Locale_Identifiers
15
- * https://tc39.es/ecma402/#sec-canonicalizeunicodelocaleid
16
- * IMPORTANT: This modifies the object inline
17
- * @param locale
18
- */
19
- export declare function canonicalizeUnicodeLocaleId(locale: UnicodeLocaleId): UnicodeLocaleId;
20
- //# sourceMappingURL=canonicalizer.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"canonicalizer.d.ts","sourceRoot":"","sources":["../../../../../../../packages/intl-getcanonicallocales/src/canonicalizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAiB,iBAAiB,EAAC,MAAM,SAAS,CAAA;AA4DzE;;;;;;;;GAQG;AACH,wBAAgB,6BAA6B,CAC3C,iBAAiB,EAAE,iBAAiB,GACnC,iBAAiB,CAmKnB;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,eAAe,GACtB,eAAe,CA0BjB"}
@@ -1,219 +0,0 @@
1
- import { __spreadArray } from "tslib";
2
- import { languageAlias, variantAlias, scriptAlias, territoryAlias, } from './aliases.generated';
3
- import { parseUnicodeLanguageId, isUnicodeVariantSubtag, isUnicodeLanguageSubtag, SEPARATOR, } from './parser';
4
- import { likelySubtags } from './likelySubtags.generated';
5
- import { emitUnicodeLanguageId } from './emitter';
6
- function canonicalizeAttrs(strs) {
7
- return Object.keys(strs.reduce(function (all, str) {
8
- all[str.toLowerCase()] = 1;
9
- return all;
10
- }, {})).sort();
11
- }
12
- function canonicalizeKVs(arr) {
13
- var all = {};
14
- var result = [];
15
- for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
16
- var kv = arr_1[_i];
17
- if (kv[0] in all) {
18
- continue;
19
- }
20
- all[kv[0]] = 1;
21
- if (!kv[1] || kv[1] === 'true') {
22
- result.push([kv[0].toLowerCase()]);
23
- }
24
- else {
25
- result.push([kv[0].toLowerCase(), kv[1].toLowerCase()]);
26
- }
27
- }
28
- return result.sort(compareKV);
29
- }
30
- function compareKV(t1, t2) {
31
- return t1[0] < t2[0] ? -1 : t1[0] > t2[0] ? 1 : 0;
32
- }
33
- function compareExtension(e1, e2) {
34
- return e1.type < e2.type ? -1 : e1.type > e2.type ? 1 : 0;
35
- }
36
- function mergeVariants(v1, v2) {
37
- var result = __spreadArray([], v1, true);
38
- for (var _i = 0, v2_1 = v2; _i < v2_1.length; _i++) {
39
- var v = v2_1[_i];
40
- if (v1.indexOf(v) < 0) {
41
- result.push(v);
42
- }
43
- }
44
- return result;
45
- }
46
- /**
47
- * CAVEAT: We don't do this section in the spec bc they have no JSON data
48
- * Use the bcp47 data to replace keys, types, tfields, and tvalues by their canonical forms. See Section 3.6.4 U Extension Data Files) and Section 3.7.1 T Extension Data Files. The aliases are in the alias attribute value, while the canonical is in the name attribute value. For example,
49
- Because of the following bcp47 data:
50
- <key name="ms"…>…<type name="uksystem" … alias="imperial" … />…</key>
51
- We get the following transformation:
52
- en-u-ms-imperial ⇒ en-u-ms-uksystem
53
- * @param lang
54
- */
55
- export function canonicalizeUnicodeLanguageId(unicodeLanguageId) {
56
- /**
57
- * If the language subtag matches the type attribute of a languageAlias element in Supplemental Data, replace the language subtag with the replacement value.
58
- * 1. If there are additional subtags in the replacement value, add them to the result, but only if there is no corresponding subtag already in the tag.
59
- * 2. Five special deprecated grandfathered codes (such as i-default) are in type attributes, and are also replaced.
60
- */
61
- // From https://github.com/unicode-org/icu/blob/master/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java#L1246
62
- // Try language _ variant
63
- var finalLangAst = unicodeLanguageId;
64
- if (unicodeLanguageId.variants.length) {
65
- var replacedLang_1 = '';
66
- for (var _i = 0, _a = unicodeLanguageId.variants; _i < _a.length; _i++) {
67
- var variant = _a[_i];
68
- if ((replacedLang_1 =
69
- languageAlias[emitUnicodeLanguageId({
70
- lang: unicodeLanguageId.lang,
71
- variants: [variant],
72
- })])) {
73
- var replacedLangAst = parseUnicodeLanguageId(replacedLang_1.split(SEPARATOR));
74
- finalLangAst = {
75
- lang: replacedLangAst.lang,
76
- script: finalLangAst.script || replacedLangAst.script,
77
- region: finalLangAst.region || replacedLangAst.region,
78
- variants: mergeVariants(finalLangAst.variants, replacedLangAst.variants),
79
- };
80
- break;
81
- }
82
- }
83
- }
84
- // language _ script _ country
85
- // ug-Arab-CN -> ug-CN
86
- if (finalLangAst.script && finalLangAst.region) {
87
- var replacedLang_2 = languageAlias[emitUnicodeLanguageId({
88
- lang: finalLangAst.lang,
89
- script: finalLangAst.script,
90
- region: finalLangAst.region,
91
- variants: [],
92
- })];
93
- if (replacedLang_2) {
94
- var replacedLangAst = parseUnicodeLanguageId(replacedLang_2.split(SEPARATOR));
95
- finalLangAst = {
96
- lang: replacedLangAst.lang,
97
- script: replacedLangAst.script,
98
- region: replacedLangAst.region,
99
- variants: finalLangAst.variants,
100
- };
101
- }
102
- }
103
- // language _ country
104
- // eg. az_AZ -> az_Latn_A
105
- if (finalLangAst.region) {
106
- var replacedLang_3 = languageAlias[emitUnicodeLanguageId({
107
- lang: finalLangAst.lang,
108
- region: finalLangAst.region,
109
- variants: [],
110
- })];
111
- if (replacedLang_3) {
112
- var replacedLangAst = parseUnicodeLanguageId(replacedLang_3.split(SEPARATOR));
113
- finalLangAst = {
114
- lang: replacedLangAst.lang,
115
- script: finalLangAst.script || replacedLangAst.script,
116
- region: replacedLangAst.region,
117
- variants: finalLangAst.variants,
118
- };
119
- }
120
- }
121
- // only language
122
- // e.g. twi -> ak
123
- var replacedLang = languageAlias[emitUnicodeLanguageId({
124
- lang: finalLangAst.lang,
125
- variants: [],
126
- })];
127
- if (replacedLang) {
128
- var replacedLangAst = parseUnicodeLanguageId(replacedLang.split(SEPARATOR));
129
- finalLangAst = {
130
- lang: replacedLangAst.lang,
131
- script: finalLangAst.script || replacedLangAst.script,
132
- region: finalLangAst.region || replacedLangAst.region,
133
- variants: finalLangAst.variants,
134
- };
135
- }
136
- if (finalLangAst.region) {
137
- var region = finalLangAst.region.toUpperCase();
138
- var regionAlias = territoryAlias[region];
139
- var replacedRegion = void 0;
140
- if (regionAlias) {
141
- var regions = regionAlias.split(' ');
142
- replacedRegion = regions[0];
143
- var likelySubtag = likelySubtags[emitUnicodeLanguageId({
144
- lang: finalLangAst.lang,
145
- script: finalLangAst.script,
146
- variants: [],
147
- })];
148
- if (likelySubtag) {
149
- var likelyRegion = parseUnicodeLanguageId(likelySubtag.split(SEPARATOR)).region;
150
- if (likelyRegion && regions.indexOf(likelyRegion) > -1) {
151
- replacedRegion = likelyRegion;
152
- }
153
- }
154
- }
155
- if (replacedRegion) {
156
- finalLangAst.region = replacedRegion;
157
- }
158
- finalLangAst.region = finalLangAst.region.toUpperCase();
159
- }
160
- if (finalLangAst.script) {
161
- finalLangAst.script =
162
- finalLangAst.script[0].toUpperCase() +
163
- finalLangAst.script.slice(1).toLowerCase();
164
- if (scriptAlias[finalLangAst.script]) {
165
- finalLangAst.script = scriptAlias[finalLangAst.script];
166
- }
167
- }
168
- if (finalLangAst.variants.length) {
169
- for (var i = 0; i < finalLangAst.variants.length; i++) {
170
- var variant = finalLangAst.variants[i].toLowerCase();
171
- if (variantAlias[variant]) {
172
- var alias = variantAlias[variant];
173
- if (isUnicodeVariantSubtag(alias)) {
174
- finalLangAst.variants[i] = alias;
175
- }
176
- else if (isUnicodeLanguageSubtag(alias)) {
177
- // Yes this can happen per the spec
178
- finalLangAst.lang = alias;
179
- }
180
- }
181
- }
182
- finalLangAst.variants.sort();
183
- }
184
- return finalLangAst;
185
- }
186
- /**
187
- * Canonicalize based on
188
- * https://www.unicode.org/reports/tr35/tr35.html#Canonical_Unicode_Locale_Identifiers
189
- * https://tc39.es/ecma402/#sec-canonicalizeunicodelocaleid
190
- * IMPORTANT: This modifies the object inline
191
- * @param locale
192
- */
193
- export function canonicalizeUnicodeLocaleId(locale) {
194
- locale.lang = canonicalizeUnicodeLanguageId(locale.lang);
195
- if (locale.extensions) {
196
- for (var _i = 0, _a = locale.extensions; _i < _a.length; _i++) {
197
- var extension = _a[_i];
198
- switch (extension.type) {
199
- case 'u':
200
- extension.keywords = canonicalizeKVs(extension.keywords);
201
- if (extension.attributes) {
202
- extension.attributes = canonicalizeAttrs(extension.attributes);
203
- }
204
- break;
205
- case 't':
206
- if (extension.lang) {
207
- extension.lang = canonicalizeUnicodeLanguageId(extension.lang);
208
- }
209
- extension.fields = canonicalizeKVs(extension.fields);
210
- break;
211
- default:
212
- extension.value = extension.value.toLowerCase();
213
- break;
214
- }
215
- }
216
- locale.extensions.sort(compareExtension);
217
- }
218
- return locale;
219
- }
@@ -1,4 +0,0 @@
1
- import { UnicodeLanguageId, UnicodeLocaleId } from './types';
2
- export declare function emitUnicodeLanguageId(lang?: UnicodeLanguageId): string;
3
- export declare function emitUnicodeLocaleId({ lang, extensions, }: UnicodeLocaleId): string;
4
- //# sourceMappingURL=emitter.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"emitter.d.ts","sourceRoot":"","sources":["../../../../../../../packages/intl-getcanonicallocales/src/emitter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,iBAAiB,EAAE,eAAe,EAAC,MAAM,SAAS,CAAA;AAE1D,wBAAgB,qBAAqB,CAAC,IAAI,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAOtE;AAED,wBAAgB,mBAAmB,CAAC,EAClC,IAAI,EACJ,UAAU,GACX,EAAE,eAAe,GAAG,MAAM,CAwB1B"}
@@ -1,28 +0,0 @@
1
- import { __spreadArray } from "tslib";
2
- export function emitUnicodeLanguageId(lang) {
3
- if (!lang) {
4
- return '';
5
- }
6
- return __spreadArray([lang.lang, lang.script, lang.region], (lang.variants || []), true).filter(Boolean)
7
- .join('-');
8
- }
9
- export function emitUnicodeLocaleId(_a) {
10
- var lang = _a.lang, extensions = _a.extensions;
11
- var chunks = [emitUnicodeLanguageId(lang)];
12
- for (var _i = 0, extensions_1 = extensions; _i < extensions_1.length; _i++) {
13
- var ext = extensions_1[_i];
14
- chunks.push(ext.type);
15
- switch (ext.type) {
16
- case 'u':
17
- chunks.push.apply(chunks, __spreadArray(__spreadArray([], ext.attributes, false), ext.keywords.reduce(function (all, kv) { return all.concat(kv); }, []), false));
18
- break;
19
- case 't':
20
- chunks.push.apply(chunks, __spreadArray([emitUnicodeLanguageId(ext.lang)], ext.fields.reduce(function (all, kv) { return all.concat(kv); }, []), false));
21
- break;
22
- default:
23
- chunks.push(ext.value);
24
- break;
25
- }
26
- }
27
- return chunks.filter(Boolean).join('-');
28
- }
@@ -1,2 +0,0 @@
1
- export declare const likelySubtags: Record<string, string>;
2
- //# sourceMappingURL=likelySubtags.generated.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"likelySubtags.generated.d.ts","sourceRoot":"","sources":["../../../../../../../packages/intl-getcanonicallocales/src/likelySubtags.generated.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CA20DhD,CAAC"}