@formatjs/intl-localematcher 0.8.2 → 0.8.4

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 (34) hide show
  1. package/index.d.ts +29 -5
  2. package/index.js +3723 -5
  3. package/index.js.map +1 -0
  4. package/package.json +2 -2
  5. package/abstract/BestAvailableLocale.d.ts +0 -6
  6. package/abstract/BestAvailableLocale.js +0 -29
  7. package/abstract/BestFitMatcher.d.ts +0 -8
  8. package/abstract/BestFitMatcher.js +0 -30
  9. package/abstract/CanonicalizeLocaleList.d.ts +0 -5
  10. package/abstract/CanonicalizeLocaleList.js +0 -7
  11. package/abstract/CanonicalizeUValue.d.ts +0 -1
  12. package/abstract/CanonicalizeUValue.js +0 -8
  13. package/abstract/CanonicalizeUnicodeLocaleId.d.ts +0 -1
  14. package/abstract/CanonicalizeUnicodeLocaleId.js +0 -3
  15. package/abstract/InsertUnicodeExtensionAndCanonicalize.d.ts +0 -2
  16. package/abstract/InsertUnicodeExtensionAndCanonicalize.js +0 -29
  17. package/abstract/LookupMatcher.d.ts +0 -8
  18. package/abstract/LookupMatcher.js +0 -24
  19. package/abstract/LookupSupportedLocales.d.ts +0 -6
  20. package/abstract/LookupSupportedLocales.js +0 -18
  21. package/abstract/ResolveLocale.d.ts +0 -15
  22. package/abstract/ResolveLocale.js +0 -95
  23. package/abstract/UnicodeExtensionComponents.d.ts +0 -5
  24. package/abstract/UnicodeExtensionComponents.js +0 -44
  25. package/abstract/UnicodeExtensionValue.d.ts +0 -6
  26. package/abstract/UnicodeExtensionValue.js +0 -43
  27. package/abstract/languageMatching.d.ts +0 -2240
  28. package/abstract/languageMatching.js +0 -1846
  29. package/abstract/regions.generated.d.ts +0 -1
  30. package/abstract/regions.generated.js +0 -1334
  31. package/abstract/types.d.ts +0 -9
  32. package/abstract/types.js +0 -1
  33. package/abstract/utils.d.ts +0 -46
  34. package/abstract/utils.js +0 -349
