@formatjs/intl-localematcher 0.5.7 → 0.5.9

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 (70) hide show
  1. package/README.md +1 -1
  2. package/abstract/BestAvailableLocale.js +0 -1
  3. package/abstract/BestFitMatcher.js +0 -1
  4. package/abstract/CanonicalizeLocaleList.js +0 -2
  5. package/abstract/CanonicalizeUValue.d.ts +1 -0
  6. package/abstract/CanonicalizeUValue.js +11 -0
  7. package/abstract/CanonicalizeUnicodeLocaleId.d.ts +1 -0
  8. package/abstract/CanonicalizeUnicodeLocaleId.js +6 -0
  9. package/abstract/InsertUnicodeExtensionAndCanonicalize.d.ts +2 -0
  10. package/abstract/InsertUnicodeExtensionAndCanonicalize.js +35 -0
  11. package/abstract/LookupMatcher.js +0 -1
  12. package/abstract/LookupSupportedLocales.js +0 -1
  13. package/abstract/ResolveLocale.js +71 -46
  14. package/abstract/UnicodeExtensionComponents.d.ts +5 -0
  15. package/abstract/UnicodeExtensionComponents.js +45 -0
  16. package/abstract/UnicodeExtensionValue.js +0 -1
  17. package/abstract/languageMatching.d.ts +2234 -7968
  18. package/abstract/languageMatching.js +0 -1
  19. package/abstract/regions.generated.js +0 -1
  20. package/abstract/types.d.ts +4 -0
  21. package/abstract/types.js +0 -1
  22. package/abstract/utils.js +0 -1
  23. package/index.js +0 -1
  24. package/lib/abstract/BestAvailableLocale.js +0 -1
  25. package/lib/abstract/BestFitMatcher.js +0 -1
  26. package/lib/abstract/CanonicalizeLocaleList.js +0 -2
  27. package/lib/abstract/CanonicalizeUValue.d.ts +1 -0
  28. package/lib/abstract/CanonicalizeUValue.js +8 -0
  29. package/lib/abstract/CanonicalizeUnicodeLocaleId.d.ts +1 -0
  30. package/lib/abstract/CanonicalizeUnicodeLocaleId.js +3 -0
  31. package/lib/abstract/InsertUnicodeExtensionAndCanonicalize.d.ts +2 -0
  32. package/lib/abstract/InsertUnicodeExtensionAndCanonicalize.js +32 -0
  33. package/lib/abstract/LookupMatcher.js +0 -1
  34. package/lib/abstract/LookupSupportedLocales.js +0 -1
  35. package/lib/abstract/ResolveLocale.js +71 -46
  36. package/lib/abstract/UnicodeExtensionComponents.d.ts +5 -0
  37. package/lib/abstract/UnicodeExtensionComponents.js +42 -0
  38. package/lib/abstract/UnicodeExtensionValue.js +0 -1
  39. package/lib/abstract/languageMatching.d.ts +2234 -7968
  40. package/lib/abstract/languageMatching.js +0 -1
  41. package/lib/abstract/regions.generated.js +0 -1
  42. package/lib/abstract/types.d.ts +4 -0
  43. package/lib/abstract/types.js +0 -1
  44. package/lib/abstract/utils.js +0 -1
  45. package/lib/index.js +0 -1
  46. package/package.json +1 -1
  47. package/abstract/BestAvailableLocale.js.map +0 -1
  48. package/abstract/BestFitMatcher.js.map +0 -1
  49. package/abstract/CanonicalizeLocaleList.js.map +0 -1
  50. package/abstract/LookupMatcher.js.map +0 -1
  51. package/abstract/LookupSupportedLocales.js.map +0 -1
  52. package/abstract/ResolveLocale.js.map +0 -1
  53. package/abstract/UnicodeExtensionValue.js.map +0 -1
  54. package/abstract/languageMatching.js.map +0 -1
  55. package/abstract/regions.generated.js.map +0 -1
  56. package/abstract/types.js.map +0 -1
  57. package/abstract/utils.js.map +0 -1
  58. package/index.js.map +0 -1
  59. package/lib/abstract/BestAvailableLocale.js.map +0 -1
  60. package/lib/abstract/BestFitMatcher.js.map +0 -1
  61. package/lib/abstract/CanonicalizeLocaleList.js.map +0 -1
  62. package/lib/abstract/LookupMatcher.js.map +0 -1
  63. package/lib/abstract/LookupSupportedLocales.js.map +0 -1
  64. package/lib/abstract/ResolveLocale.js.map +0 -1
  65. package/lib/abstract/UnicodeExtensionValue.js.map +0 -1
  66. package/lib/abstract/languageMatching.js.map +0 -1
  67. package/lib/abstract/regions.generated.js.map +0 -1
  68. package/lib/abstract/types.js.map +0 -1
  69. package/lib/abstract/utils.js.map +0 -1
  70. package/lib/index.js.map +0 -1
