@formatjs/intl-getcanonicallocales 3.3.1 → 3.4.1

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.
package/index.d.ts CHANGED
@@ -40,5 +40,4 @@ export declare function isUnicodeVariantSubtag(variant: string): boolean;
40
40
  export declare function parseUnicodeLanguageId(chunks: string[] | string): UnicodeLanguageId;
41
41
  export declare function parseUnicodeLocaleId(locale: string): UnicodeLocaleId;
42
42
  export declare const likelySubtags: Record<string, string>;
43
- export declare function getCanonicalLocales(locales?: string[] | string | Intl.Locale | Intl.Locale[] | ArrayLike<string | Intl.Locale>): string[];
44
- //# sourceMappingURL=index.d.ts.map
43
+ export declare function getCanonicalLocales(locales?: string[] | string | Intl.Locale | Intl.Locale[] | ArrayLike<string | Intl.Locale>): string[];
package/index.js CHANGED
@@ -1,3 +1,84 @@
1
+ // packages/ecma262-abstract/IsObject.js
2
+ /** Includes browser host objects such as document.all. */
3
+ function IsObject(value) {
4
+ return value !== null && (typeof value === "object" || typeof value === "function" || typeof value === "undefined" && value !== void 0);
5
+ }
6
+ // packages/ecma262-abstract/intrinsics.js
7
+ const objectFrom = Object;
8
+ const mathMin = Math.min;
9
+ const mathMax = Math.max;
10
+ const mathTrunc = Math.trunc;
11
+ Number.isInteger;
12
+ Number.isFinite;
13
+ Number.isSafeInteger;
14
+ const TypeErrorConstructor$1 = TypeError;
15
+ const arrayApply = Reflect.apply;
16
+ const every = Array.prototype.every;
17
+ Array.prototype.some;
18
+ function arrayEvery(values, predicate) {
19
+ return arrayApply(every, values, [predicate]);
20
+ }
21
+ const includes = Array.prototype.includes;
22
+ const indexOf = Array.prototype.indexOf;
23
+ function arrayIncludes(values, search) {
24
+ return arrayApply(includes, values, [search]);
25
+ }
26
+ function arrayIndexOf(values, search) {
27
+ return arrayApply(indexOf, values, [search]);
28
+ }
29
+ // packages/intl-getcanonicallocales/locale-brand.ts
30
+ const apply = Reflect.apply;
31
+ const defineProperty = Object.defineProperty;
32
+ let readers;
33
+ function addLocaleBrandReader(read) {
34
+ if (typeof read !== "function") throw new TypeErrorConstructor$1("Locale brand reader must be callable");
35
+ for (let node = readers; node; node = node.next) if (node.read === read) return;
36
+ readers = {
37
+ read,
38
+ next: readers
39
+ };
40
+ }
41
+ function getLocaleBrandReaders() {
42
+ const result = [];
43
+ for (let node = readers; node; node = node.next) defineProperty(result, result.length, {
44
+ value: node.read,
45
+ writable: true,
46
+ enumerable: true,
47
+ configurable: true
48
+ });
49
+ return result;
50
+ }
51
+ function localeString(value) {
52
+ if (!IsObject(value)) return void 0;
53
+ for (let node = readers; node; node = node.next) {
54
+ const tag = node.read(value);
55
+ if (typeof tag === "string") return tag;
56
+ }
57
+ }
58
+ const LocaleConstructor = typeof Intl === "undefined" ? void 0 : Intl.Locale;
59
+ const snapshot = LocaleConstructor?.__getLocaleBrandReaders;
60
+ if (typeof snapshot === "function") {
61
+ const known = apply(snapshot, LocaleConstructor, []);
62
+ for (let index = 0; index < known.length; index++) addLocaleBrandReader(known[index]);
63
+ } else {
64
+ const nativeToString = LocaleConstructor?.prototype.toString;
65
+ if (typeof nativeToString === "function") addLocaleBrandReader((value) => {
66
+ try {
67
+ const tag = apply(nativeToString, value, []);
68
+ return typeof tag === "string" ? tag : void 0;
69
+ } catch {
70
+ return;
71
+ }
72
+ });
73
+ }
74
+ // packages/ecma262-abstract/ToObject.js
75
+ /**
76
+ * https://tc39.es/ecma262/#sec-toobject
77
+ */
78
+ function ToObject(arg) {
79
+ if (arg === void 0 || arg === null) throw new TypeErrorConstructor$1("undefined/null cannot be converted to object");
80
+ return objectFrom(arg);
81
+ }
1
82
  // packages/intl-getcanonicallocales/appendToList.ts
