@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.
- package/abstract/BestFitMatcher.js +12 -9
- package/abstract/utils.d.ts +7 -1
- package/abstract/utils.js +22 -10
- package/lib/abstract/BestFitMatcher.js +12 -9
- package/lib/abstract/utils.d.ts +7 -1
- package/lib/abstract/utils.js +22 -10
- package/package.json +1 -1
|
@@ -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
|
-
|
|
15
|
-
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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() };
|
package/abstract/utils.d.ts
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
165
|
+
result.matchedDesiredLocale = undefined;
|
|
166
|
+
result.matchedSupportedLocale = undefined;
|
|
155
167
|
}
|
|
156
|
-
return
|
|
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
|
-
|
|
15
|
-
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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() };
|
package/lib/abstract/utils.d.ts
CHANGED
|
@@ -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
|
-
|
|
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/lib/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(
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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
|
-
|
|
165
|
+
result.matchedDesiredLocale = undefined;
|
|
166
|
+
result.matchedSupportedLocale = undefined;
|
|
155
167
|
}
|
|
156
|
-
return
|
|
168
|
+
return result;
|
|
157
169
|
}
|
|
158
170
|
exports.findBestMatch = findBestMatch;
|