@formatjs/intl-localematcher 0.6.2 → 0.7.0

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.
Files changed (54) hide show
  1. package/abstract/BestAvailableLocale.js +1 -4
  2. package/abstract/BestFitMatcher.d.ts +1 -1
  3. package/abstract/BestFitMatcher.js +4 -7
  4. package/abstract/CanonicalizeLocaleList.js +1 -4
  5. package/abstract/CanonicalizeUValue.js +3 -6
  6. package/abstract/CanonicalizeUnicodeLocaleId.js +1 -4
  7. package/abstract/InsertUnicodeExtensionAndCanonicalize.d.ts +1 -1
  8. package/abstract/InsertUnicodeExtensionAndCanonicalize.js +6 -9
  9. package/abstract/LookupMatcher.d.ts +1 -1
  10. package/abstract/LookupMatcher.js +5 -8
  11. package/abstract/LookupSupportedLocales.js +5 -8
  12. package/abstract/ResolveLocale.js +15 -18
  13. package/abstract/UnicodeExtensionComponents.d.ts +1 -1
  14. package/abstract/UnicodeExtensionComponents.js +6 -9
  15. package/abstract/UnicodeExtensionValue.js +3 -6
  16. package/abstract/languageMatching.js +1 -4
  17. package/abstract/regions.generated.js +1 -4
  18. package/abstract/types.js +1 -2
  19. package/abstract/utils.js +13 -19
  20. package/index.d.ts +2 -2
  21. package/index.js +6 -12
  22. package/package.json +6 -3
  23. package/lib/abstract/BestAvailableLocale.d.ts +0 -6
  24. package/lib/abstract/BestAvailableLocale.js +0 -21
  25. package/lib/abstract/BestFitMatcher.d.ts +0 -8
  26. package/lib/abstract/BestFitMatcher.js +0 -31
  27. package/lib/abstract/CanonicalizeLocaleList.d.ts +0 -5
  28. package/lib/abstract/CanonicalizeLocaleList.js +0 -7
  29. package/lib/abstract/CanonicalizeUValue.d.ts +0 -1
  30. package/lib/abstract/CanonicalizeUValue.js +0 -8
  31. package/lib/abstract/CanonicalizeUnicodeLocaleId.d.ts +0 -1
  32. package/lib/abstract/CanonicalizeUnicodeLocaleId.js +0 -3
  33. package/lib/abstract/InsertUnicodeExtensionAndCanonicalize.d.ts +0 -2
  34. package/lib/abstract/InsertUnicodeExtensionAndCanonicalize.js +0 -32
  35. package/lib/abstract/LookupMatcher.d.ts +0 -8
  36. package/lib/abstract/LookupMatcher.js +0 -25
  37. package/lib/abstract/LookupSupportedLocales.d.ts +0 -6
  38. package/lib/abstract/LookupSupportedLocales.js +0 -19
  39. package/lib/abstract/ResolveLocale.d.ts +0 -14
  40. package/lib/abstract/ResolveLocale.js +0 -100
  41. package/lib/abstract/UnicodeExtensionComponents.d.ts +0 -5
  42. package/lib/abstract/UnicodeExtensionComponents.js +0 -42
  43. package/lib/abstract/UnicodeExtensionValue.d.ts +0 -6
  44. package/lib/abstract/UnicodeExtensionValue.js +0 -46
  45. package/lib/abstract/languageMatching.d.ts +0 -2240
  46. package/lib/abstract/languageMatching.js +0 -2620
  47. package/lib/abstract/regions.generated.d.ts +0 -1
  48. package/lib/abstract/regions.generated.js +0 -1334
  49. package/lib/abstract/types.d.ts +0 -9
  50. package/lib/abstract/types.js +0 -1
  51. package/lib/abstract/utils.d.ts +0 -10
  52. package/lib/abstract/utils.js +0 -165
  53. package/lib/index.d.ts +0 -6
  54. package/lib/index.js +0 -9
