@formatjs/intl-localematcher 0.4.2 → 0.5.1

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 (58) hide show
  1. package/abstract/BestAvailableLocale.d.ts +1 -1
  2. package/abstract/BestAvailableLocale.js +1 -1
  3. package/abstract/BestFitMatcher.d.ts +1 -1
  4. package/abstract/BestFitMatcher.js +12 -47
  5. package/abstract/LookupMatcher.d.ts +1 -1
  6. package/abstract/LookupMatcher.js +1 -1
  7. package/abstract/LookupSupportedLocales.d.ts +1 -1
  8. package/abstract/LookupSupportedLocales.js +1 -1
  9. package/abstract/ResolveLocale.d.ts +1 -1
  10. package/abstract/ResolveLocale.js +4 -4
  11. package/abstract/languageMatching.d.ts +7974 -0
  12. package/abstract/languageMatching.js +2623 -0
  13. package/abstract/regions.generated.d.ts +1 -0
  14. package/abstract/regions.generated.js +1337 -0
  15. package/abstract/utils.d.ts +8 -0
  16. package/abstract/utils.js +160 -1
  17. package/index.js +1 -5
  18. package/lib/abstract/BestAvailableLocale.d.ts +1 -1
  19. package/lib/abstract/BestAvailableLocale.js +6 -2
  20. package/lib/abstract/BestFitMatcher.d.ts +1 -1
  21. package/lib/abstract/BestFitMatcher.js +19 -50
  22. package/lib/abstract/CanonicalizeLocaleList.js +5 -1
  23. package/lib/abstract/LookupMatcher.d.ts +1 -1
  24. package/lib/abstract/LookupMatcher.js +9 -5
  25. package/lib/abstract/LookupSupportedLocales.d.ts +1 -1
  26. package/lib/abstract/LookupSupportedLocales.js +9 -5
  27. package/lib/abstract/ResolveLocale.d.ts +1 -1
  28. package/lib/abstract/ResolveLocale.js +17 -13
  29. package/lib/abstract/UnicodeExtensionValue.js +7 -3
  30. package/lib/abstract/languageMatching.d.ts +7974 -0
  31. package/lib/abstract/languageMatching.js +2623 -0
  32. package/lib/abstract/regions.generated.d.ts +1 -0
  33. package/lib/abstract/regions.generated.js +1337 -0
  34. package/lib/abstract/types.js +2 -1
  35. package/lib/abstract/utils.d.ts +8 -0
  36. package/lib/abstract/utils.js +165 -2
  37. package/lib/index.js +12 -10
  38. package/package.json +1 -1
  39. package/abstract/BestAvailableLocale.d.ts.map +0 -1
  40. package/abstract/BestFitMatcher.d.ts.map +0 -1
  41. package/abstract/CanonicalizeLocaleList.d.ts.map +0 -1
  42. package/abstract/LookupMatcher.d.ts.map +0 -1
  43. package/abstract/LookupSupportedLocales.d.ts.map +0 -1
  44. package/abstract/ResolveLocale.d.ts.map +0 -1
  45. package/abstract/UnicodeExtensionValue.d.ts.map +0 -1
  46. package/abstract/types.d.ts.map +0 -1
  47. package/abstract/utils.d.ts.map +0 -1
  48. package/index.d.ts.map +0 -1
  49. package/lib/abstract/BestAvailableLocale.d.ts.map +0 -1
  50. package/lib/abstract/BestFitMatcher.d.ts.map +0 -1
  51. package/lib/abstract/CanonicalizeLocaleList.d.ts.map +0 -1
  52. package/lib/abstract/LookupMatcher.d.ts.map +0 -1
  53. package/lib/abstract/LookupSupportedLocales.d.ts.map +0 -1
  54. package/lib/abstract/ResolveLocale.d.ts.map +0 -1
  55. package/lib/abstract/UnicodeExtensionValue.d.ts.map +0 -1
  56. package/lib/abstract/types.d.ts.map +0 -1
  57. package/lib/abstract/utils.d.ts.map +0 -1
  58. package/lib/index.d.ts.map +0 -1