2
83
  function appendToList(list, ...values) {
3
84
  for (const value of values) Object.defineProperty(list, list.length, {
@@ -8,12 +89,14 @@ function appendToList(list, ...values) {
8
89
  });
9
90
  }
10
91
  // packages/ecma262-abstract/ToString.js
92
+ const StringConstructor = String;
93
+ const TypeErrorConstructor = TypeError;
11
94
  /**
12
95
  * https://tc39.es/ecma262/#sec-tostring
13
96
  */
14
97
  function ToString(o) {
15
- if (typeof o === "symbol") throw TypeError("Cannot convert a Symbol value to a string");
16
- return String(o);
98
+ if (typeof o === "symbol") throw TypeErrorConstructor("Cannot convert a Symbol value to a string");
99
+ return StringConstructor(o);
17
100
  }
18
101
  // node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/aliases.js
19
102
  const subdivisionAlias = {
@@ -9494,11 +9577,13 @@ function compareKV(t1, t2) {
9494
9577
  return t1[0] < t2[0] ? -1 : t1[0] > t2[0] ? 1 : 0;
9495
9578
  }
9496
9579
  function compareExtension(e1, e2) {
9580
+ if (e1.type === "x") return e2.type === "x" ? 0 : 1;
9581
+ if (e2.type === "x") return -1;
9497
9582
  return e1.type < e2.type ? -1 : e1.type > e2.type ? 1 : 0;
9498
9583
  }
9499
9584
  function mergeVariants(v1, v2) {
9500
9585
  const result = [...v1];
9501
- for (const v of v2) if (v1.indexOf(v) < 0) appendToList(result, v);
9586
+ for (const v of v2) if (arrayIndexOf(v1, v) < 0) appendToList(result, v);
9502
9587
  return result;
9503
9588
  }
9504
9589
  function compareLanguageAliasRules(a, b) {
@@ -9544,7 +9629,7 @@ function canonicalizeUnicodeLanguageId(unicodeLanguageId) {
9544
9629
  unicodeLanguageId.variants = unicodeLanguageId.variants.map((v) => v.toLowerCase());
9545
9630
  const original = emitUnicodeLanguageId(unicodeLanguageId);
9546
9631
  let finalLangAst = unicodeLanguageId;
9547
- const matches = ({ type }) => (!type.script || type.script === finalLangAst.script) && (!type.region || type.region === finalLangAst.region) && type.variants.every((variant) => finalLangAst.variants.includes(variant));
9632
+ const matches = ({ type }) => (!type.script || type.script === finalLangAst.script) && (!type.region || type.region === finalLangAst.region) && arrayEvery(type.variants, (variant) => arrayIncludes(finalLangAst.variants, variant));
9548
9633
  const languageRule = languageAliasRules[finalLangAst.lang]?.find(matches);
9549
9634
  const undRule = finalLangAst.lang === "und" ? void 0 : languageAliasRules.und?.find(matches);
9550
9635
  const matched = !languageRule ? undRule : !undRule ? languageRule : compareLanguageAliasRules(languageRule, undRule) <= 0 ? languageRule : undRule;
@@ -9554,7 +9639,7 @@ function canonicalizeUnicodeLanguageId(unicodeLanguageId) {
9554
9639
  lang: type.lang !== "und" || finalLangAst.lang === "und" ? replacement.lang : finalLangAst.lang,
9555
9640
  script: type.script ? replacement.script : finalLangAst.script || replacement.script,
9556
9641
  region: type.region ? replacement.region : finalLangAst.region || replacement.region,
9557
- variants: mergeVariants(finalLangAst.variants.filter((v) => !type.variants.includes(v)), replacement.variants)
9642
+ variants: mergeVariants(finalLangAst.variants.filter((v) => !arrayIncludes(type.variants, v)), replacement.variants)
9558
9643
  };
9559
9644
  return canonicalizeUnicodeLanguageId(finalLangAst);
9560
9645
  }
@@ -9572,7 +9657,7 @@ function canonicalizeUnicodeLanguageId(unicodeLanguageId) {
9572
9657
  })] || likelySubtags[finalLangAst.lang];
9573
9658
  if (likelySubtag) {
9574
9659
  const { region: likelyRegion } = parseUnicodeLanguageId(likelySubtag.split("-"));
9575
- if (likelyRegion && regions.indexOf(likelyRegion) > -1) replacedRegion = likelyRegion;
9660
+ if (likelyRegion && arrayIndexOf(regions, likelyRegion) > -1) replacedRegion = likelyRegion;
9576
9661
  }
9577
9662
  }