@@ -1,9 +0,0 @@
1
- export interface LookupMatcherResult {
2
- locale: string;
3
- extension?: string;
4
- nu?: string;
5
- }
6
- export interface Keyword {
7
- key: string;
8
- value: string;
9
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,10 +0,0 @@
1
- export declare const UNICODE_EXTENSION_SEQUENCE_REGEX: RegExp;
2
- export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
3
- export declare function findMatchingDistance(desired: string, supported: string): number;
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 {};
@@ -1,165 +0,0 @@
1
- import { __spreadArray } from "tslib";
2
- import { data as jsonData } from './languageMatching';
3
- import { regions } from './regions.generated';
4
- export var UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi;
5
- export function invariant(condition, message, Err) {
6
- if (Err === void 0) { Err = Error; }
7
- if (!condition) {
8
- throw new Err(message);
9
- }
10
- }
11
- // This is effectively 2 languages in 2 different regions in the same cluster
12
- var DEFAULT_MATCHING_THRESHOLD = 838;
13
- var PROCESSED_DATA;
14
- function processData() {
15
- var _a, _b;
16
- if (!PROCESSED_DATA) {
17
- 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(' ');
18
- var matchVariables = jsonData.supplemental.languageMatching['written-new'].slice(1, 5);
19
- var data = jsonData.supplemental.languageMatching['written-new'].slice(5);
20
- var matches = data.map(function (d) {
21
- var key = Object.keys(d)[0];
22
- var value = d[key];
23
- return {
24
- supported: key,
25
- desired: value._desired,
26
- distance: +value._distance,
27
- oneway: value.oneway === 'true' ? true : false,
28
- };
29
- }, {});
30
- PROCESSED_DATA = {
31
- matches: matches,
32
- matchVariables: matchVariables.reduce(function (all, d) {
33
- var key = Object.keys(d)[0];
34
- var value = d[key];
35
- all[key.slice(1)] = value._value.split('+');
36
- return all;
37
- }, {}),
38
- paradigmLocales: __spreadArray(__spreadArray([], paradigmLocales, true), paradigmLocales.map(function (l) {
39
- return new Intl.Locale(l.replace(/_/g, '-')).maximize().toString();
40
- }), true),
41
- };
42
- }
43
- return PROCESSED_DATA;
44
- }
45
- function isMatched(locale, languageMatchInfoLocale, matchVariables) {
46
- var _a = languageMatchInfoLocale.split('-'), language = _a[0], script = _a[1], region = _a[2];
47
- var matches = true;
48
- if (region && region[0] === '$') {
49
- var shouldInclude = region[1] !== '!';
50
- var matchRegions = shouldInclude
51
- ? matchVariables[region.slice(1)]
52
- : matchVariables[region.slice(2)];
53
- var expandedMatchedRegions = matchRegions
54
- .map(function (r) { return regions[r] || [r]; })
55
- .reduce(function (all, list) { return __spreadArray(__spreadArray([], all, true), list, true); }, []);
56
- matches && (matches = !(expandedMatchedRegions.indexOf(locale.region || '') > -1 !=
57
- shouldInclude));
58
- }
59
- else {
60
- matches && (matches = locale.region
61
- ? region === '*' || region === locale.region
62
- : true);
63
- }
64
- matches && (matches = locale.script ? script === '*' || script === locale.script : true);
65
- matches && (matches = locale.language
66
- ? language === '*' || language === locale.language
67
- : true);
68
- return matches;
69
- }
70
- function serializeLSR(lsr) {
71
- return [lsr.language, lsr.script, lsr.region].filter(Boolean).join('-');
72
- }
73
- function findMatchingDistanceForLSR(desired, supported, data) {
74
- for (var _i = 0, _a = data.matches; _i < _a.length; _i++) {
75
- var d = _a[_i];
76
- var matches = isMatched(desired, d.desired, data.matchVariables) &&
77
- isMatched(supported, d.supported, data.matchVariables);
78
- if (!d.oneway && !matches) {
79
- matches =
80
- isMatched(desired, d.supported, data.matchVariables) &&
81
- isMatched(supported, d.desired, data.matchVariables);
82
- }
83
- if (matches) {
84
- var distance = d.distance * 10;
85
- if (data.paradigmLocales.indexOf(serializeLSR(desired)) > -1 !=
86
- data.paradigmLocales.indexOf(serializeLSR(supported)) > -1) {
87
- return distance - 1;
88
- }
89
- return distance;
90
- }
91
- }
92
- throw new Error('No matching distance found');
93
- }
94
- export function findMatchingDistance(desired, supported) {
95
- var desiredLocale = new Intl.Locale(desired).maximize();
96
- var supportedLocale = new Intl.Locale(supported).maximize();
97
- var desiredLSR = {
98
- language: desiredLocale.language,
99
- script: desiredLocale.script || '',
100
- region: desiredLocale.region || '',
101
- };
102
- var supportedLSR = {
103
- language: supportedLocale.language,
104
- script: supportedLocale.script || '',
105
- region: supportedLocale.region || '',
106
- };
107
- var matchingDistance = 0;
108
- var data = processData();
109
- if (desiredLSR.language !== supportedLSR.language) {
110
- matchingDistance += findMatchingDistanceForLSR({
111
- language: desiredLocale.language,
112
- script: '',
113
- region: '',
114
- }, {
115
- language: supportedLocale.language,
116
- script: '',
117
- region: '',
118
- }, data);
119
- }
120
- if (desiredLSR.script !== supportedLSR.script) {
121
- matchingDistance += findMatchingDistanceForLSR({
122
- language: desiredLocale.language,
123
- script: desiredLSR.script,
124
- region: '',
125
- }, {
126
- language: supportedLocale.language,
127
- script: supportedLSR.script,
128
- region: '',
129
- }, data);
130
- }
131
- if (desiredLSR.region !== supportedLSR.region) {
132
- matchingDistance += findMatchingDistanceForLSR(desiredLSR, supportedLSR, data);
133
- }
134
- return matchingDistance;
135
- }
136
- export function findBestMatch(requestedLocales, supportedLocales, threshold) {
137
- if (threshold === void 0) { threshold = DEFAULT_MATCHING_THRESHOLD; }
138
- var lowestDistance = Infinity;
139
- var result = {
140
- matchedDesiredLocale: '',
141
- distances: {},
142
- };
143
- requestedLocales.forEach(function (desired, i) {
144
- if (!result.distances[desired]) {
145
- result.distances[desired] = {};
146
- }
147
- supportedLocales.forEach(function (supported) {
148
- // Add some weight to the distance based on the order of the supported locales
149
- // Add penalty for the order of the requested locales, which currently is 0 since ECMA-402
150
- // doesn't really have room for weighted locales like `en; q=0.1`
151
- var distance = findMatchingDistance(desired, supported) + 0 + i * 40;
152
- result.distances[desired][supported] = distance;
153
- if (distance < lowestDistance) {
154
- lowestDistance = distance;
155
- result.matchedDesiredLocale = desired;
156
- result.matchedSupportedLocale = supported;
157
- }
158
- });
159
- });
160
- if (lowestDistance >= threshold) {
161
- result.matchedDesiredLocale = undefined;
162
- result.matchedSupportedLocale = undefined;
163
- }
164
- return result;
165
- }
package/lib/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export interface Opts {
2
- algorithm: 'lookup' | 'best fit';
3
- }
4
- export declare function match(requestedLocales: readonly string[], availableLocales: readonly string[], defaultLocale: string, opts?: Opts): string;
5
- export { LookupSupportedLocales } from './abstract/LookupSupportedLocales';
6
- export { ResolveLocale } from './abstract/ResolveLocale';
package/lib/index.js DELETED
@@ -1,9 +0,0 @@
1
- import { CanonicalizeLocaleList } from './abstract/CanonicalizeLocaleList';
2
- import { ResolveLocale } from './abstract/ResolveLocale';
3
- export function match(requestedLocales, availableLocales, defaultLocale, opts) {
4
- return ResolveLocale(availableLocales, CanonicalizeLocaleList(requestedLocales), {
5
- localeMatcher: (opts === null || opts === void 0 ? void 0 : opts.algorithm) || 'best fit',
6
- }, [], {}, function () { return defaultLocale; }).locale;
7
- }
8
- export { LookupSupportedLocales } from './abstract/LookupSupportedLocales';
9
- export { ResolveLocale } from './abstract/ResolveLocale';