@formatjs/intl-localematcher
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -14,11 +14,14 @@ "use strict"; | ||
var extension; | ||
for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) { | ||
var l = requestedLocales_1[_i]; | ||
var matchedLocale = (0, utils_1.findBestMatch)(l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''), availableLocales); | ||
if (matchedLocale) { | ||
foundLocale = matchedLocale; | ||
var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''); | ||
extension = l.slice(noExtensionLocale.length, l.length) || undefined; | ||
break; | ||
} | ||
var noExtensionLocales = []; | ||
var noExtensionLocaleMap = requestedLocales.reduce(function (all, l) { | ||
var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''); | ||
noExtensionLocales.push(noExtensionLocale); | ||
all[noExtensionLocale] = l; | ||
return all; | ||
}, {}); | ||
var result = (0, utils_1.findBestMatch)(noExtensionLocales, availableLocales); | ||
if (result.matchedSupportedLocale && result.matchedDesiredLocale) { | ||
foundLocale = result.matchedSupportedLocale; | ||
extension = | ||
noExtensionLocaleMap[result.matchedDesiredLocale].slice(result.matchedDesiredLocale.length) || undefined; | ||
} | ||
@@ -25,0 +28,0 @@ if (!foundLocale) { |
export declare const UNICODE_EXTENSION_SEQUENCE_REGEX: RegExp; | ||
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition; | ||
export declare function findMatchingDistance(desired: string, supported: string): number; | ||
export declare function findBestMatch(desired: string, supportedLocales: readonly string[], threshold?: number): string | undefined; | ||
interface LocaleMatchingResult { | ||
distances: Record<string, Record<string, number>>; | ||
matchedSupportedLocale?: string; | ||
matchedDesiredLocale?: string; | ||
} | ||
export declare function findBestMatch(requestedLocales: readonly string[], supportedLocales: readonly string[], threshold?: number): LocaleMatchingResult; | ||
export {}; |
@@ -141,19 +141,31 @@ "use strict"; | ||
exports.findMatchingDistance = findMatchingDistance; | ||
function findBestMatch(desired, supportedLocales, threshold) { | ||
function findBestMatch(requestedLocales, supportedLocales, threshold) { | ||
if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; } | ||
var bestMatch = undefined; | ||
var lowestDistance = Infinity; | ||
supportedLocales.forEach(function (supported, i) { | ||
// Add some weight to the distance based on the order of the supported locales | ||
var distance = findMatchingDistance(desired, supported) + i; | ||
if (distance < lowestDistance) { | ||
lowestDistance = distance; | ||
bestMatch = supported; | ||
var result = { | ||
matchedDesiredLocale: '', | ||
distances: {}, | ||
}; | ||
requestedLocales.forEach(function (desired, i) { | ||
if (!result.distances[desired]) { | ||
result.distances[desired] = {}; | ||
} | ||
supportedLocales.forEach(function (supported, j) { | ||
// Add some weight to the distance based on the order of the supported locales | ||
// Add penalty for the order of the requested locales | ||
var distance = findMatchingDistance(desired, supported) + j + i * 40; | ||
result.distances[desired][supported] = distance; | ||
if (distance < lowestDistance) { | ||
lowestDistance = distance; | ||
result.matchedDesiredLocale = desired; | ||
result.matchedSupportedLocale = supported; | ||
} | ||
}); | ||
}); | ||
if (lowestDistance >= threshold) { | ||
return; | ||
result.matchedDesiredLocale = undefined; | ||
result.matchedSupportedLocale = undefined; | ||
} | ||
return bestMatch; | ||
return result; | ||
} | ||
exports.findBestMatch = findBestMatch; |
@@ -14,11 +14,14 @@ "use strict"; | ||
var extension; | ||
for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) { | ||
var l = requestedLocales_1[_i]; | ||
var matchedLocale = (0, utils_1.findBestMatch)(l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''), availableLocales); | ||
if (matchedLocale) { | ||
foundLocale = matchedLocale; | ||
var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''); | ||
extension = l.slice(noExtensionLocale.length, l.length) || undefined; | ||
break; | ||
} | ||
var noExtensionLocales = []; | ||
var noExtensionLocaleMap = requestedLocales.reduce(function (all, l) { | ||
var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, ''); | ||
noExtensionLocales.push(noExtensionLocale); | ||
all[noExtensionLocale] = l; | ||
return all; | ||
}, {}); | ||
var result = (0, utils_1.findBestMatch)(noExtensionLocales, availableLocales); | ||
if (result.matchedSupportedLocale && result.matchedDesiredLocale) { | ||
foundLocale = result.matchedSupportedLocale; | ||
extension = | ||
noExtensionLocaleMap[result.matchedDesiredLocale].slice(result.matchedDesiredLocale.length) || undefined; | ||
} | ||
@@ -25,0 +28,0 @@ if (!foundLocale) { |
export declare const UNICODE_EXTENSION_SEQUENCE_REGEX: RegExp; | ||
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition; | ||
export declare function findMatchingDistance(desired: string, supported: string): number; | ||
export declare function findBestMatch(desired: string, supportedLocales: readonly string[], threshold?: number): string | undefined; | ||
interface LocaleMatchingResult { | ||
distances: Record<string, Record<string, number>>; | ||
matchedSupportedLocale?: string; | ||
matchedDesiredLocale?: string; | ||
} | ||
export declare function findBestMatch(requestedLocales: readonly string[], supportedLocales: readonly string[], threshold?: number): LocaleMatchingResult; | ||
export {}; |
@@ -141,19 +141,31 @@ "use strict"; | ||
exports.findMatchingDistance = findMatchingDistance; | ||
function findBestMatch(desired, supportedLocales, threshold) { | ||
function findBestMatch(requestedLocales, supportedLocales, threshold) { | ||
if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; } | ||
var bestMatch = undefined; | ||
var lowestDistance = Infinity; | ||
supportedLocales.forEach(function (supported, i) { | ||
// Add some weight to the distance based on the order of the supported locales | ||
var distance = findMatchingDistance(desired, supported) + i; | ||
if (distance < lowestDistance) { | ||
lowestDistance = distance; | ||
bestMatch = supported; | ||
var result = { | ||
matchedDesiredLocale: '', | ||
distances: {}, | ||
}; | ||
requestedLocales.forEach(function (desired, i) { | ||
if (!result.distances[desired]) { | ||
result.distances[desired] = {}; | ||
} | ||
supportedLocales.forEach(function (supported, j) { | ||
// Add some weight to the distance based on the order of the supported locales | ||
// Add penalty for the order of the requested locales | ||
var distance = findMatchingDistance(desired, supported) + j + i * 40; | ||
result.distances[desired][supported] = distance; | ||
if (distance < lowestDistance) { | ||
lowestDistance = distance; | ||
result.matchedDesiredLocale = desired; | ||
result.matchedSupportedLocale = supported; | ||
} | ||
}); | ||
}); | ||
if (lowestDistance >= threshold) { | ||
return; | ||
result.matchedDesiredLocale = undefined; | ||
result.matchedSupportedLocale = undefined; | ||
} | ||
return bestMatch; | ||
return result; | ||
} | ||
exports.findBestMatch = findBestMatch; |
{ | ||
"name": "@formatjs/intl-localematcher", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Intl.LocaleMatcher ponyfill", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
805619
24896
0