@@ -2621,4 +2621,3 @@ exports.data = {
2621
2621
  },
2622
2622
  },
2623
2623
  };
2624
- //# sourceMappingURL=languageMatching.js.map
@@ -1335,4 +1335,3 @@ exports.regions = {
1335
1335
  "ZW"
1336
1336
  ]
1337
1337
  };
1338
- //# sourceMappingURL=regions.generated.js.map
@@ -3,3 +3,7 @@ export interface LookupMatcherResult {
3
3
  extension?: string;
4
4
  nu?: string;
5
5
  }
6
+ export interface Keyword {
7
+ key: string;
8
+ value: string;
9
+ }
package/abstract/types.js CHANGED
@@ -1,3 +1,2 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=types.js.map
package/abstract/utils.js CHANGED
@@ -169,4 +169,3 @@ function findBestMatch(requestedLocales, supportedLocales, threshold) {
169
169
  }
170
170
  return result;
171
171
  }
172
- //# sourceMappingURL=utils.js.map
package/index.js CHANGED
@@ -13,4 +13,3 @@ var LookupSupportedLocales_1 = require("./abstract/LookupSupportedLocales");
13
13
  Object.defineProperty(exports, "LookupSupportedLocales", { enumerable: true, get: function () { return LookupSupportedLocales_1.LookupSupportedLocales; } });
14
14
  var ResolveLocale_2 = require("./abstract/ResolveLocale");
15
15
  Object.defineProperty(exports, "ResolveLocale", { enumerable: true, get: function () { return ResolveLocale_2.ResolveLocale; } });
16
- //# sourceMappingURL=index.js.map
@@ -19,4 +19,3 @@ export function BestAvailableLocale(availableLocales, locale) {
19
19
  candidate = candidate.slice(0, pos);
20
20
  }
21
21
  }
22
- //# sourceMappingURL=BestAvailableLocale.js.map
@@ -29,4 +29,3 @@ export function BestFitMatcher(availableLocales, requestedLocales, getDefaultLoc
29
29
  extension: extension,
30
30
  };
31
31
  }
32
- //# sourceMappingURL=BestFitMatcher.js.map
@@ -3,7 +3,5 @@
3
3
  * @param locales
4
4
  */
5
5
  export function CanonicalizeLocaleList(locales) {
6
- // TODO
7
6
  return Intl.getCanonicalLocales(locales);
8
7
  }
