Socket
Socket
Sign inDemoInstall

@formatjs/intl-localematcher

Package Overview
Dependencies
Maintainers
3
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@formatjs/intl-localematcher - npm Package Compare versions

Comparing version 0.5.0 to 0.5.1

21

abstract/BestFitMatcher.js

@@ -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": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc