@formatjs/intl-locale 3.0.3 → 3.0.4

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 (45) hide show
  1. package/BUILD +132 -0
  2. package/CHANGELOG.md +436 -0
  3. package/LICENSE.md +0 -0
  4. package/README.md +0 -0
  5. package/get_internal_slots.ts +15 -0
  6. package/index.ts +529 -0
  7. package/package.json +4 -4
  8. package/polyfill-force.ts +8 -0
  9. package/polyfill.ts +10 -0
  10. package/should-polyfill.ts +14 -0
  11. package/tests/index.test.ts +18 -0
  12. package/tests/likely-subtags.test.ts +108 -0
  13. package/tests/minimize.test.ts +35 -0
  14. package/tsconfig.json +5 -0
  15. package/get_internal_slots.d.ts +0 -3
  16. package/get_internal_slots.d.ts.map +0 -1
  17. package/get_internal_slots.js +0 -14
  18. package/index.d.ts +0 -49
  19. package/index.d.ts.map +0 -1
  20. package/index.js +0 -394
  21. package/lib/get_internal_slots.d.ts +0 -3
  22. package/lib/get_internal_slots.d.ts.map +0 -1
  23. package/lib/get_internal_slots.js +0 -11
  24. package/lib/index.d.ts +0 -49
  25. package/lib/index.d.ts.map +0 -1
  26. package/lib/index.js +0 -391
  27. package/lib/polyfill-force.d.ts +0 -2
  28. package/lib/polyfill-force.d.ts.map +0 -1
  29. package/lib/polyfill-force.js +0 -7
  30. package/lib/polyfill.d.ts +0 -2
  31. package/lib/polyfill.d.ts.map +0 -1
  32. package/lib/polyfill.js +0 -10
  33. package/lib/should-polyfill.d.ts +0 -2
  34. package/lib/should-polyfill.d.ts.map +0 -1
  35. package/lib/should-polyfill.js +0 -14
  36. package/polyfill-force.d.ts +0 -2
  37. package/polyfill-force.d.ts.map +0 -1
  38. package/polyfill-force.js +0 -9
  39. package/polyfill.d.ts +0 -2
  40. package/polyfill.d.ts.map +0 -1
  41. package/polyfill.iife.js +0 -6468
  42. package/polyfill.js +0 -12
  43. package/should-polyfill.d.ts +0 -2
  44. package/should-polyfill.d.ts.map +0 -1
  45. package/should-polyfill.js +0 -18