9
- //# sourceMappingURL=CanonicalizeLocaleList.js.map
@@ -0,0 +1 @@
1
+ export declare function CanonicalizeUValue(ukey: string, uvalue: string): string;
@@ -0,0 +1,8 @@
1
+ import { invariant } from './utils';
2
+ export function CanonicalizeUValue(ukey, uvalue) {
3
+ // TODO: Implement algorithm for CanonicalizeUValue per https://tc39.es/ecma402/#sec-canonicalizeuvalue
4
+ var lowerValue = uvalue.toLowerCase();
5
+ invariant(ukey !== undefined, "ukey must be defined");
6
+ var canonicalized = lowerValue;
7
+ return canonicalized;
8
+ }
@@ -0,0 +1 @@
1
+ export declare function CanonicalizeUnicodeLocaleId(locale: string): string;
@@ -0,0 +1,3 @@
1
+ export function CanonicalizeUnicodeLocaleId(locale) {
2
+ return Intl.getCanonicalLocales(locale)[0];
3
+ }
@@ -0,0 +1,2 @@
1
+ import { Keyword } from './types';
2
+ export declare function InsertUnicodeExtensionAndCanonicalize(locale: string, attributes: string[], keywords: Array<Keyword>): string;
@@ -0,0 +1,32 @@
1
+ import { CanonicalizeUnicodeLocaleId } from './CanonicalizeUnicodeLocaleId';
2
+ import { invariant } from './utils';
3
+ export function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
4
+ invariant(locale.indexOf('-u-') === -1, 'Expected locale to not have a Unicode locale extension');
5
+ var extension = '-u';
6
+ for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) {
7
+ var attr = attributes_1[_i];
8
+ extension += "-".concat(attr);
9
+ }
10
+ for (var _a = 0, keywords_1 = keywords; _a < keywords_1.length; _a++) {
11
+ var kw = keywords_1[_a];
12
+ var key = kw.key, value = kw.value;
13
+ extension += "-".concat(key);
14
+ if (value !== '') {
15
+ extension += "-".concat(value);
16
+ }
17
+ }
18
+ if (extension === '-u') {
19
+ return CanonicalizeUnicodeLocaleId(locale);
20
+ }
21
+ var privateIndex = locale.indexOf('-x-');
22
+ var newLocale;
23
+ if (privateIndex === -1) {
24
+ newLocale = locale + extension;
25
+ }
26
+ else {
27
+ var preExtension = locale.slice(0, privateIndex);
28
+ var postExtension = locale.slice(privateIndex);
29
+ newLocale = preExtension + extension + postExtension;
30
+ }
31
+ return CanonicalizeUnicodeLocaleId(newLocale);
32
+ }
@@ -23,4 +23,3 @@ export function LookupMatcher(availableLocales, requestedLocales, getDefaultLoca
23
23
  result.locale = getDefaultLocale();
24
24
  return result;
25
25
  }
26
- //# sourceMappingURL=LookupMatcher.js.map
@@ -17,4 +17,3 @@ export function LookupSupportedLocales(availableLocales, requestedLocales) {
17
17
  }
18
18
  return subset;
19
19
  }
20
- //# sourceMappingURL=LookupSupportedLocales.js.map
@@ -1,11 +1,14 @@
1
1
  import { BestFitMatcher } from './BestFitMatcher';
2
+ import { CanonicalizeUValue } from './CanonicalizeUValue';
3
+ import { InsertUnicodeExtensionAndCanonicalize } from './InsertUnicodeExtensionAndCanonicalize';
2
4
  import { LookupMatcher } from './LookupMatcher';
3
- import { UnicodeExtensionValue } from './UnicodeExtensionValue';
5
+ import { UnicodeExtensionComponents } from './UnicodeExtensionComponents';
4
6
  import { invariant } from './utils';
5
7
  /**
6
8
  * https://tc39.es/ecma402/#sec-resolvelocale
7
9
  */
8
10
  export function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
11
+ var _a;
9
12
  var matcher = options.localeMatcher;
10
13
  var r;