@@ -1,15 +0,0 @@
1
- export interface ResolveLocaleResult {
2
- locale: string;
3
- dataLocale: string;
4
- [k: string]: any;
5
- }
6
- /**
7
- * https://tc39.es/ecma402/#sec-resolvelocale
8
- */
9
- export declare function ResolveLocale<
10
- K extends string,
11
- D extends { [k in K] : any }
12
- >(availableLocales: Set<string> | readonly string[], requestedLocales: readonly string[], options: {
13
- localeMatcher: string;
14
- [k: string]: string;
15
- }, relevantExtensionKeys: K[], localeData: Record<string, D | undefined>, getDefaultLocale: () => string): ResolveLocaleResult;
@@ -1,95 +0,0 @@
1
- import { BestFitMatcher } from "./BestFitMatcher.js";
2
- import { CanonicalizeUValue } from "./CanonicalizeUValue.js";
3
- import { InsertUnicodeExtensionAndCanonicalize } from "./InsertUnicodeExtensionAndCanonicalize.js";
4
- import { LookupMatcher } from "./LookupMatcher.js";
5
- import { UnicodeExtensionComponents } from "./UnicodeExtensionComponents.js";
6
- import { invariant } from "./utils.js";
7
- /**
8
- * https://tc39.es/ecma402/#sec-resolvelocale
9
- */
10
- export function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
11
- const matcher = options.localeMatcher;
12
- let r;
13
- if (matcher === "lookup") {
14
- r = LookupMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
15
- } else {
16
- r = BestFitMatcher(Array.from(availableLocales), requestedLocales, getDefaultLocale);
17
- }
18
- if (r == null) {
19
- r = {
20
- locale: getDefaultLocale(),
21
- extension: ""
22
- };
23
- }
24
- let foundLocale = r.locale;
25
- let foundLocaleData = localeData[foundLocale];
26
- // TODO: We can't really guarantee that the locale data is available
27
- // invariant(
28
- // foundLocaleData !== undefined,
29
- // `Missing locale data for ${foundLocale}`
30
- // )
31
- const result = {
32
- locale: "en",
33
- dataLocale: foundLocale
34
- };
35
- let components;
36
- let keywords;
37
- if (r.extension) {
38
- components = UnicodeExtensionComponents(r.extension);
39
- keywords = components.keywords;
40
- } else {
41
- keywords = [];
42
- }
43
- let supportedKeywords = [];
44
- for (const key of relevantExtensionKeys) {
45
- // TODO: Shouldn't default to empty array, see TODO above
46
- let keyLocaleData = foundLocaleData?.[key] ?? [];
47
- invariant(Array.isArray(keyLocaleData), `keyLocaleData for ${key} must be an array`);
48
- let value = keyLocaleData[0];
49
- invariant(value === undefined || typeof value === "string", `value must be a string or undefined`);
50
- let supportedKeyword;
51
- let entry = keywords.find((k) => k.key === key);
52
- if (entry) {
53
- let requestedValue = entry.value;
54
- if (requestedValue !== "") {
55
- if (keyLocaleData.indexOf(requestedValue) > -1) {
56
- value = requestedValue;
57
- supportedKeyword = {
58
- key,
59
- value
60
- };
61
- }
62
- } else if (keyLocaleData.indexOf("true") > -1) {
63
- value = "true";
64
- supportedKeyword = {
65
- key,
66
- value
67
- };
68
- }
69
- }
70
- let optionsValue = options[key];
71
- invariant(optionsValue == null || typeof optionsValue === "string", `optionsValue must be a string or undefined`);
72
- if (typeof optionsValue === "string") {
73
- let ukey = key.toLowerCase();
74
- optionsValue = CanonicalizeUValue(ukey, optionsValue);
75
- if (optionsValue === "") {
76
- optionsValue = "true";
77
- }
78
- }
79
- if (optionsValue !== value && keyLocaleData.indexOf(optionsValue) > -1) {
80
- value = optionsValue;
81
- supportedKeyword = undefined;
82
- }
83
- if (supportedKeyword) {
84
- supportedKeywords.push(supportedKeyword);
85
- }
86
- result[key] = value;
87
- }
88
- let supportedAttributes = [];
89
- if (supportedKeywords.length > 0) {
90
- supportedAttributes = [];
91
- foundLocale = InsertUnicodeExtensionAndCanonicalize(foundLocale, supportedAttributes, supportedKeywords);
92
- }
93
- result.locale = foundLocale;
94
- return result;
95
- }
@@ -1,5 +0,0 @@
1
- import type { Keyword } from "./types.js";
2
- export declare function UnicodeExtensionComponents(extension: string): {
3
- attributes: string[];
4
- keywords: Array<Keyword>;
5
- };
@@ -1,44 +0,0 @@
1
- import { invariant } from "./utils.js";
2
- export function UnicodeExtensionComponents(extension) {
3
- invariant(extension === extension.toLowerCase(), "Expected extension to be lowercase");
4
- invariant(extension.slice(0, 3) === "-u-", "Expected extension to be a Unicode locale extension");
5
- const attributes = [];
6
- const keywords = [];
7
- let keyword;
8
- let size = extension.length;
9
- let k = 3;
10
- while (k < size) {
11
- let e = extension.indexOf("-", k);
12
- let len;
13
- if (e === -1) {
14
- len = size - k;
15
- } else {
16
- len = e - k;
17
- }
18
- let subtag = extension.slice(k, k + len);
19
- invariant(len >= 2, "Expected a subtag to have at least 2 characters");
20
- if (keyword === undefined && len != 2) {
21
- if (attributes.indexOf(subtag) === -1) {
22
- attributes.push(subtag);
23
- }
24
- } else if (len === 2) {
25
- keyword = {
26
- key: subtag,
27
- value: ""
28
- };
29
- if (keywords.find((k) => k.key === keyword?.key) === undefined) {
30
- keywords.push(keyword);
31
- }
32
- } else if (keyword?.value === "") {
33
- keyword.value = subtag;
34
- } else {
35
- invariant(keyword !== undefined, "Expected keyword to be defined");
36
- keyword.value += "-" + subtag;
37
- }
38
- k += len + 1;
39
- }
40
- return {
41
- attributes,
42
- keywords
43
- };
44
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * https://tc39.es/ecma402/#sec-unicodeextensionvalue
3
- * @param extension
4
- * @param key
5
- */
6
- export declare function UnicodeExtensionValue(extension: string, key: string): string | undefined;
@@ -1,43 +0,0 @@
1
- import { invariant } from "./utils.js";
2
- /**
3
- * https://tc39.es/ecma402/#sec-unicodeextensionvalue
4
- * @param extension
5
- * @param key
6
- */
7
- export function UnicodeExtensionValue(extension, key) {
8
- invariant(key.length === 2, "key must have 2 elements");
9
- const size = extension.length;
10
- let searchValue = `-${key}-`;
11
- let pos = extension.indexOf(searchValue);
12
- if (pos !== -1) {
13
- const start = pos + 4;
14
- let end = start;
15
- let k = start;
16
- let done = false;
17
- while (!done) {
18
- const e = extension.indexOf("-", k);
19
- let len;
20
- if (e === -1) {
21
- len = size - k;
22
- } else {
23
- len = e - k;
24
- }
25
- if (len === 2) {
26
- done = true;
27
- } else if (e === -1) {
28
- end = size;
29
- done = true;
30
- } else {
31
- end = e;
32
- k = e + 1;
33
- }
34
- }
35
- return extension.slice(start, end);
36
- }
37
- searchValue = `-${key}`;
38
- pos = extension.indexOf(searchValue);
39
- if (pos !== -1 && pos + 3 === size) {
40
- return "";
41
- }
42
- return undefined;
43
- }