@@ -1,2 +1,10 @@
1
1
  export declare const UNICODE_EXTENSION_SEQUENCE_REGEX: RegExp;
2
2
  export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
3
+ export declare function findMatchingDistance(desired: string, supported: string): number;
4
+ interface LocaleMatchingResult {
5
+ distances: Record<string, Record<string, number>>;
6
+ matchedSupportedLocale?: string;
7
+ matchedDesiredLocale?: string;
8
+ }
9
+ export declare function findBestMatch(requestedLocales: readonly string[], supportedLocales: readonly string[], threshold?: number): LocaleMatchingResult;
10
+ export {};
package/abstract/utils.js CHANGED
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.invariant = exports.UNICODE_EXTENSION_SEQUENCE_REGEX = void 0;
3
+ exports.findBestMatch = exports.findMatchingDistance = exports.invariant = exports.UNICODE_EXTENSION_SEQUENCE_REGEX = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var languageMatching_1 = require("./languageMatching");
6
+ var regions_generated_1 = require("./regions.generated");
4
7
  exports.UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi;
5
8
  function invariant(condition, message, Err) {
6
9
  if (Err === void 0) { Err = Error; }
@@ -9,3 +12,159 @@ function invariant(condition, message, Err) {
9
12
  }
10
13
  }
11
14
  exports.invariant = invariant;