11
14
  if (matcher === 'lookup') {
@@ -14,62 +17,84 @@ export function ResolveLocale(availableLocales, requestedLocales, options, relev
14
17
  else {
15
18
  r = BestFitMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
16
19
  }
20
+ if (r == null) {
21
+ r = {
22
+ locale: getDefaultLocale(),
23
+ extension: '',
24
+ };
25
+ }
17
26
  var foundLocale = r.locale;
18
- var result = { locale: '', dataLocale: foundLocale };
19
- var supportedExtension = '-u';
20
- for (var _i = 0, relevantExtensionKeys_1 = relevantExtensionKeys; _i < relevantExtensionKeys_1.length; _i++) {
21
- var key = relevantExtensionKeys_1[_i];
22
- invariant(foundLocale in localeData, "Missing locale data for ".concat(foundLocale));
23
- var foundLocaleData = localeData[foundLocale];
24
- invariant(typeof foundLocaleData === 'object' && foundLocaleData !== null, "locale data ".concat(key, " must be an object"));
25
- var keyLocaleData = foundLocaleData[key];
27
+ var foundLocaleData = localeData[foundLocale];
28
+ // TODO: We can't really guarantee that the locale data is available
29
+ // invariant(
30
+ // foundLocaleData !== undefined,
31
+ // `Missing locale data for ${foundLocale}`
32
+ // )
33
+ var result = { locale: 'en', dataLocale: foundLocale };
34
+ var components;
35
+ var keywords;
36
+ if (r.extension) {
37
+ components = UnicodeExtensionComponents(r.extension);
38
+ keywords = components.keywords;
39
+ }
40
+ else {
41
+ keywords = [];
42
+ }
43
+ var supportedKeywords = [];
44
+ var _loop_1 = function (key) {
45
+ // TODO: Shouldn't default to empty array, see TODO above
46
+ var keyLocaleData = (_a = foundLocaleData === null || foundLocaleData === void 0 ? void 0 : foundLocaleData[key]) !== null && _a !== void 0 ? _a : [];
26
47
  invariant(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
27
48
  var value = keyLocaleData[0];
28
- invariant(typeof value === 'string' || value === null, "value must be string or null but got ".concat(typeof value, " in key ").concat(key));
29
- var supportedExtensionAddition = '';
30
- if (r.extension) {
31
- var requestedValue = UnicodeExtensionValue(r.extension, key);
32
- if (requestedValue !== undefined) {
33
- if (requestedValue !== '') {
34
- if (~keyLocaleData.indexOf(requestedValue)) {
35
- value = requestedValue;
36
- supportedExtensionAddition = "-".concat(key, "-").concat(value);
37
- }
38
- }
39
- else if (~requestedValue.indexOf('true')) {
40
- value = 'true';
41
- supportedExtensionAddition = "-".concat(key);
49
+ invariant(value === undefined || typeof value === 'string', "value must be a string or undefined");
50
+ var supportedKeyword = void 0;
51
+ var entry = keywords.find(function (k) { return k.key === key; });
52
+ if (entry) {
53
+ var requestedValue = entry.value;
54
+ if (requestedValue !== '') {
55
+ if (keyLocaleData.indexOf(requestedValue) > -1) {
56
+ value = requestedValue;
57
+ supportedKeyword = {
58
+ key: key,
59
+ value: value,
60
+ };
42
61
  }
43
62
  }
63
+ else if (keyLocaleData.indexOf('true') > -1) {
64
+ value = 'true';
65
+ supportedKeyword = {
66
+ key: key,
67
+ value: value,
68
+ };
69
+ }
44
70
  }
45
- if (key in options) {
46
- var optionsValue = options[key];
47
- invariant(typeof optionsValue === 'string' ||
48
- typeof optionsValue === 'undefined' ||
49
- optionsValue === null, 'optionsValue must be String, Undefined or Null');
50
- if (~keyLocaleData.indexOf(optionsValue)) {
51
- if (optionsValue !== value) {
52
- value = optionsValue;
53
- supportedExtensionAddition = '';
54
- }
71
+ var optionsValue = options[key];
72
+ invariant(optionsValue == null || typeof optionsValue === 'string', "optionsValue must be a string or undefined");
73
+ if (typeof optionsValue === 'string') {
74
+ var ukey = key.toLowerCase();
75
+ optionsValue = CanonicalizeUValue(ukey, optionsValue);
76
+ if (optionsValue === '') {
77
+ optionsValue = 'true';
55
78
  }
56
79
  }
57
- result[key] = value;
58
- supportedExtension += supportedExtensionAddition;
59
- }
60
- if (supportedExtension.length > 2) {
61
- var privateIndex = foundLocale.indexOf('-x-');
62
- if (privateIndex === -1) {
63
- foundLocale = foundLocale + supportedExtension;
80
+ if (optionsValue !== value && keyLocaleData.indexOf(optionsValue) > -1) {
81
+ value = optionsValue;
82
+ supportedKeyword = undefined;
64
83
  }
65
- else {
66
- var preExtension = foundLocale.slice(0, privateIndex);
67
- var postExtension = foundLocale.slice(privateIndex, foundLocale.length);
68
- foundLocale = preExtension + supportedExtension + postExtension;
84
+ if (supportedKeyword) {
85
+ supportedKeywords.push(supportedKeyword);
69
86
  }
70
- foundLocale = Intl.getCanonicalLocales(foundLocale)[0];
87
+ result[key] = value;
88
+ };
89
+ for (var _i = 0, relevantExtensionKeys_1 = relevantExtensionKeys; _i < relevantExtensionKeys_1.length; _i++) {
90
+ var key = relevantExtensionKeys_1[_i];
91
+ _loop_1(key);
92
+ }
93
+ var supportedAttributes = [];
94
+ if (supportedKeywords.length > 0) {
95
+ supportedAttributes = [];
96
+ foundLocale = InsertUnicodeExtensionAndCanonicalize(foundLocale, supportedAttributes, supportedKeywords);
71
97
  }
72
98
  result.locale = foundLocale;
73
99
  return result;
74
100
  }
75
- //# sourceMappingURL=ResolveLocale.js.map
@@ -0,0 +1,5 @@
1
+ import { Keyword } from './types';
2
+ export declare function UnicodeExtensionComponents(extension: string): {
3
+ attributes: string[];
4
+ keywords: Array<Keyword>;
5
+ };
@@ -0,0 +1,42 @@
1
+ import { invariant } from './utils';
2
+ export function UnicodeExtensionComponents(extension) {
3
+ invariant(extension === extension.toLowerCase(), 'Expected extension to be lowercase');
4
+ invariant(extension.slice(0, 3) === '-u-', 'Expected extension to be a Unicode locale extension');
5
+ var attributes = [];
6
+ var keywords = [];
7
+ var keyword;
8
+ var size = extension.length;
9
+ var k = 3;
10
+ while (k < size) {
11
+ var e = extension.indexOf('-', k);
12
+ var len = void 0;
13
+ if (e === -1) {
14
+ len = size - k;
15
+ }
16
+ else {
17
+ len = e - k;
18
+ }
19
+ var subtag = extension.slice(k, k + len);
20
+ invariant(len >= 2, 'Expected a subtag to have at least 2 characters');
21
+ if (keyword === undefined && len != 2) {
22
+ if (attributes.indexOf(subtag) === -1) {
23
+ attributes.push(subtag);
24
+ }
25
+ }
26
+ else if (len === 2) {
27
+ keyword = { key: subtag, value: '' };
28
+ if (keywords.find(function (k) { return k.key === (keyword === null || keyword === void 0 ? void 0 : keyword.key); }) === undefined) {
29
+ keywords.push(keyword);
30
+ }
31
+ }
32
+ else if ((keyword === null || keyword === void 0 ? void 0 : keyword.value) === '') {
33
+ keyword.value = subtag;
34
+ }
35
+ else {
36
+ invariant(keyword !== undefined, 'Expected keyword to be defined');
37
+ keyword.value += '-' + subtag;
38
+ }
39
+ k += len + 1;
40
+ }
41
+ return { attributes: attributes, keywords: keywords };
42
+ }
@@ -44,4 +44,3 @@ export function UnicodeExtensionValue(extension, key) {
44
44
  }
45
45
  return undefined;
46
46
  }
47
- //# sourceMappingURL=UnicodeExtensionValue.js.map