9578
9663
  if (replacedRegion) finalLangAst.region = replacedRegion;
@@ -9621,15 +9706,6 @@ function CanonicalizeUnicodeLocaleId(locale) {
9621
9706
  return locale;
9622
9707
  }
9623
9708
  // packages/intl-getcanonicallocales/index.ts
9624
- const originalLocaleToString = typeof Intl !== "undefined" ? Intl.Locale?.prototype.toString : void 0;
9625
- function localeString(value) {
9626
- if (value === null || typeof value !== "object" && typeof value !== "function") return void 0;
9627
- const currentLocaleToString = typeof Intl !== "undefined" ? Intl.Locale?.prototype.toString : void 0;
9628
- for (const method of [originalLocaleToString, currentLocaleToString]) if (method) try {
9629
- const tag = method.call(value);
9630
- if (typeof tag === "string") return tag;
9631
- } catch {}
9632
- }
9633
9709
  /**
9634
9710
  * ECMA-402 §9.2.1 CanonicalizeLocaleList, steps 3–7.
9635
9711
  * https://tc39.es/ecma402/#sec-canonicalizelocalelist
@@ -9637,28 +9713,28 @@ function localeString(value) {
9637
9713
  */
9638
9714
  function CanonicalizeLocaleList(locales) {
9639
9715
  if (locales === void 0) return [];
9640
- if (locales === null) throw new TypeError("Cannot convert null to an object");
9716
+ if (locales === null) throw new TypeErrorConstructor$1("Cannot convert null to an object");
9641
9717
  const singleLocale = localeString(locales);
9642
- const list = typeof locales === "string" ? [locales] : singleLocale !== void 0 ? [singleLocale] : Object(locales);
9718
+ const list = typeof locales === "string" ? [locales] : singleLocale !== void 0 ? [singleLocale] : ToObject(locales);
9643
9719
  const length = +list.length;
9644
- const len = isNaN(length) ? 0 : Math.min(Math.max(Math.floor(length), 0), 9007199254740991);
9720
+ const len = length !== length ? 0 : mathMin(mathMax(mathTrunc(length), 0), 9007199254740991);
9645
9721
  const seen = [];
9646
9722
  for (let k = 0; k < len; k++) {
9647
9723
  if (!(k in list)) continue;
9648
9724
  const value = list[k];
9649
- if (value === null || ![
9650
- "string",
9651
- "object",
9652
- "function"
9653
- ].includes(typeof value)) throw new TypeError("Locale list entries must be strings or objects");
9725
+ if (typeof value !== "string" && !IsObject(value)) throw new TypeErrorConstructor$1("Locale list entries must be strings or objects");
9654
9726
  const canonicalizedTag = emitUnicodeLocaleId(CanonicalizeUnicodeLocaleId(parseUnicodeLocaleId(localeString(value) ?? ToString(value))));
9655
- if (seen.indexOf(canonicalizedTag) < 0) appendToList(seen, canonicalizedTag);
9727
+ if (arrayIndexOf(seen, canonicalizedTag) < 0) appendToList(seen, canonicalizedTag);
9656
9728
  }
9657
9729
  return seen;
9658
9730
  }
9659
9731
  function getCanonicalLocales(locales) {
9660
9732
  return CanonicalizeLocaleList(locales);
9661
9733
  }
9734
+ Object.defineProperties(getCanonicalLocales, {
9735
+ __addLocaleBrandReader: { value: addLocaleBrandReader },
9736
+ __getLocaleBrandReaders: { value: getLocaleBrandReaders }
9737
+ });
9662
9738
  export { emitUnicodeLanguageId, emitUnicodeLocaleId, getCanonicalLocales, isStructurallyValidLanguageTag, isUnicodeLanguageSubtag, isUnicodeRegionSubtag, isUnicodeScriptSubtag, isUnicodeVariantSubtag, likelySubtags, parseUnicodeLanguageId, parseUnicodeLocaleId };
9663
9739
 
9664
9740
  //# sourceMappingURL=index.js.map