15
+ // This is effectively 2 languages in 2 different regions not even in the same cluster
16
+ var DEFAULT_MATCHING_THRESHOLD = 840;
17
+ var PROCESSED_DATA;
18
+ function processData() {
19
+ var _a, _b;
20
+ if (!PROCESSED_DATA) {
21
+ var paradigmLocales = (_b = (_a = languageMatching_1.data.supplemental.languageMatching['written-new'][0]) === null || _a === void 0 ? void 0 : _a.paradigmLocales) === null || _b === void 0 ? void 0 : _b._locales.split(' ');
22
+ var matchVariables = languageMatching_1.data.supplemental.languageMatching['written-new'].slice(1, 5);
23
+ var data = languageMatching_1.data.supplemental.languageMatching['written-new'].slice(5);
24
+ var matches = data.map(function (d) {
25
+ var key = Object.keys(d)[0];
26
+ var value = d[key];
27
+ return {
28
+ supported: key,
29
+ desired: value._desired,
30
+ distance: +value._distance,
31
+ oneway: value.oneway === 'true' ? true : false,
32
+ };
33
+ }, {});
34
+ PROCESSED_DATA = {
35
+ matches: matches,
36
+ matchVariables: matchVariables.reduce(function (all, d) {
37
+ var key = Object.keys(d)[0];
38
+ var value = d[key];
39
+ all[key.slice(1)] = value._value.split('+');
40
+ return all;
41
+ }, {}),
42
+ paradigmLocales: tslib_1.__spreadArray(tslib_1.__spreadArray([], paradigmLocales, true), paradigmLocales.map(function (l) {
43
+ return new Intl.Locale(l.replace(/_/g, '-')).maximize().toString();
44
+ }), true),
45
+ };
46
+ }
47
+ return PROCESSED_DATA;
48
+ }
49
+ function isMatched(locale, languageMatchInfoLocale, matchVariables) {
50
+ var _a = languageMatchInfoLocale.split('-'), language = _a[0], script = _a[1], region = _a[2];
51
+ var matches = true;
52
+ if (region && region[0] === '$') {
53
+ var shouldInclude = region[1] !== '!';
54
+ var matchRegions = shouldInclude
55
+ ? matchVariables[region.slice(1)]
56
+ : matchVariables[region.slice(2)];
57
+ var expandedMatchedRegions = matchRegions
58
+ .map(function (r) { return regions_generated_1.regions[r] || [r]; })
59
+ .reduce(function (all, list) { return tslib_1.__spreadArray(tslib_1.__spreadArray([], all, true), list, true); }, []);
60
+ matches && (matches = !(expandedMatchedRegions.indexOf(locale.region || '') > 1 !=
61
+ shouldInclude));
62
+ }
63
+ else {
64
+ matches && (matches = locale.region
65
+ ? region === '*' || region === locale.region
66
+ : true);
67
+ }
68
+ matches && (matches = locale.script ? script === '*' || script === locale.script : true);
69
+ matches && (matches = locale.language
70
+ ? language === '*' || language === locale.language
71
+ : true);
72
+ return matches;
73
+ }
74
+ function serializeLSR(lsr) {
75
+ return [lsr.language, lsr.script, lsr.region].filter(Boolean).join('-');
76
+ }
77
+ function findMatchingDistanceForLSR(desired, supported, data) {
78
+ for (var _i = 0, _a = data.matches; _i < _a.length; _i++) {
79
+ var d = _a[_i];
80
+ var matches = isMatched(desired, d.desired, data.matchVariables) &&
81
+ isMatched(supported, d.supported, data.matchVariables);
82
+ if (!d.oneway && !matches) {
83
+ matches =
84
+ isMatched(desired, d.supported, data.matchVariables) &&
85
+ isMatched(supported, d.desired, data.matchVariables);
86
+ }
87
+ if (matches) {
88
+ var distance = d.distance * 10;
89
+ if (data.paradigmLocales.indexOf(serializeLSR(desired)) > -1 !=
90
+ data.paradigmLocales.indexOf(serializeLSR(supported)) > -1) {
91
+ return distance - 1;
92
+ }
93
+ return distance;
94
+ }
95
+ }
96
+ throw new Error('No matching distance found');
97
+ }
98
+ function findMatchingDistance(desired, supported) {
99
+ var desiredLocale = new Intl.Locale(desired).maximize();
100
+ var supportedLocale = new Intl.Locale(supported).maximize();
101
+ var desiredLSR = {
102
+ language: desiredLocale.language,
103
+ script: desiredLocale.script || '',
104
+ region: desiredLocale.region || '',
105
+ };
106
+ var supportedLSR = {
107
+ language: supportedLocale.language,
108
+ script: supportedLocale.script || '',
109
+ region: supportedLocale.region || '',
110
+ };
111
+ var matchingDistance = 0;
112
+ var data = processData();
113
+ if (desiredLSR.language !== supportedLSR.language) {
114
+ matchingDistance += findMatchingDistanceForLSR({
115
+ language: desiredLocale.language,
116
+ script: '',
117
+ region: '',
118
+ }, {
119
+ language: supportedLocale.language,
120
+ script: '',
121
+ region: '',
122
+ }, data);
123
+ }
124
+ if (desiredLSR.script !== supportedLSR.script) {
125
+ matchingDistance += findMatchingDistanceForLSR({
126
+ language: desiredLocale.language,
127
+ script: desiredLSR.script,
128
+ region: '',
129
+ }, {
130
+ language: supportedLocale.language,
131
+ script: desiredLSR.script,
132
+ region: '',
133
+ }, data);
134
+ }
135
+ if (desiredLSR.region !== supportedLSR.region) {
136
+ matchingDistance += findMatchingDistanceForLSR(desiredLSR, supportedLSR, data);
137
+ }
138
+ return matchingDistance;
139
+ }
140
+ exports.findMatchingDistance = findMatchingDistance;
141
+ function findBestMatch(requestedLocales, supportedLocales, threshold) {
142
+ if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; }
143
+ var lowestDistance = Infinity;
144
+ var result = {
145
+ matchedDesiredLocale: '',
146
+ distances: {},
147
+ };
148
+ requestedLocales.forEach(function (desired, i) {
149
+ if (!result.distances[desired]) {
150
+ result.distances[desired] = {};
151
+ }
152
+ supportedLocales.forEach(function (supported, j) {
153
+ // Add some weight to the distance based on the order of the supported locales
154
+ // Add penalty for the order of the requested locales
155
+ var distance = findMatchingDistance(desired, supported) + j + i * 40;
156
+ result.distances[desired][supported] = distance;
157
+ if (distance < lowestDistance) {
158
+ lowestDistance = distance;
159
+ result.matchedDesiredLocale = desired;
160
+ result.matchedSupportedLocale = supported;
161
+ }
162
+ });
163
+ });
164
+ if (lowestDistance >= threshold) {
165
+ result.matchedDesiredLocale = undefined;
166
+ result.matchedSupportedLocale = undefined;
167
+ }
168
+ return result;
169
+ }
170
+ exports.findBestMatch = findBestMatch;
package/index.js CHANGED
@@ -4,11 +4,7 @@ exports.ResolveLocale = exports.LookupSupportedLocales = exports.match = void 0;
4
4
  var CanonicalizeLocaleList_1 = require("./abstract/CanonicalizeLocaleList");
