You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@formatjs/intl-localematcher

Package Overview
Dependencies
Maintainers
3
Versions
60
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.6.2
to
0.7.0
+1
-4
abstract/BestAvailableLocale.js

@@ -1,4 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BestAvailableLocale = BestAvailableLocale;
/**

@@ -9,3 +6,3 @@ * https://tc39.es/ecma402/#sec-bestavailablelocale

*/
function BestAvailableLocale(availableLocales, locale) {
export function BestAvailableLocale(availableLocales, locale) {
var candidate = locale;

@@ -12,0 +9,0 @@ while (true) {

+1
-1

@@ -1,2 +0,2 @@

import { LookupMatcherResult } from './types';
import { LookupMatcherResult } from './types.js';
/**

@@ -3,0 +3,0 @@ * https://tc39.es/ecma402/#sec-bestfitmatcher

@@ -1,5 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BestFitMatcher = BestFitMatcher;
var utils_1 = require("./utils");
import { UNICODE_EXTENSION_SEQUENCE_REGEX, findBestMatch } from './utils.js';
/**

@@ -11,3 +8,3 @@ * https://tc39.es/ecma402/#sec-bestfitmatcher

*/
function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
export function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
var foundLocale;

@@ -17,3 +14,3 @@ var extension;

var noExtensionLocaleMap = requestedLocales.reduce(function (all, l) {
var noExtensionLocale = l.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var noExtensionLocale = l.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
noExtensionLocales.push(noExtensionLocale);

@@ -23,3 +20,3 @@ all[noExtensionLocale] = l;

}, {});
var result = (0, utils_1.findBestMatch)(noExtensionLocales, availableLocales);
var result = findBestMatch(noExtensionLocales, availableLocales);
if (result.matchedSupportedLocale && result.matchedDesiredLocale) {

@@ -26,0 +23,0 @@ foundLocale = result.matchedSupportedLocale;

@@ -1,4 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CanonicalizeLocaleList = CanonicalizeLocaleList;
/**

@@ -8,4 +5,4 @@ * http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist

*/
function CanonicalizeLocaleList(locales) {
export function CanonicalizeLocaleList(locales) {
return Intl.getCanonicalLocales(locales);
}

@@ -1,6 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CanonicalizeUnicodeLocaleId = CanonicalizeUnicodeLocaleId;
function CanonicalizeUnicodeLocaleId(locale) {
export function CanonicalizeUnicodeLocaleId(locale) {
return Intl.getCanonicalLocales(locale)[0];
}

@@ -1,11 +0,8 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CanonicalizeUValue = CanonicalizeUValue;
var utils_1 = require("./utils");
function CanonicalizeUValue(ukey, uvalue) {
import { invariant } from './utils.js';
export function CanonicalizeUValue(ukey, uvalue) {
// TODO: Implement algorithm for CanonicalizeUValue per https://tc39.es/ecma402/#sec-canonicalizeuvalue
var lowerValue = uvalue.toLowerCase();
(0, utils_1.invariant)(ukey !== undefined, "ukey must be defined");
invariant(ukey !== undefined, "ukey must be defined");
var canonicalized = lowerValue;
return canonicalized;
}

@@ -1,2 +0,2 @@

import { Keyword } from './types';
import { Keyword } from './types.js';
export declare function InsertUnicodeExtensionAndCanonicalize(locale: string, attributes: string[], keywords: Array<Keyword>): string;

@@ -1,8 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InsertUnicodeExtensionAndCanonicalize = InsertUnicodeExtensionAndCanonicalize;
var CanonicalizeUnicodeLocaleId_1 = require("./CanonicalizeUnicodeLocaleId");
var utils_1 = require("./utils");
function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
(0, utils_1.invariant)(locale.indexOf('-u-') === -1, 'Expected locale to not have a Unicode locale extension');
import { CanonicalizeUnicodeLocaleId } from './CanonicalizeUnicodeLocaleId.js';
import { invariant } from './utils.js';
export function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
invariant(locale.indexOf('-u-') === -1, 'Expected locale to not have a Unicode locale extension');
var extension = '-u';

@@ -22,3 +19,3 @@ for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) {

if (extension === '-u') {
return (0, CanonicalizeUnicodeLocaleId_1.CanonicalizeUnicodeLocaleId)(locale);
return CanonicalizeUnicodeLocaleId(locale);
}

@@ -35,3 +32,3 @@ var privateIndex = locale.indexOf('-x-');

}
return (0, CanonicalizeUnicodeLocaleId_1.CanonicalizeUnicodeLocaleId)(newLocale);
return CanonicalizeUnicodeLocaleId(newLocale);
}

@@ -1,2 +0,2 @@

import { LookupMatcherResult } from './types';
import { LookupMatcherResult } from './types.js';
/**

@@ -3,0 +3,0 @@ * https://tc39.es/ecma402/#sec-lookupmatcher

@@ -1,6 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LookupMatcher = LookupMatcher;
var BestAvailableLocale_1 = require("./BestAvailableLocale");
var utils_1 = require("./utils");
import { BestAvailableLocale } from './BestAvailableLocale.js';
import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils.js';
/**

@@ -12,8 +9,8 @@ * https://tc39.es/ecma402/#sec-lookupmatcher

*/
function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
export function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
var result = { locale: '' };
for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
var locale = requestedLocales_1[_i];
var noExtensionLocale = locale.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var availableLocale = (0, BestAvailableLocale_1.BestAvailableLocale)(availableLocales, noExtensionLocale);
var noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
if (availableLocale) {

@@ -20,0 +17,0 @@ result.locale = availableLocale;

@@ -1,6 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LookupSupportedLocales = LookupSupportedLocales;
var BestAvailableLocale_1 = require("./BestAvailableLocale");
var utils_1 = require("./utils");
import { BestAvailableLocale } from './BestAvailableLocale.js';
import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils.js';
/**

@@ -11,8 +8,8 @@ * https://tc39.es/ecma402/#sec-lookupsupportedlocales

*/
function LookupSupportedLocales(availableLocales, requestedLocales) {
export function LookupSupportedLocales(availableLocales, requestedLocales) {
var subset = [];
for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
var locale = requestedLocales_1[_i];
var noExtensionLocale = locale.replace(utils_1.UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var availableLocale = (0, BestAvailableLocale_1.BestAvailableLocale)(availableLocales, noExtensionLocale);
var noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
if (availableLocale) {

@@ -19,0 +16,0 @@ subset.push(availableLocale);

@@ -1,6 +0,3 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.regions = void 0;
// This file is generated from regions-gen.ts
exports.regions = {
export var regions = {
"001": [

@@ -7,0 +4,0 @@ "001",

@@ -1,14 +0,11 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResolveLocale = ResolveLocale;
var BestFitMatcher_1 = require("./BestFitMatcher");
var CanonicalizeUValue_1 = require("./CanonicalizeUValue");
var InsertUnicodeExtensionAndCanonicalize_1 = require("./InsertUnicodeExtensionAndCanonicalize");
var LookupMatcher_1 = require("./LookupMatcher");
var UnicodeExtensionComponents_1 = require("./UnicodeExtensionComponents");
var utils_1 = require("./utils");
import { BestFitMatcher } from './BestFitMatcher.js';
import { CanonicalizeUValue } from './CanonicalizeUValue.js';
import { InsertUnicodeExtensionAndCanonicalize } from './InsertUnicodeExtensionAndCanonicalize.js';
import { LookupMatcher } from './LookupMatcher.js';
import { UnicodeExtensionComponents } from './UnicodeExtensionComponents.js';
import { invariant } from './utils.js';
/**
* https://tc39.es/ecma402/#sec-resolvelocale
*/
function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
export function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
var _a;

@@ -18,6 +15,6 @@ var matcher = options.localeMatcher;

if (matcher === 'lookup') {
r = (0, LookupMatcher_1.LookupMatcher)(Array.from(availableLocales), requestedLocales, getDefaultLocale);
r = LookupMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
}
else {
r = (0, BestFitMatcher_1.BestFitMatcher)(Array.from(availableLocales), requestedLocales, getDefaultLocale);
r = BestFitMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
}

@@ -41,3 +38,3 @@ if (r == null) {

if (r.extension) {
components = (0, UnicodeExtensionComponents_1.UnicodeExtensionComponents)(r.extension);
components = UnicodeExtensionComponents(r.extension);
keywords = components.keywords;

@@ -52,5 +49,5 @@ }

var keyLocaleData = (_a = foundLocaleData === null || foundLocaleData === void 0 ? void 0 : foundLocaleData[key]) !== null && _a !== void 0 ? _a : [];
(0, utils_1.invariant)(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
invariant(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
var value = keyLocaleData[0];
(0, utils_1.invariant)(value === undefined || typeof value === 'string', "value must be a string or undefined");
invariant(value === undefined || typeof value === 'string', "value must be a string or undefined");
var supportedKeyword = void 0;

@@ -78,6 +75,6 @@ var entry = keywords.find(function (k) { return k.key === key; });

var optionsValue = options[key];
(0, utils_1.invariant)(optionsValue == null || typeof optionsValue === 'string', "optionsValue must be a string or undefined");
invariant(optionsValue == null || typeof optionsValue === 'string', "optionsValue must be a string or undefined");
if (typeof optionsValue === 'string') {
var ukey = key.toLowerCase();
optionsValue = (0, CanonicalizeUValue_1.CanonicalizeUValue)(ukey, optionsValue);
optionsValue = CanonicalizeUValue(ukey, optionsValue);
if (optionsValue === '') {

@@ -103,3 +100,3 @@ optionsValue = 'true';

supportedAttributes = [];
foundLocale = (0, InsertUnicodeExtensionAndCanonicalize_1.InsertUnicodeExtensionAndCanonicalize)(foundLocale, supportedAttributes, supportedKeywords);
foundLocale = InsertUnicodeExtensionAndCanonicalize(foundLocale, supportedAttributes, supportedKeywords);
}

@@ -106,0 +103,0 @@ result.locale = foundLocale;

@@ -1,2 +0,1 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
export {};

@@ -1,2 +0,2 @@

import { Keyword } from './types';
import { Keyword } from './types.js';
export declare function UnicodeExtensionComponents(extension: string): {

@@ -3,0 +3,0 @@ attributes: string[];

@@ -1,8 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnicodeExtensionComponents = UnicodeExtensionComponents;
var utils_1 = require("./utils");
function UnicodeExtensionComponents(extension) {
(0, utils_1.invariant)(extension === extension.toLowerCase(), 'Expected extension to be lowercase');
(0, utils_1.invariant)(extension.slice(0, 3) === '-u-', 'Expected extension to be a Unicode locale extension');
import { invariant } from './utils.js';
export function UnicodeExtensionComponents(extension) {
invariant(extension === extension.toLowerCase(), 'Expected extension to be lowercase');
invariant(extension.slice(0, 3) === '-u-', 'Expected extension to be a Unicode locale extension');
var attributes = [];

@@ -23,3 +20,3 @@ var keywords = [];

var subtag = extension.slice(k, k + len);
(0, utils_1.invariant)(len >= 2, 'Expected a subtag to have at least 2 characters');
invariant(len >= 2, 'Expected a subtag to have at least 2 characters');
if (keyword === undefined && len != 2) {

@@ -40,3 +37,3 @@ if (attributes.indexOf(subtag) === -1) {

else {
(0, utils_1.invariant)(keyword !== undefined, 'Expected keyword to be defined');
invariant(keyword !== undefined, 'Expected keyword to be defined');
keyword.value += '-' + subtag;

@@ -43,0 +40,0 @@ }

@@ -1,5 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnicodeExtensionValue = UnicodeExtensionValue;
var utils_1 = require("./utils");
import { invariant } from './utils.js';
/**

@@ -10,4 +7,4 @@ * https://tc39.es/ecma402/#sec-unicodeextensionvalue

*/
function UnicodeExtensionValue(extension, key) {
(0, utils_1.invariant)(key.length === 2, 'key must have 2 elements');
export function UnicodeExtensionValue(extension, key) {
invariant(key.length === 2, 'key must have 2 elements');
var size = extension.length;

@@ -14,0 +11,0 @@ var searchValue = "-".concat(key, "-");

@@ -1,12 +0,6 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UNICODE_EXTENSION_SEQUENCE_REGEX = void 0;
exports.invariant = invariant;
exports.findMatchingDistance = findMatchingDistance;
exports.findBestMatch = findBestMatch;
var tslib_1 = require("tslib");
var languageMatching_1 = require("./languageMatching");
var regions_generated_1 = require("./regions.generated");
exports.UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi;
function invariant(condition, message, Err) {
import { __spreadArray } from "tslib";
import { data as jsonData } from './languageMatching.js';
import { regions } from './regions.generated.js';
export var UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi;
export function invariant(condition, message, Err) {
if (Err === void 0) { Err = Error; }

@@ -23,5 +17,5 @@ if (!condition) {

if (!PROCESSED_DATA) {
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(' ');
var matchVariables = languageMatching_1.data.supplemental.languageMatching['written-new'].slice(1, 5);
var data = languageMatching_1.data.supplemental.languageMatching['written-new'].slice(5);
var paradigmLocales = (_b = (_a = jsonData.supplemental.languageMatching['written-new'][0]) === null || _a === void 0 ? void 0 : _a.paradigmLocales) === null || _b === void 0 ? void 0 : _b._locales.split(' ');
var matchVariables = jsonData.supplemental.languageMatching['written-new'].slice(1, 5);
var data = jsonData.supplemental.languageMatching['written-new'].slice(5);
var matches = data.map(function (d) {

@@ -45,3 +39,3 @@ var key = Object.keys(d)[0];

}, {}),
paradigmLocales: tslib_1.__spreadArray(tslib_1.__spreadArray([], paradigmLocales, true), paradigmLocales.map(function (l) {
paradigmLocales: __spreadArray(__spreadArray([], paradigmLocales, true), paradigmLocales.map(function (l) {
return new Intl.Locale(l.replace(/_/g, '-')).maximize().toString();

@@ -62,4 +56,4 @@ }), true),

var expandedMatchedRegions = matchRegions
.map(function (r) { return regions_generated_1.regions[r] || [r]; })
.reduce(function (all, list) { return tslib_1.__spreadArray(tslib_1.__spreadArray([], all, true), list, true); }, []);
.map(function (r) { return regions[r] || [r]; })
.reduce(function (all, list) { return __spreadArray(__spreadArray([], all, true), list, true); }, []);
matches && (matches = !(expandedMatchedRegions.indexOf(locale.region || '') > -1 !=

@@ -103,3 +97,3 @@ shouldInclude));

}
function findMatchingDistance(desired, supported) {
export function findMatchingDistance(desired, supported) {
var desiredLocale = new Intl.Locale(desired).maximize();

@@ -146,3 +140,3 @@ var supportedLocale = new Intl.Locale(supported).maximize();

}
function findBestMatch(requestedLocales, supportedLocales, threshold) {
export function findBestMatch(requestedLocales, supportedLocales, threshold) {
if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; }

@@ -149,0 +143,0 @@ var lowestDistance = Infinity;

@@ -5,3 +5,3 @@ export interface Opts {

export declare function match(requestedLocales: readonly string[], availableLocales: readonly string[], defaultLocale: string, opts?: Opts): string;
export { LookupSupportedLocales } from './abstract/LookupSupportedLocales';
export { ResolveLocale } from './abstract/ResolveLocale';
export { LookupSupportedLocales } from './abstract/LookupSupportedLocales.js';
export { ResolveLocale } from './abstract/ResolveLocale.js';

@@ -1,15 +0,9 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResolveLocale = exports.LookupSupportedLocales = void 0;
exports.match = match;
var CanonicalizeLocaleList_1 = require("./abstract/CanonicalizeLocaleList");
var ResolveLocale_1 = require("./abstract/ResolveLocale");
function match(requestedLocales, availableLocales, defaultLocale, opts) {
return (0, ResolveLocale_1.ResolveLocale)(availableLocales, (0, CanonicalizeLocaleList_1.CanonicalizeLocaleList)(requestedLocales), {
import { CanonicalizeLocaleList } from './abstract/CanonicalizeLocaleList.js';
import { ResolveLocale } from './abstract/ResolveLocale.js';
export function match(requestedLocales, availableLocales, defaultLocale, opts) {
return ResolveLocale(availableLocales, CanonicalizeLocaleList(requestedLocales), {
localeMatcher: (opts === null || opts === void 0 ? void 0 : opts.algorithm) || 'best fit',
}, [], {}, function () { return defaultLocale; }).locale;
}
var LookupSupportedLocales_1 = require("./abstract/LookupSupportedLocales");
Object.defineProperty(exports, "LookupSupportedLocales", { enumerable: true, get: function () { return LookupSupportedLocales_1.LookupSupportedLocales; } });
var ResolveLocale_2 = require("./abstract/ResolveLocale");
Object.defineProperty(exports, "ResolveLocale", { enumerable: true, get: function () { return ResolveLocale_2.ResolveLocale; } });
export { LookupSupportedLocales } from './abstract/LookupSupportedLocales.js';
export { ResolveLocale } from './abstract/ResolveLocale.js';
{
"name": "@formatjs/intl-localematcher",
"description": "Intl.LocaleMatcher ponyfill",
"version": "0.6.2",
"version": "0.7.0",
"license": "MIT",
"author": "Long Ho <holevietlong@gmail.com>",
"type": "module",
"sideEffects": false,
"types": "index.d.ts",
"exports": {
".": "./index.js"
},
"dependencies": {

@@ -22,5 +27,3 @@ "tslib": "^2.8.0"

],
"main": "index.js",
"module": "lib/index.js",
"repository": "formatjs/formatjs.git"
}
/**
* https://tc39.es/ecma402/#sec-bestavailablelocale
* @param availableLocales
* @param locale
*/
export declare function BestAvailableLocale(availableLocales: readonly string[], locale: string): string | undefined;
/**
* https://tc39.es/ecma402/#sec-bestavailablelocale
* @param availableLocales
* @param locale
*/
export function BestAvailableLocale(availableLocales, locale) {
var candidate = locale;
while (true) {
if (availableLocales.indexOf(candidate) > -1) {
return candidate;
}
var pos = candidate.lastIndexOf('-');
if (!~pos) {
return undefined;
}
if (pos >= 2 && candidate[pos - 2] === '-') {
pos -= 2;
}
candidate = candidate.slice(0, pos);
}
}
import { LookupMatcherResult } from './types';
/**
* https://tc39.es/ecma402/#sec-bestfitmatcher
* @param availableLocales
* @param requestedLocales
* @param getDefaultLocale
*/
export declare function BestFitMatcher(availableLocales: readonly string[], requestedLocales: readonly string[], getDefaultLocale: () => string): LookupMatcherResult;
import { UNICODE_EXTENSION_SEQUENCE_REGEX, findBestMatch } from './utils';
/**
* https://tc39.es/ecma402/#sec-bestfitmatcher
* @param availableLocales
* @param requestedLocales
* @param getDefaultLocale
*/
export function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
var foundLocale;
var extension;
var noExtensionLocales = [];
var noExtensionLocaleMap = requestedLocales.reduce(function (all, l) {
var noExtensionLocale = l.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
noExtensionLocales.push(noExtensionLocale);
all[noExtensionLocale] = l;
return all;
}, {});
var result = findBestMatch(noExtensionLocales, availableLocales);
if (result.matchedSupportedLocale && result.matchedDesiredLocale) {
foundLocale = result.matchedSupportedLocale;
extension =
noExtensionLocaleMap[result.matchedDesiredLocale].slice(result.matchedDesiredLocale.length) || undefined;
}
if (!foundLocale) {
return { locale: getDefaultLocale() };
}
return {
locale: foundLocale,
extension: extension,
};
}
/**
* http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist
* @param locales
*/
export declare function CanonicalizeLocaleList(locales?: string | readonly string[]): string[];
/**
* http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist
* @param locales
*/
export function CanonicalizeLocaleList(locales) {
return Intl.getCanonicalLocales(locales);
}
export declare function CanonicalizeUnicodeLocaleId(locale: string): string;
export function CanonicalizeUnicodeLocaleId(locale) {
return Intl.getCanonicalLocales(locale)[0];
}
export declare function CanonicalizeUValue(ukey: string, uvalue: string): string;
import { invariant } from './utils';
export function CanonicalizeUValue(ukey, uvalue) {
// TODO: Implement algorithm for CanonicalizeUValue per https://tc39.es/ecma402/#sec-canonicalizeuvalue
var lowerValue = uvalue.toLowerCase();
invariant(ukey !== undefined, "ukey must be defined");
var canonicalized = lowerValue;
return canonicalized;
}
import { Keyword } from './types';
export declare function InsertUnicodeExtensionAndCanonicalize(locale: string, attributes: string[], keywords: Array<Keyword>): string;
import { CanonicalizeUnicodeLocaleId } from './CanonicalizeUnicodeLocaleId';
import { invariant } from './utils';
export function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
invariant(locale.indexOf('-u-') === -1, 'Expected locale to not have a Unicode locale extension');
var extension = '-u';
for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) {
var attr = attributes_1[_i];
extension += "-".concat(attr);
}
for (var _a = 0, keywords_1 = keywords; _a < keywords_1.length; _a++) {
var kw = keywords_1[_a];
var key = kw.key, value = kw.value;
extension += "-".concat(key);
if (value !== '') {
extension += "-".concat(value);
}
}
if (extension === '-u') {
return CanonicalizeUnicodeLocaleId(locale);
}
var privateIndex = locale.indexOf('-x-');
var newLocale;
if (privateIndex === -1) {
newLocale = locale + extension;
}
else {
var preExtension = locale.slice(0, privateIndex);
var postExtension = locale.slice(privateIndex);
newLocale = preExtension + extension + postExtension;
}
return CanonicalizeUnicodeLocaleId(newLocale);
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

import { LookupMatcherResult } from './types';
/**
* https://tc39.es/ecma402/#sec-lookupmatcher
* @param availableLocales
* @param requestedLocales
* @param getDefaultLocale
*/
export declare function LookupMatcher(availableLocales: readonly string[], requestedLocales: readonly string[], getDefaultLocale: () => string): LookupMatcherResult;
import { BestAvailableLocale } from './BestAvailableLocale';
import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils';
/**
* https://tc39.es/ecma402/#sec-lookupmatcher
* @param availableLocales
* @param requestedLocales
* @param getDefaultLocale
*/
export function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
var result = { locale: '' };
for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
var locale = requestedLocales_1[_i];
var noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
if (availableLocale) {
result.locale = availableLocale;
if (locale !== noExtensionLocale) {
result.extension = locale.slice(noExtensionLocale.length, locale.length);
}
return result;
}
}
result.locale = getDefaultLocale();
return result;
}
/**
* https://tc39.es/ecma402/#sec-lookupsupportedlocales
* @param availableLocales
* @param requestedLocales
*/
export declare function LookupSupportedLocales(availableLocales: string[], requestedLocales: string[]): string[];
import { BestAvailableLocale } from './BestAvailableLocale';
import { UNICODE_EXTENSION_SEQUENCE_REGEX } from './utils';
/**
* https://tc39.es/ecma402/#sec-lookupsupportedlocales
* @param availableLocales
* @param requestedLocales
*/
export function LookupSupportedLocales(availableLocales, requestedLocales) {
var subset = [];
for (var _i = 0, requestedLocales_1 = requestedLocales; _i < requestedLocales_1.length; _i++) {
var locale = requestedLocales_1[_i];
var noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, '');
var availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
if (availableLocale) {
subset.push(availableLocale);
}
}
return subset;
}
export declare const regions: Record<string, string[]>;
// This file is generated from regions-gen.ts
export var regions = {
"001": [
"001",
"001-status-grouping",
"002",
"005",
"009",
"011",
"013",
"014",
"015",
"017",
"018",
"019",
"021",
"029",
"030",
"034",
"035",
"039",
"053",
"054",
"057",
"061",
"142",
"143",
"145",
"150",
"151",
"154",
"155",
"AC",
"AD",
"AE",
"AF",
"AG",
"AI",
"AL",
"AM",
"AO",
"AQ",
"AR",
"AS",
"AT",
"AU",
"AW",
"AX",
"AZ",
"BA",
"BB",
"BD",
"BE",
"BF",
"BG",
"BH",
"BI",
"BJ",
"BL",
"BM",
"BN",
"BO",
"BQ",
"BR",
"BS",
"BT",
"BV",
"BW",
"BY",
"BZ",
"CA",
"CC",
"CD",
"CF",
"CG",
"CH",
"CI",
"CK",
"CL",
"CM",
"CN",
"CO",
"CP",
"CQ",
"CR",
"CU",
"CV",
"CW",
"CX",
"CY",
"CZ",
"DE",
"DG",
"DJ",
"DK",
"DM",
"DO",
"DZ",
"EA",
"EC",
"EE",
"EG",
"EH",
"ER",
"ES",
"ET",
"EU",
"EZ",
"FI",
"FJ",
"FK",
"FM",
"FO",
"FR",
"GA",
"GB",
"GD",
"GE",
"GF",
"GG",
"GH",
"GI",
"GL",
"GM",
"GN",
"GP",
"GQ",
"GR",
"GS",
"GT",
"GU",
"GW",
"GY",
"HK",
"HM",
"HN",
"HR",
"HT",
"HU",
"IC",
"ID",
"IE",
"IL",
"IM",
"IN",
"IO",
"IQ",
"IR",
"IS",
"IT",
"JE",
"JM",
"JO",
"JP",
"KE",
"KG",
"KH",
"KI",
"KM",
"KN",
"KP",
"KR",
"KW",
"KY",
"KZ",
"LA",
"LB",
"LC",
"LI",
"LK",
"LR",
"LS",
"LT",
"LU",
"LV",
"LY",
"MA",
"MC",
"MD",
"ME",
"MF",
"MG",
"MH",
"MK",
"ML",
"MM",
"MN",
"MO",
"MP",
"MQ",
"MR",
"MS",
"MT",
"MU",
"MV",
"MW",
"MX",
"MY",
"MZ",
"NA",
"NC",
"NE",
"NF",
"NG",
"NI",
"NL",
"NO",
"NP",
"NR",
"NU",
"NZ",
"OM",
"PA",
"PE",
"PF",
"PG",
"PH",
"PK",
"PL",
"PM",
"PN",
"PR",
"PS",
"PT",
"PW",
"PY",
"QA",
"QO",
"RE",
"RO",
"RS",
"RU",
"RW",
"SA",
"SB",
"SC",
"SD",
"SE",
"SG",
"SH",
"SI",
"SJ",
"SK",
"SL",
"SM",
"SN",
"SO",
"SR",
"SS",
"ST",
"SV",
"SX",
"SY",
"SZ",
"TA",
"TC",
"TD",
"TF",
"TG",
"TH",
"TJ",
"TK",
"TL",
"TM",
"TN",
"TO",
"TR",
"TT",
"TV",
"TW",
"TZ",
"UA",
"UG",
"UM",
"UN",
"US",
"UY",
"UZ",
"VA",
"VC",
"VE",
"VG",
"VI",
"VN",
"VU",
"WF",
"WS",
"XK",
"YE",
"YT",
"ZA",
"ZM",
"ZW"
],
"002": [
"002",
"002-status-grouping",
"011",
"014",
"015",
"017",
"018",
"202",
"AO",
"BF",
"BI",
"BJ",
"BW",
"CD",
"CF",
"CG",
"CI",
"CM",
"CV",
"DJ",
"DZ",
"EA",
"EG",
"EH",
"ER",
"ET",
"GA",
"GH",
"GM",
"GN",
"GQ",
"GW",
"IC",
"IO",
"KE",
"KM",
"LR",
"LS",
"LY",
"MA",
"MG",
"ML",
"MR",
"MU",
"MW",
"MZ",
"NA",
"NE",
"NG",
"RE",
"RW",
"SC",
"SD",
"SH",
"SL",
"SN",
"SO",
"SS",
"ST",
"SZ",
"TD",
"TF",
"TG",
"TN",
"TZ",
"UG",
"YT",
"ZA",
"ZM",
"ZW"
],
"003": [
"003",
"013",
"021",
"029",
"AG",
"AI",
"AW",
"BB",
"BL",
"BM",
"BQ",
"BS",
"BZ",
"CA",
"CR",
"CU",
"CW",
"DM",
"DO",
"GD",
"GL",
"GP",
"GT",
"HN",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"MX",
"NI",
"PA",
"PM",
"PR",
"SV",
"SX",
"TC",
"TT",
"US",
"VC",
"VG",
"VI"
],
"005": [
"005",
"AR",
"BO",
"BR",
"BV",
"CL",
"CO",
"EC",
"FK",
"GF",
"GS",
"GY",
"PE",
"PY",
"SR",
"UY",
"VE"
],
"009": [
"009",
"053",
"054",
"057",
"061",
"AC",
"AQ",
"AS",
"AU",
"CC",
"CK",
"CP",
"CX",
"DG",
"FJ",
"FM",
"GU",
"HM",
"KI",
"MH",
"MP",
"NC",
"NF",
"NR",
"NU",
"NZ",
"PF",
"PG",
"PN",
"PW",
"QO",
"SB",
"TA",
"TK",
"TO",
"TV",
"UM",
"VU",
"WF",
"WS"
],
"011": [
"011",
"BF",
"BJ",
"CI",
"CV",
"GH",
"GM",
"GN",
"GW",
"LR",
"ML",
"MR",
"NE",
"NG",
"SH",
"SL",
"SN",
"TG"
],
"013": [
"013",
"BZ",
"CR",
"GT",
"HN",
"MX",
"NI",
"PA",
"SV"
],
"014": [
"014",
"BI",
"DJ",
"ER",
"ET",
"IO",
"KE",
"KM",
"MG",
"MU",
"MW",
"MZ",
"RE",
"RW",
"SC",
"SO",
"SS",
"TF",
"TZ",
"UG",
"YT",
"ZM",
"ZW"
],
"015": [
"015",
"DZ",
"EA",
"EG",
"EH",
"IC",
"LY",
"MA",
"SD",
"TN"
],
"017": [
"017",
"AO",
"CD",
"CF",
"CG",
"CM",
"GA",
"GQ",
"ST",
"TD"
],
"018": [
"018",
"BW",
"LS",
"NA",
"SZ",
"ZA"
],
"019": [
"003",
"005",
"013",
"019",
"019-status-grouping",
"021",
"029",
"419",
"AG",
"AI",
"AR",
"AW",
"BB",
"BL",
"BM",
"BO",
"BQ",
"BR",
"BS",
"BV",
"BZ",
"CA",
"CL",
"CO",
"CR",
"CU",
"CW",
"DM",
"DO",
"EC",
"FK",
"GD",
"GF",
"GL",
"GP",
"GS",
"GT",
"GY",
"HN",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"MX",
"NI",
"PA",
"PE",
"PM",
"PR",
"PY",
"SR",
"SV",
"SX",
"TC",
"TT",
"US",
"UY",
"VC",
"VE",
"VG",
"VI"
],
"021": [
"021",
"BM",
"CA",
"GL",
"PM",
"US"
],
"029": [
"029",
"AG",
"AI",
"AW",
"BB",
"BL",
"BQ",
"BS",
"CU",
"CW",
"DM",
"DO",
"GD",
"GP",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"PR",
"SX",
"TC",
"TT",
"VC",
"VG",
"VI"
],
"030": [
"030",
"CN",
"HK",
"JP",
"KP",
"KR",
"MN",
"MO",
"TW"
],
"034": [
"034",
"AF",
"BD",
"BT",
"IN",
"IR",
"LK",
"MV",
"NP",
"PK"
],
"035": [
"035",
"BN",
"ID",
"KH",
"LA",
"MM",
"MY",
"PH",
"SG",
"TH",
"TL",
"VN"
],
"039": [
"039",
"AD",
"AL",
"BA",
"ES",
"GI",
"GR",
"HR",
"IT",
"ME",
"MK",
"MT",
"PT",
"RS",
"SI",
"SM",
"VA",
"XK"
],
"053": [
"053",
"AU",
"CC",
"CX",
"HM",
"NF",
"NZ"
],
"054": [
"054",
"FJ",
"NC",
"PG",
"SB",
"VU"
],
"057": [
"057",
"FM",
"GU",
"KI",
"MH",
"MP",
"NR",
"PW",
"UM"
],
"061": [
"061",
"AS",
"CK",
"NU",
"PF",
"PN",
"TK",
"TO",
"TV",
"WF",
"WS"
],
"142": [
"030",
"034",
"035",
"142",
"143",
"145",
"AE",
"AF",
"AM",
"AZ",
"BD",
"BH",
"BN",
"BT",
"CN",
"CY",
"GE",
"HK",
"ID",
"IL",
"IN",
"IQ",
"IR",
"JO",
"JP",
"KG",
"KH",
"KP",
"KR",
"KW",
"KZ",
"LA",
"LB",
"LK",
"MM",
"MN",
"MO",
"MV",
"MY",
"NP",
"OM",
"PH",
"PK",
"PS",
"QA",
"SA",
"SG",
"SY",
"TH",
"TJ",
"TL",
"TM",
"TR",
"TW",
"UZ",
"VN",
"YE"
],
"143": [
"143",
"KG",
"KZ",
"TJ",
"TM",
"UZ"
],
"145": [
"145",
"AE",
"AM",
"AZ",
"BH",
"CY",
"GE",
"IL",
"IQ",
"JO",
"KW",
"LB",
"OM",
"PS",
"QA",
"SA",
"SY",
"TR",
"YE"
],
"150": [
"039",
"150",
"151",
"154",
"155",
"AD",
"AL",
"AT",
"AX",
"BA",
"BE",
"BG",
"BY",
"CH",
"CQ",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FO",
"FR",
"GB",
"GG",
"GI",
"GR",
"HR",
"HU",
"IE",
"IM",
"IS",
"IT",
"JE",
"LI",
"LT",
"LU",
"LV",
"MC",
"MD",
"ME",
"MK",
"MT",
"NL",
"NO",
"PL",
"PT",
"RO",
"RS",
"RU",
"SE",
"SI",
"SJ",
"SK",
"SM",
"UA",
"VA",
"XK"
],
"151": [
"151",
"BG",
"BY",
"CZ",
"HU",
"MD",
"PL",
"RO",
"RU",
"SK",
"UA"
],
"154": [
"154",
"AX",
"CQ",
"DK",
"EE",
"FI",
"FO",
"GB",
"GG",
"IE",
"IM",
"IS",
"JE",
"LT",
"LV",
"NO",
"SE",
"SJ"
],
"155": [
"155",
"AT",
"BE",
"CH",
"DE",
"FR",
"LI",
"LU",
"MC",
"NL"
],
"202": [
"011",
"014",
"017",
"018",
"202",
"AO",
"BF",
"BI",
"BJ",
"BW",
"CD",
"CF",
"CG",
"CI",
"CM",
"CV",
"DJ",
"ER",
"ET",
"GA",
"GH",
"GM",
"GN",
"GQ",
"GW",
"IO",
"KE",
"KM",
"LR",
"LS",
"MG",
"ML",
"MR",
"MU",
"MW",
"MZ",
"NA",
"NE",
"NG",
"RE",
"RW",
"SC",
"SH",
"SL",
"SN",
"SO",
"SS",
"ST",
"SZ",
"TD",
"TF",
"TG",
"TZ",
"UG",
"YT",
"ZA",
"ZM",
"ZW"
],
"419": [
"005",
"013",
"029",
"419",
"AG",
"AI",
"AR",
"AW",
"BB",
"BL",
"BO",
"BQ",
"BR",
"BS",
"BV",
"BZ",
"CL",
"CO",
"CR",
"CU",
"CW",
"DM",
"DO",
"EC",
"FK",
"GD",
"GF",
"GP",
"GS",
"GT",
"GY",
"HN",
"HT",
"JM",
"KN",
"KY",
"LC",
"MF",
"MQ",
"MS",
"MX",
"NI",
"PA",
"PE",
"PR",
"PY",
"SR",
"SV",
"SX",
"TC",
"TT",
"UY",
"VC",
"VE",
"VG",
"VI"
],
"EU": [
"AT",
"BE",
"BG",
"CY",
"CZ",
"DE",
"DK",
"EE",
"ES",
"EU",
"FI",
"FR",
"GR",
"HR",
"HU",
"IE",
"IT",
"LT",
"LU",
"LV",
"MT",
"NL",
"PL",
"PT",
"RO",
"SE",
"SI",
"SK"
],
"EZ": [
"AT",
"BE",
"CY",
"DE",
"EE",
"ES",
"EZ",
"FI",
"FR",
"GR",
"IE",
"IT",
"LT",
"LU",
"LV",
"MT",
"NL",
"PT",
"SI",
"SK"
],
"QO": [
"AC",
"AQ",
"CP",
"DG",
"QO",
"TA"
],
"UN": [
"AD",
"AE",
"AF",
"AG",
"AL",
"AM",
"AO",
"AR",
"AT",
"AU",
"AZ",
"BA",
"BB",
"BD",
"BE",
"BF",
"BG",
"BH",
"BI",
"BJ",
"BN",
"BO",
"BR",
"BS",
"BT",
"BW",
"BY",
"BZ",
"CA",
"CD",
"CF",
"CG",
"CH",
"CI",
"CL",
"CM",
"CN",
"CO",
"CR",
"CU",
"CV",
"CY",
"CZ",
"DE",
"DJ",
"DK",
"DM",
"DO",
"DZ",
"EC",
"EE",
"EG",
"ER",
"ES",
"ET",
"FI",
"FJ",
"FM",
"FR",
"GA",
"GB",
"GD",
"GE",
"GH",
"GM",
"GN",
"GQ",
"GR",
"GT",
"GW",
"GY",
"HN",
"HR",
"HT",
"HU",
"ID",
"IE",
"IL",
"IN",
"IQ",
"IR",
"IS",
"IT",
"JM",
"JO",
"JP",
"KE",
"KG",
"KH",
"KI",
"KM",
"KN",
"KP",
"KR",
"KW",
"KZ",
"LA",
"LB",
"LC",
"LI",
"LK",
"LR",
"LS",
"LT",
"LU",
"LV",
"LY",
"MA",
"MC",
"MD",
"ME",
"MG",
"MH",
"MK",
"ML",
"MM",
"MN",
"MR",
"MT",
"MU",
"MV",
"MW",
"MX",
"MY",
"MZ",
"NA",
"NE",
"NG",
"NI",
"NL",
"NO",
"NP",
"NR",
"NZ",
"OM",
"PA",
"PE",
"PG",
"PH",
"PK",
"PL",
"PT",
"PW",
"PY",
"QA",
"RO",
"RS",
"RU",
"RW",
"SA",
"SB",
"SC",
"SD",
"SE",
"SG",
"SI",
"SK",
"SL",
"SM",
"SN",
"SO",
"SR",
"SS",
"ST",
"SV",
"SY",
"SZ",
"TD",
"TG",
"TH",
"TJ",
"TL",
"TM",
"TN",
"TO",
"TR",
"TT",
"TV",
"TZ",
"UA",
"UG",
"UN",
"US",
"UY",
"UZ",
"VC",
"VE",
"VN",
"VU",
"WS",
"YE",
"ZA",
"ZM",
"ZW"
]
};
export interface ResolveLocaleResult {
locale: string;
dataLocale: string;
[k: string]: any;
}
/**
* https://tc39.es/ecma402/#sec-resolvelocale
*/
export declare function ResolveLocale<K extends string, D extends {
[k in K]: any;
}>(availableLocales: Set<string> | readonly string[], requestedLocales: readonly string[], options: {
localeMatcher: string;
[k: string]: string;
}, relevantExtensionKeys: K[], localeData: Record<string, D | undefined>, getDefaultLocale: () => string): ResolveLocaleResult;
import { BestFitMatcher } from './BestFitMatcher';
import { CanonicalizeUValue } from './CanonicalizeUValue';
import { InsertUnicodeExtensionAndCanonicalize } from './InsertUnicodeExtensionAndCanonicalize';
import { LookupMatcher } from './LookupMatcher';
import { UnicodeExtensionComponents } from './UnicodeExtensionComponents';
import { invariant } from './utils';
/**
* https://tc39.es/ecma402/#sec-resolvelocale
*/
export function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
var _a;
var matcher = options.localeMatcher;
var r;
if (matcher === 'lookup') {
r = LookupMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
}
else {
r = BestFitMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
}
if (r == null) {
r = {
locale: getDefaultLocale(),
extension: '',
};
}
var foundLocale = r.locale;
var foundLocaleData = localeData[foundLocale];
// TODO: We can't really guarantee that the locale data is available
// invariant(
// foundLocaleData !== undefined,
// `Missing locale data for ${foundLocale}`
// )
var result = { locale: 'en', dataLocale: foundLocale };
var components;
var keywords;
if (r.extension) {
components = UnicodeExtensionComponents(r.extension);
keywords = components.keywords;
}
else {
keywords = [];
}
var supportedKeywords = [];
var _loop_1 = function (key) {
// TODO: Shouldn't default to empty array, see TODO above
var keyLocaleData = (_a = foundLocaleData === null || foundLocaleData === void 0 ? void 0 : foundLocaleData[key]) !== null && _a !== void 0 ? _a : [];
invariant(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
var value = keyLocaleData[0];
invariant(value === undefined || typeof value === 'string', "value must be a string or undefined");
var supportedKeyword = void 0;
var entry = keywords.find(function (k) { return k.key === key; });
if (entry) {
var requestedValue = entry.value;
if (requestedValue !== '') {
if (keyLocaleData.indexOf(requestedValue) > -1) {
value = requestedValue;
supportedKeyword = {
key: key,
value: value,
};
}
}
else if (keyLocaleData.indexOf('true') > -1) {
value = 'true';
supportedKeyword = {
key: key,
value: value,
};
}
}
var optionsValue = options[key];
invariant(optionsValue == null || typeof optionsValue === 'string', "optionsValue must be a string or undefined");
if (typeof optionsValue === 'string') {
var ukey = key.toLowerCase();
optionsValue = CanonicalizeUValue(ukey, optionsValue);
if (optionsValue === '') {
optionsValue = 'true';
}
}
if (optionsValue !== value && keyLocaleData.indexOf(optionsValue) > -1) {
value = optionsValue;
supportedKeyword = undefined;
}
if (supportedKeyword) {
supportedKeywords.push(supportedKeyword);
}
result[key] = value;
};
for (var _i = 0, relevantExtensionKeys_1 = relevantExtensionKeys; _i < relevantExtensionKeys_1.length; _i++) {
var key = relevantExtensionKeys_1[_i];
_loop_1(key);
}
var supportedAttributes = [];
if (supportedKeywords.length > 0) {
supportedAttributes = [];
foundLocale = InsertUnicodeExtensionAndCanonicalize(foundLocale, supportedAttributes, supportedKeywords);
}
result.locale = foundLocale;
return result;
}
export interface LookupMatcherResult {
locale: string;
extension?: string;
nu?: string;
}
export interface Keyword {
key: string;
value: string;
}
import { Keyword } from './types';
export declare function UnicodeExtensionComponents(extension: string): {
attributes: string[];
keywords: Array<Keyword>;
};
import { invariant } from './utils';
export function UnicodeExtensionComponents(extension) {
invariant(extension === extension.toLowerCase(), 'Expected extension to be lowercase');
invariant(extension.slice(0, 3) === '-u-', 'Expected extension to be a Unicode locale extension');
var attributes = [];
var keywords = [];
var keyword;
var size = extension.length;
var k = 3;
while (k < size) {
var e = extension.indexOf('-', k);
var len = void 0;
if (e === -1) {
len = size - k;
}
else {
len = e - k;
}
var subtag = extension.slice(k, k + len);
invariant(len >= 2, 'Expected a subtag to have at least 2 characters');
if (keyword === undefined && len != 2) {
if (attributes.indexOf(subtag) === -1) {
attributes.push(subtag);
}
}
else if (len === 2) {
keyword = { key: subtag, value: '' };
if (keywords.find(function (k) { return k.key === (keyword === null || keyword === void 0 ? void 0 : keyword.key); }) === undefined) {
keywords.push(keyword);
}
}
else if ((keyword === null || keyword === void 0 ? void 0 : keyword.value) === '') {
keyword.value = subtag;
}
else {
invariant(keyword !== undefined, 'Expected keyword to be defined');
keyword.value += '-' + subtag;
}
k += len + 1;
}
return { attributes: attributes, keywords: keywords };
}
/**
* https://tc39.es/ecma402/#sec-unicodeextensionvalue
* @param extension
* @param key
*/
export declare function UnicodeExtensionValue(extension: string, key: string): string | undefined;
import { invariant } from './utils';
/**
* https://tc39.es/ecma402/#sec-unicodeextensionvalue
* @param extension
* @param key
*/
export function UnicodeExtensionValue(extension, key) {
invariant(key.length === 2, 'key must have 2 elements');
var size = extension.length;
var searchValue = "-".concat(key, "-");
var pos = extension.indexOf(searchValue);
if (pos !== -1) {
var start = pos + 4;
var end = start;
var k = start;
var done = false;
while (!done) {
var e = extension.indexOf('-', k);
var len = void 0;
if (e === -1) {
len = size - k;
}
else {
len = e - k;
}
if (len === 2) {
done = true;
}
else if (e === -1) {
end = size;
done = true;
}
else {
end = e;
k = e + 1;
}
}
return extension.slice(start, end);
}
searchValue = "-".concat(key);
pos = extension.indexOf(searchValue);
if (pos !== -1 && pos + 3 === size) {
return '';
}
return undefined;
}
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;
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 {};
import { __spreadArray } from "tslib";
import { data as jsonData } from './languageMatching';
import { regions } from './regions.generated';
export var UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi;
export function invariant(condition, message, Err) {
if (Err === void 0) { Err = Error; }
if (!condition) {
throw new Err(message);
}
}
// This is effectively 2 languages in 2 different regions in the same cluster
var DEFAULT_MATCHING_THRESHOLD = 838;
var PROCESSED_DATA;
function processData() {
var _a, _b;
if (!PROCESSED_DATA) {
var paradigmLocales = (_b = (_a = jsonData.supplemental.languageMatching['written-new'][0]) === null || _a === void 0 ? void 0 : _a.paradigmLocales) === null || _b === void 0 ? void 0 : _b._locales.split(' ');
var matchVariables = jsonData.supplemental.languageMatching['written-new'].slice(1, 5);
var data = jsonData.supplemental.languageMatching['written-new'].slice(5);
var matches = data.map(function (d) {
var key = Object.keys(d)[0];
var value = d[key];
return {
supported: key,
desired: value._desired,
distance: +value._distance,
oneway: value.oneway === 'true' ? true : false,
};
}, {});
PROCESSED_DATA = {
matches: matches,
matchVariables: matchVariables.reduce(function (all, d) {
var key = Object.keys(d)[0];
var value = d[key];
all[key.slice(1)] = value._value.split('+');
return all;
}, {}),
paradigmLocales: __spreadArray(__spreadArray([], paradigmLocales, true), paradigmLocales.map(function (l) {
return new Intl.Locale(l.replace(/_/g, '-')).maximize().toString();
}), true),
};
}
return PROCESSED_DATA;
}
function isMatched(locale, languageMatchInfoLocale, matchVariables) {
var _a = languageMatchInfoLocale.split('-'), language = _a[0], script = _a[1], region = _a[2];
var matches = true;
if (region && region[0] === '$') {
var shouldInclude = region[1] !== '!';
var matchRegions = shouldInclude
? matchVariables[region.slice(1)]
: matchVariables[region.slice(2)];
var expandedMatchedRegions = matchRegions
.map(function (r) { return regions[r] || [r]; })
.reduce(function (all, list) { return __spreadArray(__spreadArray([], all, true), list, true); }, []);
matches && (matches = !(expandedMatchedRegions.indexOf(locale.region || '') > -1 !=
shouldInclude));
}
else {
matches && (matches = locale.region
? region === '*' || region === locale.region
: true);
}
matches && (matches = locale.script ? script === '*' || script === locale.script : true);
matches && (matches = locale.language
? language === '*' || language === locale.language
: true);
return matches;
}
function serializeLSR(lsr) {
return [lsr.language, lsr.script, lsr.region].filter(Boolean).join('-');
}
function findMatchingDistanceForLSR(desired, supported, data) {
for (var _i = 0, _a = data.matches; _i < _a.length; _i++) {
var d = _a[_i];
var matches = isMatched(desired, d.desired, data.matchVariables) &&
isMatched(supported, d.supported, data.matchVariables);
if (!d.oneway && !matches) {
matches =
isMatched(desired, d.supported, data.matchVariables) &&
isMatched(supported, d.desired, data.matchVariables);
}
if (matches) {
var distance = d.distance * 10;
if (data.paradigmLocales.indexOf(serializeLSR(desired)) > -1 !=
data.paradigmLocales.indexOf(serializeLSR(supported)) > -1) {
return distance - 1;
}
return distance;
}
}
throw new Error('No matching distance found');
}
export function findMatchingDistance(desired, supported) {
var desiredLocale = new Intl.Locale(desired).maximize();
var supportedLocale = new Intl.Locale(supported).maximize();
var desiredLSR = {
language: desiredLocale.language,
script: desiredLocale.script || '',
region: desiredLocale.region || '',
};
var supportedLSR = {
language: supportedLocale.language,
script: supportedLocale.script || '',
region: supportedLocale.region || '',
};
var matchingDistance = 0;
var data = processData();
if (desiredLSR.language !== supportedLSR.language) {
matchingDistance += findMatchingDistanceForLSR({
language: desiredLocale.language,
script: '',
region: '',
}, {
language: supportedLocale.language,
script: '',
region: '',
}, data);
}
if (desiredLSR.script !== supportedLSR.script) {
matchingDistance += findMatchingDistanceForLSR({
language: desiredLocale.language,
script: desiredLSR.script,
region: '',
}, {
language: supportedLocale.language,
script: supportedLSR.script,
region: '',
}, data);
}
if (desiredLSR.region !== supportedLSR.region) {
matchingDistance += findMatchingDistanceForLSR(desiredLSR, supportedLSR, data);
}
return matchingDistance;
}
export function findBestMatch(requestedLocales, supportedLocales, threshold) {
if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; }
var lowestDistance = Infinity;
var result = {
matchedDesiredLocale: '',
distances: {},
};
requestedLocales.forEach(function (desired, i) {
if (!result.distances[desired]) {
result.distances[desired] = {};
}
supportedLocales.forEach(function (supported) {
// Add some weight to the distance based on the order of the supported locales
// Add penalty for the order of the requested locales, which currently is 0 since ECMA-402
// doesn't really have room for weighted locales like `en; q=0.1`
var distance = findMatchingDistance(desired, supported) + 0 + i * 40;
result.distances[desired][supported] = distance;
if (distance < lowestDistance) {
lowestDistance = distance;
result.matchedDesiredLocale = desired;
result.matchedSupportedLocale = supported;
}
});
});
if (lowestDistance >= threshold) {
result.matchedDesiredLocale = undefined;
result.matchedSupportedLocale = undefined;
}
return result;
}
export interface Opts {
algorithm: 'lookup' | 'best fit';
}
export declare function match(requestedLocales: readonly string[], availableLocales: readonly string[], defaultLocale: string, opts?: Opts): string;
export { LookupSupportedLocales } from './abstract/LookupSupportedLocales';
export { ResolveLocale } from './abstract/ResolveLocale';
import { CanonicalizeLocaleList } from './abstract/CanonicalizeLocaleList';
import { ResolveLocale } from './abstract/ResolveLocale';
export function match(requestedLocales, availableLocales, defaultLocale, opts) {
return ResolveLocale(availableLocales, CanonicalizeLocaleList(requestedLocales), {
localeMatcher: (opts === null || opts === void 0 ? void 0 : opts.algorithm) || 'best fit',
}, [], {}, function () { return defaultLocale; }).locale;
}
export { LookupSupportedLocales } from './abstract/LookupSupportedLocales';
export { ResolveLocale } from './abstract/ResolveLocale';

Sorry, the diff of this file is too big to display