@phensley/locale-matcher
Advanced tools
Comparing version 1.0.7 to 1.0.8
import { LanguageTag } from '@phensley/language-tag'; | ||
import { Locale } from '@phensley/locale'; | ||
declare class Entry implements Locale { | ||
readonly id: string; | ||
readonly tag: LanguageTag; | ||
readonly compact: string; | ||
constructor(id: string, tag: LanguageTag); | ||
} | ||
declare type LangTag = Locale | LanguageTag | string; | ||
/** | ||
* Flatten and split the string or array into a list of matcher entries. | ||
* | ||
* Visible for testing. | ||
*/ | ||
export declare const parse: (locales?: string | LangTag[], options?: LocaleMatcherOptions) => Entry[]; | ||
/** | ||
* A result returned by the LocaleMatcher. | ||
@@ -30,2 +43,12 @@ * | ||
/** | ||
* Sort the supported locale entries. The result will have the following order: | ||
* | ||
* First: default locale | ||
* Next: all paradigm locales | ||
* Last: all other locales | ||
* | ||
* Visible for testing. | ||
*/ | ||
export declare const sortEntries: (d: Entry) => (a: Entry, b: Entry) => number; | ||
/** | ||
* Given a list of supported locales, and a list of a user's desired locales | ||
@@ -55,1 +78,2 @@ * (sorted in the order of preference, descending), returns the supported | ||
} | ||
export {}; |
@@ -19,4 +19,6 @@ import { parseLanguageTag, LanguageTag } from '@phensley/language-tag'; | ||
* Flatten and split the string or array into a list of matcher entries. | ||
* | ||
* Visible for testing. | ||
*/ | ||
var parse = function (locales, options) { | ||
export var parse = function (locales, options) { | ||
if (locales === void 0) { locales = []; } | ||
@@ -86,2 +88,32 @@ if (options === void 0) { options = {}; } | ||
/** | ||
* Sort the supported locale entries. The result will have the following order: | ||
* | ||
* First: default locale | ||
* Next: all paradigm locales | ||
* Last: all other locales | ||
* | ||
* Visible for testing. | ||
*/ | ||
export var sortEntries = function (d) { return function (a, b) { | ||
// Check if entry is our default tag, to keep it at the front of the array. | ||
if (a.tag === d.tag) { | ||
return -1; | ||
} | ||
if (b.tag === d.tag) { | ||
return 1; | ||
} | ||
// Sort all paradigm locales before non-paradigms. | ||
var pa = paradigmLocales[a.compact]; | ||
var pb = paradigmLocales[b.compact]; | ||
console.log(a.compact, pa, ' ', b.compact, pb); | ||
if (pa !== undefined) { | ||
return pb === U ? -1 : numberCmp(pa, pb); | ||
} | ||
else if (pb !== undefined) { | ||
return 1; | ||
} | ||
// All other locales stay in their relative positions. | ||
return 0; | ||
}; }; | ||
/** | ||
* Given a list of supported locales, and a list of a user's desired locales | ||
@@ -110,23 +142,3 @@ * (sorted in the order of preference, descending), returns the supported | ||
this.default = this.supported[0]; | ||
this.supported.sort(function (a, b) { | ||
// Keep default tag at the front. | ||
if (a.tag === _this.default.tag) { | ||
return -1; | ||
} | ||
/* istanbul ignore if */ | ||
if (b.tag === _this.default.tag) { | ||
return 1; | ||
} | ||
// Sort all paradigm locales before non-paradigms. | ||
var pa = paradigmLocales[a.compact]; | ||
var pb = paradigmLocales[b.compact]; | ||
if (pa !== undefined) { | ||
return pb === U ? -1 : numberCmp(pa, pb); | ||
} | ||
else if (pb !== undefined) { | ||
return 1; | ||
} | ||
// All other locales stay in their relative positions. | ||
return 0; | ||
}); | ||
this.supported.sort(sortEntries(this.default)); | ||
// Wire up a map for quick lookups of exact matches. These have a | ||
@@ -133,0 +145,0 @@ // distance of 0 and will short-circuit the matching loop. |
import { LanguageTag } from '@phensley/language-tag'; | ||
import { Locale } from '@phensley/locale'; | ||
declare class Entry implements Locale { | ||
readonly id: string; | ||
readonly tag: LanguageTag; | ||
readonly compact: string; | ||
constructor(id: string, tag: LanguageTag); | ||
} | ||
declare type LangTag = Locale | LanguageTag | string; | ||
/** | ||
* Flatten and split the string or array into a list of matcher entries. | ||
* | ||
* Visible for testing. | ||
*/ | ||
export declare const parse: (locales?: string | LangTag[], options?: LocaleMatcherOptions) => Entry[]; | ||
/** | ||
* A result returned by the LocaleMatcher. | ||
@@ -30,2 +43,12 @@ * | ||
/** | ||
* Sort the supported locale entries. The result will have the following order: | ||
* | ||
* First: default locale | ||
* Next: all paradigm locales | ||
* Last: all other locales | ||
* | ||
* Visible for testing. | ||
*/ | ||
export declare const sortEntries: (d: Entry) => (a: Entry, b: Entry) => number; | ||
/** | ||
* Given a list of supported locales, and a list of a user's desired locales | ||
@@ -55,1 +78,2 @@ * (sorted in the order of preference, descending), returns the supported | ||
} | ||
export {}; |
@@ -21,4 +21,6 @@ "use strict"; | ||
* Flatten and split the string or array into a list of matcher entries. | ||
* | ||
* Visible for testing. | ||
*/ | ||
var parse = function (locales, options) { | ||
exports.parse = function (locales, options) { | ||
if (locales === void 0) { locales = []; } | ||
@@ -88,2 +90,32 @@ if (options === void 0) { options = {}; } | ||
/** | ||
* Sort the supported locale entries. The result will have the following order: | ||
* | ||
* First: default locale | ||
* Next: all paradigm locales | ||
* Last: all other locales | ||
* | ||
* Visible for testing. | ||
*/ | ||
exports.sortEntries = function (d) { return function (a, b) { | ||
// Check if entry is our default tag, to keep it at the front of the array. | ||
if (a.tag === d.tag) { | ||
return -1; | ||
} | ||
if (b.tag === d.tag) { | ||
return 1; | ||
} | ||
// Sort all paradigm locales before non-paradigms. | ||
var pa = autogen_distance_1.paradigmLocales[a.compact]; | ||
var pb = autogen_distance_1.paradigmLocales[b.compact]; | ||
console.log(a.compact, pa, ' ', b.compact, pb); | ||
if (pa !== undefined) { | ||
return pb === U ? -1 : numberCmp(pa, pb); | ||
} | ||
else if (pb !== undefined) { | ||
return 1; | ||
} | ||
// All other locales stay in their relative positions. | ||
return 0; | ||
}; }; | ||
/** | ||
* Given a list of supported locales, and a list of a user's desired locales | ||
@@ -105,3 +137,3 @@ * (sorted in the order of preference, descending), returns the supported | ||
this.exactMap = {}; | ||
this.supported = parse(supportedLocales, options); | ||
this.supported = exports.parse(supportedLocales, options); | ||
this.count = this.supported.length; | ||
@@ -113,23 +145,3 @@ if (!this.count) { | ||
this.default = this.supported[0]; | ||
this.supported.sort(function (a, b) { | ||
// Keep default tag at the front. | ||
if (a.tag === _this.default.tag) { | ||
return -1; | ||
} | ||
/* istanbul ignore if */ | ||
if (b.tag === _this.default.tag) { | ||
return 1; | ||
} | ||
// Sort all paradigm locales before non-paradigms. | ||
var pa = autogen_distance_1.paradigmLocales[a.compact]; | ||
var pb = autogen_distance_1.paradigmLocales[b.compact]; | ||
if (pa !== undefined) { | ||
return pb === U ? -1 : numberCmp(pa, pb); | ||
} | ||
else if (pb !== undefined) { | ||
return 1; | ||
} | ||
// All other locales stay in their relative positions. | ||
return 0; | ||
}); | ||
this.supported.sort(exports.sortEntries(this.default)); | ||
// Wire up a map for quick lookups of exact matches. These have a | ||
@@ -156,3 +168,3 @@ // distance of 0 and will short-circuit the matching loop. | ||
if (threshold === void 0) { threshold = distance_1.DEFAULT_THRESHOLD; } | ||
var desireds = parse(desiredLocales); | ||
var desireds = exports.parse(desiredLocales); | ||
var len = desireds.length; | ||
@@ -159,0 +171,0 @@ var bestDistance = distance_1.MAX_DISTANCE; |
{ | ||
"name": "@phensley/locale-matcher", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Implements CLDR enhanced language matching", | ||
@@ -39,19 +39,19 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@phensley/language-tag": "1.0.7", | ||
"@phensley/locale": "1.0.7", | ||
"tslib": "1.11.1" | ||
"@phensley/language-tag": "1.0.8", | ||
"@phensley/locale": "1.0.8", | ||
"tslib": "^1.11.1" | ||
}, | ||
"devDependencies": { | ||
"@microsoft/api-extractor": "^7.7.8", | ||
"@types/jest": "25.1.3", | ||
"@types/node": "^10.14.10", | ||
"@types/jest": "^25.1.3", | ||
"@types/node": "^13.9.2", | ||
"beautify-benchmark": "^0.2.4", | ||
"benchmark": "^2.1.4", | ||
"chalk": "^2.3.2", | ||
"jest": "25.1.0", | ||
"jest": "^25.1.0", | ||
"rimraf": "^3.0.0", | ||
"ts-jest": "25.2.1", | ||
"tslint": "^5.18.0", | ||
"ts-jest": "^25.2.1", | ||
"tslint": "^6.1.0", | ||
"tslint-no-circular-imports": "^0.7.0", | ||
"typescript": "3.8.x" | ||
"typescript": "^3.8.x" | ||
}, | ||
@@ -71,3 +71,3 @@ "jest": { | ||
}, | ||
"gitHead": "f411d4925d2d57589ef2cd9f1c13ef6871224bc3" | ||
"gitHead": "42451ff35e9b5e3abeb04d2fd71ae0fc0639a82a" | ||
} |
@@ -1,4 +0,41 @@ | ||
[`@phensley/locale-matcher`](https://www.npmjs.com/package/@phensley/locale-matcher)  | ||
# @phensley/locale-matcher | ||
[](https://www.npmjs.com/package/@phensley/locale-matcher) [](https://bundlephobia.com/result?p=@phensley/locale-matcher) | ||
Implements distance based locale matching using the CLDR enhanced language matching algorithm. | ||
## Installation | ||
NPM: | ||
``` | ||
npm install --save @phensley/locale-matcher | ||
``` | ||
Yarn: | ||
``` | ||
yarn add @phensley/locale-matcher | ||
``` | ||
## Examples | ||
```typescript | ||
import { LocaleMatch, LocaleMatcher } from '@phensley/locale-matcher'; | ||
// Add supported locales to matcher | ||
const matcher = new LocaleMatcher('en, en_GB, zh, pt_AR, es-419'); | ||
let m: LocaleMatch; | ||
// Query desired locales to find the nearest match | ||
m = matcher.match('en-AU'); | ||
console.log(`distance ${m.distance} locale ${m.locale.id}`); | ||
m = matcher.match('es-MX'); | ||
console.log(`distance ${m.distance} locale ${m.locale.id}`); | ||
``` | ||
``` | ||
distance 3 locale en_GB | ||
distance 4 locale es-419 | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
233209
1107
42
+ Added@phensley/language-tag@1.0.8(transitive)
+ Added@phensley/locale@1.0.8(transitive)
+ Addedtslib@1.14.1(transitive)
- Removed@phensley/language-tag@1.0.7(transitive)
- Removed@phensley/locale@1.0.7(transitive)
- Removedtslib@1.11.1(transitive)
Updated@phensley/language-tag@1.0.8
Updated@phensley/locale@1.0.8
Updatedtslib@^1.11.1