5
5
  var ResolveLocale_1 = require("./abstract/ResolveLocale");
6
6
  function match(requestedLocales, availableLocales, defaultLocale, opts) {
7
- var locales = availableLocales.reduce(function (all, l) {
8
- all.add(l);
9
- return all;
10
- }, new Set());
11
- return (0, ResolveLocale_1.ResolveLocale)(locales, (0, CanonicalizeLocaleList_1.CanonicalizeLocaleList)(requestedLocales), {
7
+ return (0, ResolveLocale_1.ResolveLocale)(availableLocales, (0, CanonicalizeLocaleList_1.CanonicalizeLocaleList)(requestedLocales), {
12
8
  localeMatcher: (opts === null || opts === void 0 ? void 0 : opts.algorithm) || 'best fit',
13
9
  }, [], {}, function () { return defaultLocale; }).locale;
14
10
  }
@@ -3,4 +3,4 @@
3
3
  * @param availableLocales
4
4
  * @param locale
5
5
  */
6
- export declare function BestAvailableLocale(availableLocales: Set<String>, locale: string): string | undefined;
6
+ export declare function BestAvailableLocale(availableLocales: readonly string[], locale: string): string | undefined;
@@ -1,12 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BestAvailableLocale = void 0;
1
4
  /**
2
5
  * https://tc39.es/ecma402/#sec-bestavailablelocale
3
6
  * @param availableLocales
4
7
  * @param locale
5
8
  */
6
- export function BestAvailableLocale(availableLocales, locale) {
9
+ function BestAvailableLocale(availableLocales, locale) {
7
10
  var candidate = locale;
8
11
  while (true) {
9
- if (availableLocales.has(candidate)) {
12
+ if (availableLocales.indexOf(candidate) > -1) {
10
13
  return candidate;
11
14
  }
12
15
  var pos = candidate.lastIndexOf('-');
@@ -19,3 +22,4 @@ export function BestAvailableLocale(availableLocales, locale) {
19
22
  candidate = candidate.slice(0, pos);
20
23
  }
21
24
  }
25
+ exports.BestAvailableLocale = BestAvailableLocale;
@@ -5,4 +5,4 @@ import { LookupMatcherResult } from './types';
5
5
  * @param requestedLocales
6
6
  * @param getDefaultLocale
7
7
  */
8
- export declare function BestFitMatcher(availableLocales: Set<string>, requestedLocales: string[], getDefaultLocale: () => string): LookupMatcherResult;
8
+ export declare function BestFitMatcher(availableLocales: readonly string[], requestedLocales: readonly string[], getDefaultLocale: () => string): LookupMatcherResult;
@@ -1,66 +1,35 @@
1
- import { BestAvailableLocale } from './BestAvailableLocale';
2
- import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BestFitMatcher = void 0;
4
+ var utils_1 = require("./utils");
3
5
  /**
4
6
  * https://tc39.es/ecma402/#sec-bestfitmatcher
5
7
  * @param availableLocales
6
8
  * @param requestedLocales
7
9
  * @param getDefaultLocale
8
10
  */
9
- export function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
10
- var minimizedAvailableLocaleMap = {};
11
- var availableLocaleMap = {};
12
- var canonicalizedLocaleMap = {};
13
- var minimizedAvailableLocales = new Set();
14
- availableLocales.forEach(function (locale) {
15
- var minimizedLocale = new Intl.Locale(locale)
16
- .minimize()
17
- .toString();
18
- var canonicalizedLocale = Intl.getCanonicalLocales(locale)[0] || locale;
19
- minimizedAvailableLocaleMap[minimizedLocale] = locale;
20
- availableLocaleMap[locale] = locale;
21
- canonicalizedLocaleMap[canonicalizedLocale] = locale;
22
- minimizedAvailableLocales.add(minimizedLocale);
23
- minimizedAvailableLocales.add(locale);
24
- minimizedAvailableLocales.add(canonicalizedLocale);
25
- });
11
+ function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
26
12
  var foundLocale;
27
13
  var extension;
28
- for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
29
- var l = requestedLocales_1[_i];
30
- if (foundLocale) {
31
- break;
32
- }
33
- var noExtensionLocale = l.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
34
- if (l !== noExtensionLocale) {
35
- extension = l.slice(noExtensionLocale.length, l.length);
36
- }
37
- if (availableLocales.has(noExtensionLocale)) {
38
- foundLocale = noExtensionLocale;
39
- break;
40
- }
41
- if (minimizedAvailableLocales.has(noExtensionLocale)) {
42
- foundLocale = noExtensionLocale;
43
- break;
44
- }
45
- var locale = new Intl.Locale(noExtensionLocale);
46
- var maximizedRequestedLocale = locale.maximize().toString();
47
- var minimizedRequestedLocale = locale.minimize().toString();
48
- // Check minimized locale
49
- if (minimizedAvailableLocales.has(minimizedRequestedLocale)) {
50
- foundLocale = minimizedRequestedLocale;
51
- break;
52
- }
53
- // Lookup algo on maximized locale
54
- foundLocale = BestAvailableLocale(minimizedAvailableLocales, maximizedRequestedLocale);
14
+ var noExtensionLocales = [];
15
+ var noExtensionLocaleMap = requestedLocales.reduce(function (all, l) {
16
+ var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
17
+ noExtensionLocales.push(noExtensionLocale);
18
+ all[noExtensionLocale] = l;
19
+ return all;
20
+ }, {});
21
+ var result = (0, utils_1.findBestMatch)(noExtensionLocales, availableLocales);
22
+ if (result.matchedSupportedLocale && result.matchedDesiredLocale) {
23
+ foundLocale = result.matchedSupportedLocale;
24
+ extension =
25
+ noExtensionLocaleMap[result.matchedDesiredLocale].slice(result.matchedDesiredLocale.length) || undefined;
55
26
  }
56
27
  if (!foundLocale) {
57
28
  return { locale: getDefaultLocale() };
58
29
  }
59
30
  return {
60
- locale: availableLocaleMap[foundLocale] ||
61
- canonicalizedLocaleMap[foundLocale] ||
62
- minimizedAvailableLocaleMap[foundLocale] ||
63
- foundLocale,
31
+ locale: foundLocale,
64
32
  extension: extension,
65
33
  };
66
34
  }
35
+ exports.BestFitMatcher = BestFitMatcher;
@@ -1,8 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CanonicalizeLocaleList = void 0;
1
4
  /**
2
5
  * http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist
3
6
  * @param locales
4
7
  */
5
- export function CanonicalizeLocaleList(locales) {
8
+ function CanonicalizeLocaleList(locales) {
6
9
  // TODO
7
10
  return Intl.getCanonicalLocales(locales);
8
11
  }
12
+ exports.CanonicalizeLocaleList = CanonicalizeLocaleList;
@@ -5,4 +5,4 @@ import { LookupMatcherResult } from './types';
5
5
  * @param requestedLocales
6
6
  * @param getDefaultLocale
7
7
  */
8
- export declare function LookupMatcher(availableLocales: Set<string>, requestedLocales: string[], getDefaultLocale: () => string): LookupMatcherResult;
8
+ export declare function LookupMatcher(availableLocales: readonly string[], requestedLocales: readonly string[], getDefaultLocale: () => string): LookupMatcherResult;
@@ -1,17 +1,20 @@
1
- import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils';
2
- import { BestAvailableLocale } from './BestAvailableLocale';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LookupMatcher = void 0;
4
+ var BestAvailableLocale_1 = require("./BestAvailableLocale");
5
+ var utils_1 = require("./utils");
3
6
  /**
4
7
  * https://tc39.es/ecma402/#sec-lookupmatcher
5
8
  * @param availableLocales
6
9
  * @param requestedLocales
7
10
  * @param getDefaultLocale
8
11
  */
9
- export function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
12
+ function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
10
13
  var result = { locale: '' };
11
14
  for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
12
15
  var locale = requestedLocales_1[_i];
13
- var noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
14
- var availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
16
+ var noExtensionLocale = locale.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
17
+ var availableLocale = (0, BestAvailableLocale_1.BestAvailableLocale)(availableLocales, noExtensionLocale);
15
18
  if (availableLocale) {
16
19
  result.locale = availableLocale;
17
20
  if (locale !== noExtensionLocale) {
@@ -23,3 +26,4 @@ export function LookupMatcher(availableLocales, requestedLocales, getDefaultLoca
23
26
  result.locale = getDefaultLocale();
24
27
  return result;
25
28
  }
29
+ exports.LookupMatcher = LookupMatcher;
@@ -3,4 +3,4 @@
3
3
  * @param availableLocales
4
4
  * @param requestedLocales
5
5
  */
6
- export declare function LookupSupportedLocales(availableLocales: Set<string>, requestedLocales: string[]): string[];
6
+ export declare function LookupSupportedLocales(availableLocales: string[], requestedLocales: string[]): string[];
@@ -1,19 +1,23 @@
1
- import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils';
2
- import { BestAvailableLocale } from './BestAvailableLocale';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LookupSupportedLocales = void 0;
4
+ var BestAvailableLocale_1 = require("./BestAvailableLocale");
5
+ var utils_1 = require("./utils");
3
6
  /**
4
7
  * https://tc39.es/ecma402/#sec-lookupsupportedlocales
5
8
  * @param availableLocales
6
9
  * @param requestedLocales
7
10
  */
8
- export function LookupSupportedLocales(availableLocales, requestedLocales) {
11
+ function LookupSupportedLocales(availableLocales, requestedLocales) {
9
12
  var subset = [];
10
13
  for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
11
14
  var locale = requestedLocales_1[_i];
12
- var noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
13
- var availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
15
+ var noExtensionLocale = locale.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
16
+ var availableLocale = (0, BestAvailableLocale_1.BestAvailableLocale)(availableLocales, noExtensionLocale);
14
17
  if (availableLocale) {
15
18
  subset.push(availableLocale);
16
19
  }
17
20
  }
18
21
  return subset;
19
22
  }
23
+ exports.LookupSupportedLocales = LookupSupportedLocales;
@@ -8,7 +8,7 @@ export interface ResolveLocaleResult {
8
8
  */
9
9
  export declare function ResolveLocale<K extends string, D extends {
10
10
  [k in K]: any;
11
- }>(availableLocales: Set<string>, requestedLocales: string[], options: {
11
+ }>(availableLocales: Set<string> | readonly string[], requestedLocales: readonly string[], options: {
12
12
  localeMatcher: string;
13
13
  [k: string]: string;
14
14
  }, relevantExtensionKeys: K[], localeData: Record<string, D | undefined>, getDefaultLocale: () => string): ResolveLocaleResult;
@@ -1,34 +1,37 @@
1
- import { LookupMatcher } from './LookupMatcher';
2
- import { BestFitMatcher } from './BestFitMatcher';
3
- import { invariant } from './utils';
4
- import { UnicodeExtensionValue } from './UnicodeExtensionValue';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResolveLocale = void 0;
4
+ var BestFitMatcher_1 = require("./BestFitMatcher");
5
+ var LookupMatcher_1 = require("./LookupMatcher");
6
+ var UnicodeExtensionValue_1 = require("./UnicodeExtensionValue");
7
+ var utils_1 = require("./utils");
5
8
  /**
6
9
  * https://tc39.es/ecma402/#sec-resolvelocale
7
10
  */
8
- export function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
11
+ function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
9
12
  var matcher = options.localeMatcher;
10
13
  var r;
11
14
  if (matcher === 'lookup') {
12
- r = LookupMatcher(availableLocales, requestedLocales, getDefaultLocale);
15
+ r = (0, LookupMatcher_1.LookupMatcher)(Array.from(availableLocales), requestedLocales, getDefaultLocale);
13
16
  }
14
17
  else {
15
- r = BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale);
18
+ r = (0, BestFitMatcher_1.BestFitMatcher)(Array.from(availableLocales), requestedLocales, getDefaultLocale);
16
19
  }
17
20
  var foundLocale = r.locale;
18
21
  var result = { locale: '', dataLocale: foundLocale };
19
22
  var supportedExtension = '-u';
20
23
  for (var _i = 0, relevantExtensionKeys_1 = relevantExtensionKeys; _i < relevantExtensionKeys_1.length; _i++) {
21
24
  var key = relevantExtensionKeys_1[_i];
22
- invariant(foundLocale in localeData, "Missing locale data for ".concat(foundLocale));
25
+ (0, utils_1.invariant)(foundLocale in localeData, "Missing locale data for ".concat(foundLocale));
23
26
  var foundLocaleData = localeData[foundLocale];
24
- invariant(typeof foundLocaleData === 'object' && foundLocaleData !== null, "locale data ".concat(key, " must be an object"));
27
+ (0, utils_1.invariant)(typeof foundLocaleData === 'object' && foundLocaleData !== null, "locale data ".concat(key, " must be an object"));
25
28
  var keyLocaleData = foundLocaleData[key];
26
- invariant(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
29
+ (0, utils_1.invariant)(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
27
30
  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));
31
+ (0, utils_1.invariant)(typeof value === 'string' || value === null, "value must be string or null but got ".concat(typeof value, " in key ").concat(key));
29
32
  var supportedExtensionAddition = '';
30
33
  if (r.extension) {
31
- var requestedValue = UnicodeExtensionValue(r.extension, key);
34
+ var requestedValue = (0, UnicodeExtensionValue_1.UnicodeExtensionValue)(r.extension, key);
32
35
  if (requestedValue !== undefined) {
33
36
  if (requestedValue !== '') {
34
37
  if (~keyLocaleData.indexOf(requestedValue)) {
@@ -44,7 +47,7 @@ export function ResolveLocale(availableLocales, requestedLocales, options, relev
44
47
  }
45
48
  if (key in options) {
46
49
  var optionsValue = options[key];
47
- invariant(typeof optionsValue === 'string' ||
50
+ (0, utils_1.invariant)(typeof optionsValue === 'string' ||
48
51
  typeof optionsValue === 'undefined' ||
49
52
  optionsValue === null, 'optionsValue must be String, Undefined or Null');
50
53
  if (~keyLocaleData.indexOf(optionsValue)) {
@@ -72,3 +75,4 @@ export function ResolveLocale(availableLocales, requestedLocales, options, relev
72
75
  result.locale = foundLocale;
73
76
  return result;
74
77
  }
78
+ exports.ResolveLocale = ResolveLocale;
@@ -1,11 +1,14 @@
1
- import { invariant } from './utils';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UnicodeExtensionValue = void 0;
4
+ var utils_1 = require("./utils");
2
5
  /**
3
6
  * https://tc39.es/ecma402/#sec-unicodeextensionvalue
4
7
  * @param extension
5
8
  * @param key
6
9
  */
7
- export function UnicodeExtensionValue(extension, key) {
8
- invariant(key.length === 2, 'key must have 2 elements');
10
+ function UnicodeExtensionValue(extension, key) {
11
+ (0, utils_1.invariant)(key.length === 2, 'key must have 2 elements');
9
12
  var size = extension.length;
10
13
  var searchValue = "-".concat(key, "-");
11
14
  var pos = extension.indexOf(searchValue);
@@ -44,3 +47,4 @@ export function UnicodeExtensionValue(extension, key) {
44
47
  }
45
48
  return undefined;
46
49
  }
50
+ exports.UnicodeExtensionValue = UnicodeExtensionValue;