package/lib/index.js DELETED
@@ -1,391 +0,0 @@
1
- import { __assign, __spreadArray } from "tslib";
2
- import { GetOption, invariant, SameValue, CoerceOptionsToObject, } from '@formatjs/ecma402-abstract';
3
- import { isStructurallyValidLanguageTag, isUnicodeLanguageSubtag, isUnicodeRegionSubtag, parseUnicodeLanguageId, isUnicodeScriptSubtag, emitUnicodeLocaleId, parseUnicodeLocaleId, emitUnicodeLanguageId, likelySubtags, } from '@formatjs/intl-getcanonicallocales';
4
- import getInternalSlots from './get_internal_slots';
5
- var RELEVANT_EXTENSION_KEYS = ['ca', 'co', 'hc', 'kf', 'kn', 'nu'];
6
- var UNICODE_TYPE_REGEX = /^[a-z0-9]{3,8}(-[a-z0-9]{3,8})*$/i;
7
- function applyOptionsToTag(tag, options) {
8
- invariant(typeof tag === 'string', 'language tag must be a string');
9
- invariant(isStructurallyValidLanguageTag(tag), 'malformed language tag', RangeError);
10
- var language = GetOption(options, 'language', 'string', undefined, undefined);
11
- if (language !== undefined) {
12
- invariant(isUnicodeLanguageSubtag(language), 'Malformed unicode_language_subtag', RangeError);
13
- }
14
- var script = GetOption(options, 'script', 'string', undefined, undefined);
15
- if (script !== undefined) {
16
- invariant(isUnicodeScriptSubtag(script), 'Malformed unicode_script_subtag', RangeError);
17
- }
18
- var region = GetOption(options, 'region', 'string', undefined, undefined);
19
- if (region !== undefined) {
20
- invariant(isUnicodeRegionSubtag(region), 'Malformed unicode_region_subtag', RangeError);
21
- }
22
- var languageId = parseUnicodeLanguageId(tag);
23
- if (language !== undefined) {
24
- languageId.lang = language;
25
- }
26
- if (script !== undefined) {
27
- languageId.script = script;
28
- }
29
- if (region !== undefined) {
30
- languageId.region = region;
31
- }
32
- return Intl.getCanonicalLocales(emitUnicodeLocaleId(__assign(__assign({}, parseUnicodeLocaleId(tag)), { lang: languageId })))[0];
33
- }
34
- function applyUnicodeExtensionToTag(tag, options, relevantExtensionKeys) {
35
- var unicodeExtension;
36
- var keywords = [];
37
- var ast = parseUnicodeLocaleId(tag);
38
- for (var _i = 0, _a = ast.extensions; _i < _a.length; _i++) {
39
- var ext = _a[_i];
40
- if (ext.type === 'u') {
41
- unicodeExtension = ext;
42
- if (Array.isArray(ext.keywords))
43
- keywords = ext.keywords;
44
- }
45
- }
46
- var result = Object.create(null);
47
- for (var _b = 0, relevantExtensionKeys_1 = relevantExtensionKeys; _b < relevantExtensionKeys_1.length; _b++) {
48
- var key = relevantExtensionKeys_1[_b];
49
- var value = void 0, entry = void 0;
50
- for (var _c = 0, keywords_1 = keywords; _c < keywords_1.length; _c++) {
51
- var keyword = keywords_1[_c];
52
- if (keyword[0] === key) {
53
- entry = keyword;
54
- value = entry[1];
55
- }
56
- }
57
- invariant(key in options, "".concat(key, " must be in options"));
58
- var optionsValue = options[key];
59
- if (optionsValue !== undefined) {
60
- invariant(typeof optionsValue === 'string', "Value for ".concat(key, " must be a string"));
61
- value = optionsValue;
62
- if (entry) {
63
- entry[1] = value;
64
- }
65
- else {
66
- keywords.push([key, value]);
67
- }
68
- }
69
- result[key] = value;
70
- }
71
- if (!unicodeExtension) {
72
- if (keywords.length) {
73
- ast.extensions.push({
74
- type: 'u',
75
- keywords: keywords,
76
- attributes: [],
77
- });
78
- }
79
- }
80
- else {
81
- unicodeExtension.keywords = keywords;
82
- }
83
- result.locale = Intl.getCanonicalLocales(emitUnicodeLocaleId(ast))[0];
84
- return result;
85
- }
86
- function mergeUnicodeLanguageId(lang, script, region, variants, replacement) {
87
- if (variants === void 0) { variants = []; }
88
- if (!replacement) {
89
- return {
90
- lang: lang || 'und',
91
- script: script,
92
- region: region,
93
- variants: variants,
94
- };
95
- }
96
- return {
97
- lang: !lang || lang === 'und' ? replacement.lang : lang,
98
- script: script || replacement.script,
99
- region: region || replacement.region,
100
- variants: __spreadArray(__spreadArray([], variants, true), replacement.variants, true),
101
- };
102
- }
103
- function addLikelySubtags(tag) {
104
- var ast = parseUnicodeLocaleId(tag);
105
- var unicodeLangId = ast.lang;
106
- var lang = unicodeLangId.lang, script = unicodeLangId.script, region = unicodeLangId.region, variants = unicodeLangId.variants;
107
- if (script && region) {
108
- var match_1 = likelySubtags[emitUnicodeLanguageId({ lang: lang, script: script, region: region, variants: [] })];
109
- if (match_1) {
110
- var parts_1 = parseUnicodeLanguageId(match_1);
111
- ast.lang = mergeUnicodeLanguageId(undefined, undefined, undefined, variants, parts_1);
112
- return emitUnicodeLocaleId(ast);
113
- }
114
- }
115
- if (script) {
116
- var match_2 = likelySubtags[emitUnicodeLanguageId({ lang: lang, script: script, variants: [] })];
117
- if (match_2) {
118
- var parts_2 = parseUnicodeLanguageId(match_2);
119
- ast.lang = mergeUnicodeLanguageId(undefined, undefined, region, variants, parts_2);
120
- return emitUnicodeLocaleId(ast);
121
- }
122
- }
123
- if (region) {
124
- var match_3 = likelySubtags[emitUnicodeLanguageId({ lang: lang, region: region, variants: [] })];
125
- if (match_3) {
126
- var parts_3 = parseUnicodeLanguageId(match_3);
127
- ast.lang = mergeUnicodeLanguageId(undefined, script, undefined, variants, parts_3);
128
- return emitUnicodeLocaleId(ast);
129
- }
130
- }
131
- var match = likelySubtags[lang] ||
132
- likelySubtags[emitUnicodeLanguageId({ lang: 'und', script: script, variants: [] })];
133
- if (!match) {
134
- throw new Error("No match for addLikelySubtags");
135
- }
136
- var parts = parseUnicodeLanguageId(match);
137
- ast.lang = mergeUnicodeLanguageId(undefined, script, region, variants, parts);
138
- return emitUnicodeLocaleId(ast);
139
- }
140
- /**
141
- * From: https://github.com/unicode-org/icu/blob/4231ca5be053a22a1be24eb891817458c97db709/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java#L2395
142
- * @param tag
143
- */
144
- function removeLikelySubtags(tag) {
145
- var maxLocale = addLikelySubtags(tag);
146
- if (!maxLocale) {
147
- return tag;
148
- }
149
- maxLocale = emitUnicodeLanguageId(__assign(__assign({}, parseUnicodeLanguageId(maxLocale)), { variants: [] }));
150
- var ast = parseUnicodeLocaleId(tag);
151
- var _a = ast.lang, lang = _a.lang, script = _a.script, region = _a.region, variants = _a.variants;
152
- var trial = addLikelySubtags(emitUnicodeLanguageId({ lang: lang, variants: [] }));
153
- if (trial === maxLocale) {
154
- return emitUnicodeLocaleId(__assign(__assign({}, ast), { lang: mergeUnicodeLanguageId(lang, undefined, undefined, variants) }));
155
- }
156
- if (region) {
157
- var trial_1 = addLikelySubtags(emitUnicodeLanguageId({ lang: lang, region: region, variants: [] }));
158
- if (trial_1 === maxLocale) {
159
- return emitUnicodeLocaleId(__assign(__assign({}, ast), { lang: mergeUnicodeLanguageId(lang, undefined, region, variants) }));
160
- }
161
- }
162
- if (script) {
163
- var trial_2 = addLikelySubtags(emitUnicodeLanguageId({ lang: lang, script: script, variants: [] }));
164
- if (trial_2 === maxLocale) {
165
- return emitUnicodeLocaleId(__assign(__assign({}, ast), { lang: mergeUnicodeLanguageId(lang, script, undefined, variants) }));
166
- }
167
- }
168
- return tag;
169
- }
170
- var Locale = /** @class */ (function () {
171
- function Locale(tag, opts) {
172
- // test262/test/intl402/RelativeTimeFormat/constructor/constructor/newtarget-undefined.js
173
- // Cannot use `new.target` bc of IE11 & TS transpiles it to something else
174
- var newTarget = this && this instanceof Locale ? this.constructor : void 0;
175
- if (!newTarget) {
176
- throw new TypeError("Intl.Locale must be called with 'new'");
177
- }
178
- var relevantExtensionKeys = Locale.relevantExtensionKeys;
179
- var internalSlotsList = [
180
- 'initializedLocale',
181
- 'locale',
182
- 'calendar',
183
- 'collation',
184
- 'hourCycle',
185
- 'numberingSystem',
186
- ];
187
- if (relevantExtensionKeys.indexOf('kf') > -1) {
188
- internalSlotsList.push('caseFirst');
189
- }
190
- if (relevantExtensionKeys.indexOf('kn') > -1) {
191
- internalSlotsList.push('numeric');
192
- }
193
- if (tag === undefined) {
194
- throw new TypeError("First argument to Intl.Locale constructor can't be empty or missing");
195
- }
196
- if (typeof tag !== 'string' && typeof tag !== 'object') {
197
- throw new TypeError('tag must be a string or object');
198
- }
199
- var internalSlots;
200
- if (typeof tag === 'object' &&
201
- (internalSlots = getInternalSlots(tag)) &&
202
- internalSlots.initializedLocale) {
203
- tag = internalSlots.locale;
204
- }
205
- else {
206
- tag = tag.toString();
207
- }
208
- internalSlots = getInternalSlots(this);
209
- var options = CoerceOptionsToObject(opts);
210
- tag = applyOptionsToTag(tag, options);
211
- var opt = Object.create(null);
212
- var calendar = GetOption(options, 'calendar', 'string', undefined, undefined);
213
- if (calendar !== undefined) {
214
- if (!UNICODE_TYPE_REGEX.test(calendar)) {
215
- throw new RangeError('invalid calendar');
216
- }
217
- }
218
- opt.ca = calendar;
219
- var collation = GetOption(options, 'collation', 'string', undefined, undefined);
220
- if (collation !== undefined) {
221
- if (!UNICODE_TYPE_REGEX.test(collation)) {
222
- throw new RangeError('invalid collation');
223
- }
224
- }
225
- opt.co = collation;
226
- var hc = GetOption(options, 'hourCycle', 'string', ['h11', 'h12', 'h23', 'h24'], undefined);
227
- opt.hc = hc;
228
- var kf = GetOption(options, 'caseFirst', 'string', ['upper', 'lower', 'false'], undefined);
229
- opt.kf = kf;
230
- var _kn = GetOption(options, 'numeric', 'boolean', undefined, undefined);
231
- var kn;
232
- if (_kn !== undefined) {
233
- kn = String(_kn);
234
- }
235
- opt.kn = kn;
236
- var numberingSystem = GetOption(options, 'numberingSystem', 'string', undefined, undefined);
237
- if (numberingSystem !== undefined) {
238
- if (!UNICODE_TYPE_REGEX.test(numberingSystem)) {
239
- throw new RangeError('Invalid numberingSystem');
240
- }
241
- }
242
- opt.nu = numberingSystem;
243
- var r = applyUnicodeExtensionToTag(tag, opt, relevantExtensionKeys);
244
- internalSlots.locale = r.locale;
245
- internalSlots.calendar = r.ca;
246
- internalSlots.collation = r.co;
247
- internalSlots.hourCycle = r.hc;
248
- if (relevantExtensionKeys.indexOf('kf') > -1) {
249
- internalSlots.caseFirst = r.kf;
250
- }
251
- if (relevantExtensionKeys.indexOf('kn') > -1) {
252
- internalSlots.numeric = SameValue(r.kn, 'true');
253
- }
254
- internalSlots.numberingSystem = r.nu;
255
- }
256
- /**
257
- * https://www.unicode.org/reports/tr35/#Likely_Subtags
258
- */
259
- Locale.prototype.maximize = function () {
260
- var locale = getInternalSlots(this).locale;
261
- try {
262
- var maximizedLocale = addLikelySubtags(locale);
263
- return new Locale(maximizedLocale);
264
- }
265
- catch (e) {
266
- return new Locale(locale);
267
- }
268
- };
269
- /**
270
- * https://www.unicode.org/reports/tr35/#Likely_Subtags
271
- */
272
- Locale.prototype.minimize = function () {
273
- var locale = getInternalSlots(this).locale;
274
- try {
275
- var minimizedLocale = removeLikelySubtags(locale);
276
- return new Locale(minimizedLocale);
277
- }
278
- catch (e) {
279
- return new Locale(locale);
280
- }
281
- };
282
- Locale.prototype.toString = function () {
283
- return getInternalSlots(this).locale;
284
- };
285
- Object.defineProperty(Locale.prototype, "baseName", {
286
- get: function () {
287
- var locale = getInternalSlots(this).locale;
288
- return emitUnicodeLanguageId(parseUnicodeLanguageId(locale));
289
- },
290
- enumerable: false,
291
- configurable: true
292
- });
293
- Object.defineProperty(Locale.prototype, "calendar", {
294
- get: function () {
295
- return getInternalSlots(this).calendar;
296
- },
297
- enumerable: false,
298
- configurable: true
299
- });
300
- Object.defineProperty(Locale.prototype, "collation", {
301
- get: function () {
302
- return getInternalSlots(this).collation;
303
- },
304
- enumerable: false,
305
- configurable: true
306
- });
307
- Object.defineProperty(Locale.prototype, "hourCycle", {
308
- get: function () {
309
- return getInternalSlots(this).hourCycle;
310
- },
311
- enumerable: false,
312
- configurable: true
313
- });
314
- Object.defineProperty(Locale.prototype, "caseFirst", {
315
- get: function () {
316
- return getInternalSlots(this).caseFirst;
317
- },
318
- enumerable: false,
319
- configurable: true
320
- });
321
- Object.defineProperty(Locale.prototype, "numeric", {
322
- get: function () {
323
- return getInternalSlots(this).numeric;
324
- },
325
- enumerable: false,
326
- configurable: true
327
- });
328
- Object.defineProperty(Locale.prototype, "numberingSystem", {
329
- get: function () {
330
- return getInternalSlots(this).numberingSystem;
331
- },
332
- enumerable: false,
333
- configurable: true
334
- });
335
- Object.defineProperty(Locale.prototype, "language", {
336
- /**
337
- * https://tc39.es/proposal-intl-locale/#sec-Intl.Locale.prototype.language
338
- */
339
- get: function () {
340
- var locale = getInternalSlots(this).locale;
341
- return parseUnicodeLanguageId(locale).lang;
342
- },
343
- enumerable: false,
344
- configurable: true
345
- });
346
- Object.defineProperty(Locale.prototype, "script", {
347
- /**
348
- * https://tc39.es/proposal-intl-locale/#sec-Intl.Locale.prototype.script
349
- */
350
- get: function () {
351
- var locale = getInternalSlots(this).locale;
352
- return parseUnicodeLanguageId(locale).script;
353
- },
354
- enumerable: false,
355
- configurable: true
356
- });
357
- Object.defineProperty(Locale.prototype, "region", {
358
- /**
359
- * https://tc39.es/proposal-intl-locale/#sec-Intl.Locale.prototype.region
360
- */
361
- get: function () {
362
- var locale = getInternalSlots(this).locale;
363
- return parseUnicodeLanguageId(locale).region;
364
- },
365
- enumerable: false,
366
- configurable: true
367
- });
368
- Locale.relevantExtensionKeys = RELEVANT_EXTENSION_KEYS;
369
- return Locale;
370
- }());
371
- export { Locale };
372
- try {
373
- if (typeof Symbol !== 'undefined') {
374
- Object.defineProperty(Locale.prototype, Symbol.toStringTag, {
375
- value: 'Intl.Locale',
376
- writable: false,
377
- enumerable: false,
378
- configurable: true,
379
- });
380
- }
381
- Object.defineProperty(Locale.prototype.constructor, 'length', {
382
- value: 1,
383
- writable: false,
384
- enumerable: false,
385
- configurable: true,
386
- });
387
- }
388
- catch (e) {
389
- // Meta fix so we're test262-compliant, not important
390
- }
391
- export default Locale;
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=polyfill-force.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"polyfill-force.d.ts","sourceRoot":"","sources":["../../../../../../packages/intl-locale/polyfill-force.ts"],"names":[],"mappings":""}
@@ -1,7 +0,0 @@
1
- import { Locale } from './';
2
- Object.defineProperty(Intl, 'Locale', {
3
- value: Locale,
4
- writable: true,
5
- enumerable: false,
6
- configurable: true,
7
- });
package/lib/polyfill.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=polyfill.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"polyfill.d.ts","sourceRoot":"","sources":["../../../../../../packages/intl-locale/polyfill.ts"],"names":[],"mappings":""}
package/lib/polyfill.js DELETED
@@ -1,10 +0,0 @@
1
- import { Locale } from './';
2
- import { shouldPolyfill } from './should-polyfill';
3
- if (shouldPolyfill()) {
4
- Object.defineProperty(Intl, 'Locale', {
5
- value: Locale,
6
- writable: true,
7
- enumerable: false,
8
- configurable: true,
9
- });
10
- }
@@ -1,2 +0,0 @@
1
- export declare function shouldPolyfill(): boolean;
2
- //# sourceMappingURL=should-polyfill.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"should-polyfill.d.ts","sourceRoot":"","sources":["../../../../../../packages/intl-locale/should-polyfill.ts"],"names":[],"mappings":"AAWA,wBAAgB,cAAc,YAE7B"}
@@ -1,14 +0,0 @@
1
- /**
2
- * https://bugs.chromium.org/p/v8/issues/detail?id=10682
3
- */
4
- function hasIntlGetCanonicalLocalesBug() {
5
- try {
6
- return new Intl.Locale('und-x-private').toString() === 'x-private';
7
- }
8
- catch (e) {
9
- return true;
10
- }
11
- }
12
- export function shouldPolyfill() {
13
- return !('Locale' in Intl) || hasIntlGetCanonicalLocalesBug();
14
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=polyfill-force.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"polyfill-force.d.ts","sourceRoot":"","sources":["../../../../../packages/intl-locale/polyfill-force.ts"],"names":[],"mappings":""}
package/polyfill-force.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var _1 = require("./");
4
- Object.defineProperty(Intl, 'Locale', {
5
- value: _1.Locale,
6
- writable: true,
7
- enumerable: false,
8
- configurable: true,
9
- });
package/polyfill.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=polyfill.d.ts.map
package/polyfill.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"polyfill.d.ts","sourceRoot":"","sources":["../../../../../packages/intl-locale/polyfill.ts"],"names":[],"mappings":""}