@formatjs/intl-localematcher 0.5.0 → 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.
@@ -11,15 +11,18 @@ var utils_1 = require("./utils");
11
11
  function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
12
12
  var foundLocale;
13
13
  var extension;
14
- for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
15
- var l = requestedLocales_1[_i];
16
- var matchedLocale = (0, utils_1.findBestMatch)(l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''), availableLocales);
17
- if (matchedLocale) {
18
- foundLocale = matchedLocale;
19
- var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
20
- extension = l.slice(noExtensionLocale.length, l.length) || undefined;
21
- break;
22
- }
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;
23
26
  }
24
27
  if (!foundLocale) {
25
28
  return { locale: getDefaultLocale() };
@@ -1,4 +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
3
  export declare function findMatchingDistance(desired: string, supported: string): number;
4
- export declare function findBestMatch(desired: string, supportedLocales: readonly string[], threshold?: number): string | undefined;
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
@@ -138,21 +138,33 @@ function findMatchingDistance(desired, supported) {
138
138
  return matchingDistance;
139
139
  }
140
140
  exports.findMatchingDistance = findMatchingDistance;
141
- function findBestMatch(desired, supportedLocales, threshold) {
141
+ function findBestMatch(requestedLocales, supportedLocales, threshold) {
142
142
  if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; }
143
- var bestMatch = undefined;
144
143
  var lowestDistance = Infinity;
145
- supportedLocales.forEach(function (supported, i) {
146
- // Add some weight to the distance based on the order of the supported locales
147
- var distance = findMatchingDistance(desired, supported) + i;
148
- if (distance < lowestDistance) {
149
- lowestDistance = distance;
150
- bestMatch = supported;
144
+ var result = {
145
+ matchedDesiredLocale: '',
146
+ distances: {},
147
+ };
148
+ requestedLocales.forEach(function (desired, i) {
149
+ if (!result.distances[desired]) {
150
+ result.distances[desired] = {};
151
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
+ });
152
163
  });
153
164
  if (lowestDistance >= threshold) {
154
- return;
165
+ result.matchedDesiredLocale = undefined;
166
+ result.matchedSupportedLocale = undefined;
155
167
  }
156
- return bestMatch;
168
+ return result;
157
169
  }
158
170
  exports.findBestMatch = findBestMatch;
@@ -11,15 +11,18 @@ var utils_1 = require("./utils");
11
11
  function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
12
12
  var foundLocale;
13
13
  var extension;
14
- for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
15
- var l = requestedLocales_1[_i];
16
- var matchedLocale = (0, utils_1.findBestMatch)(l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''), availableLocales);
17
- if (matchedLocale) {
18
- foundLocale = matchedLocale;
19
- var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
20
- extension = l.slice(noExtensionLocale.length, l.length) || undefined;
21
- break;
22
- }
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;
23
26
  }
24
27
  if (!foundLocale) {
25
28
  return { locale: getDefaultLocale() };
@@ -1,4 +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
3
  export declare function findMatchingDistance(desired: string, supported: string): number;
4
- export declare function findBestMatch(desired: string, supportedLocales: readonly string[], threshold?: number): string | undefined;
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 {};
@@ -138,21 +138,33 @@ function findMatchingDistance(desired, supported) {
138
138
  return matchingDistance;
139
139
  }
140
140
  exports.findMatchingDistance = findMatchingDistance;
141
- function findBestMatch(desired, supportedLocales, threshold) {
141
+ function findBestMatch(requestedLocales, supportedLocales, threshold) {
142
142
  if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; }
143
- var bestMatch = undefined;
144
143
  var lowestDistance = Infinity;
145
- supportedLocales.forEach(function (supported, i) {
146
- // Add some weight to the distance based on the order of the supported locales
147
- var distance = findMatchingDistance(desired, supported) + i;
148
- if (distance < lowestDistance) {
149
- lowestDistance = distance;
150
- bestMatch = supported;
144
+ var result = {
145
+ matchedDesiredLocale: '',
146
+ distances: {},
147
+ };
148
+ requestedLocales.forEach(function (desired, i) {
149
+ if (!result.distances[desired]) {
150
+ result.distances[desired] = {};
151
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
+ });
152
163
  });
153
164
  if (lowestDistance >= threshold) {
154
- return;
165
+ result.matchedDesiredLocale = undefined;
166
+ result.matchedSupportedLocale = undefined;
155
167
  }
156
- return bestMatch;
168
+ return result;
157
169
  }
158
170
  exports.findBestMatch = findBestMatch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/intl-localematcher",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Intl.LocaleMatcher ponyfill",
5
5
  "keywords": [
6
6
  "intl",