@formatjs/intl-localematcher 0.8.13 → 0.9.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.
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * @param availableLocales
5
5
  * @param requestedLocales
6
6
  */
7
- declare function LookupSupportedLocales(availableLocales: string[], requestedLocales: string[]): string[];
7
+ export declare function LookupSupportedLocales(availableLocales: string[], requestedLocales: string[]): string[];
8
8
  //#endregion
9
9
  //#region packages/intl-localematcher/abstract/ResolveLocale.d.ts
10
10
  interface ResolveLocaleResult {
@@ -15,16 +15,15 @@ interface ResolveLocaleResult {
15
15
  /**
16
16
  * https://tc39.es/ecma402/#sec-resolvelocale
17
17
  */
18
- declare function ResolveLocale<K extends string, D extends { [k in K]: any; }>(availableLocales: Set<string> | readonly string[], requestedLocales: readonly string[], options: {
18
+ export declare function ResolveLocale<K extends string, D extends { [k in K]: any; }>(availableLocales: Set<string> | readonly string[], requestedLocales: readonly string[], options: {
19
19
  localeMatcher: string;
20
- [k: string]: string;
20
+ [k: string]: string | null | undefined;
21
21
  }, relevantExtensionKeys: K[], localeData: Record<string, D | undefined>, getDefaultLocale: () => string): ResolveLocaleResult;
22
22
  //#endregion
23
23
  //#region packages/intl-localematcher/index.d.ts
24
- interface Opts {
24
+ export interface Opts {
25
25
  algorithm: "lookup" | "best fit";
26
26
  }
27
- declare function match(requestedLocales: readonly string[], availableLocales: readonly string[], defaultLocale: string, opts?: Opts): string;
27
+ export declare function match(requestedLocales: readonly string[], availableLocales: readonly string[], defaultLocale: string, opts?: Opts): string;
28
28
  //#endregion
29
- export { LookupSupportedLocales, Opts, ResolveLocale, match };
30
29
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -3192,7 +3192,20 @@ const regions = {
3192
3192
  };
3193
3193
  //#endregion
3194
3194
  //#region packages/intl-localematcher/abstract/utils.ts
3195
- const UNICODE_EXTENSION_SEQUENCE_REGEX = /-u(?:-[0-9a-z]{2,8})+/gi;
3195
+ function splitUnicodeExtension(locale) {
3196
+ const subtags = locale.split("-");
3197
+ for (let i = 1; i < subtags.length; i++) {
3198
+ if (subtags[i] === "x") break;
3199
+ if (subtags[i] !== "u") continue;
3200
+ let end = i + 1;
3201
+ while (end < subtags.length && subtags[end].length > 1) end++;
3202
+ return {
3203
+ locale: [...subtags.slice(0, i), ...subtags.slice(end)].join("-"),
3204
+ extension: "-" + subtags.slice(i, end).join("-")
3205
+ };
3206
+ }
3207
+ return { locale };
3208
+ }
3196
3209
  /**
3197
3210
  * Asserts that a condition is true, throwing an error if it is not.
3198
3211
  * Used for runtime validation and type narrowing.
@@ -3494,7 +3507,7 @@ function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
3494
3507
  let extension;
3495
3508
  const noExtensionLocales = [];
3496
3509
  const noExtensionLocaleMap = requestedLocales.reduce((all, l) => {
3497
- const noExtensionLocale = l.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, "");
3510
+ const { locale: noExtensionLocale } = splitUnicodeExtension(l);
3498
3511
  noExtensionLocales.push(noExtensionLocale);
3499
3512
  all[noExtensionLocale] = l;
3500
3513
  return all;
@@ -3502,7 +3515,7 @@ function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
3502
3515
  const result = findBestMatch(noExtensionLocales, availableLocales);
3503
3516
  if (result.matchedSupportedLocale && result.matchedDesiredLocale) {
3504
3517
  foundLocale = result.matchedSupportedLocale;
3505
- extension = noExtensionLocaleMap[result.matchedDesiredLocale].slice(result.matchedDesiredLocale.length) || void 0;
3518
+ extension = splitUnicodeExtension(noExtensionLocaleMap[result.matchedDesiredLocale]).extension;
3506
3519
  }
3507
3520
  if (!foundLocale) return { locale: getDefaultLocale() };
3508
3521
  return {
@@ -3511,11 +3524,248 @@ function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
3511
3524
  };
3512
3525
  }
3513
3526
  //#endregion
3527
+ //#region node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/aliases.js
3528
+ const subdivisionAlias = {
3529
+ "cn11": "cnbj",
3530
+ "cn12": "cntj",
3531
+ "cn13": "cnhe",
3532
+ "cn14": "cnsx",
3533
+ "cn15": "cnmn",
3534
+ "cn21": "cnln",
3535
+ "cn22": "cnjl",
3536
+ "cn23": "cnhl",
3537
+ "cn31": "cnsh",
3538
+ "cn32": "cnjs",
3539
+ "cn33": "cnzj",
3540
+ "cn34": "cnah",
3541
+ "cn35": "cnfj",
3542
+ "cn36": "cnjx",
3543
+ "cn37": "cnsd",
3544
+ "cn41": "cnha",
3545
+ "cn42": "cnhb",
3546
+ "cn43": "cnhn",
3547
+ "cn44": "cngd",
3548
+ "cn45": "cngx",
3549
+ "cn46": "cnhi",
3550
+ "cn50": "cncq",
3551
+ "cn51": "cnsc",
3552
+ "cn52": "cngz",
3553
+ "cn53": "cnyn",
3554
+ "cn54": "cnxz",
3555
+ "cn61": "cnsn",
3556
+ "cn62": "cngs",
3557
+ "cn63": "cnqh",
3558
+ "cn64": "cnnx",
3559
+ "cn65": "cnxj",
3560
+ "cn71": "twzzzz",
3561
+ "cn91": "hkzzzz",
3562
+ "cn92": "mozzzz",
3563
+ "cz10a": "cz110",
3564
+ "cz10b": "cz111",
3565
+ "cz10c": "cz112",
3566
+ "cz10d": "cz113",
3567
+ "cz10e": "cz114",
3568
+ "cz10f": "cz115",
3569
+ "cz611": "cz663",
3570
+ "cz612": "cz632",
3571
+ "cz613": "cz633",
3572
+ "cz614": "cz634",
3573
+ "cz615": "cz635",
3574
+ "cz621": "cz641",
3575
+ "cz622": "cz642",
3576
+ "cz623": "cz643",
3577
+ "cz624": "cz644",
3578
+ "cz626": "cz646",
3579
+ "cz627": "cz647",
3580
+ "czjc": "cz31",
3581
+ "czjm": "cz64",
3582
+ "czka": "cz41",
3583
+ "czkr": "cz52",
3584
+ "czli": "cz51",
3585
+ "czmo": "cz80",
3586
+ "czol": "cz71",
3587
+ "czpa": "cz53",
3588
+ "czpl": "cz32",
3589
+ "czpr": "cz10",
3590
+ "czst": "cz20",
3591
+ "czus": "cz42",
3592
+ "czvy": "cz63",
3593
+ "czzl": "cz72",
3594
+ "fi01": "axzzzz",
3595
+ "fra": "frges",
3596
+ "frb": "frnaq",
3597
+ "frbl": "blzzzz",
3598
+ "frc": "frara",
3599
+ "frcp": "cpzzzz",
3600
+ "frd": "frbfc",
3601
+ "fre": "frbre",
3602
+ "frf": "frcvl",
3603
+ "frg": "frges",
3604
+ "frgf": "gfzzzz",
3605
+ "frgp": "gpzzzz",
3606
+ "frgua": "gpzzzz",
3607
+ "frh": "frcor",
3608
+ "fri": "frbfc",
3609
+ "frj": "fridf",
3610
+ "frk": "frocc",
3611
+ "frl": "frnaq",
3612
+ "frlre": "rezzzz",
3613
+ "frm": "frges",
3614
+ "frmay": "ytzzzz",
3615
+ "frmf": "mfzzzz",
3616
+ "frmq": "mqzzzz",
3617
+ "frn": "frocc",
3618
+ "frnc": "nczzzz",
3619
+ "fro": "frhdf",
3620
+ "frp": "frnor",
3621
+ "frpf": "pfzzzz",
3622
+ "frpm": "pmzzzz",
3623
+ "frq": "frnor",
3624
+ "frr": "frpdl",
3625
+ "frre": "rezzzz",
3626
+ "frs": "frhdf",
3627
+ "frt": "frnaq",
3628
+ "frtf": "tfzzzz",
3629
+ "fru": "frpac",
3630
+ "frv": "frara",
3631
+ "frwf": "wfzzzz",
3632
+ "fryt": "ytzzzz",
3633
+ "laxn": "laxs",
3634
+ "lud": "lucl",
3635
+ "lug": "luec",
3636
+ "lul": "luca",
3637
+ "mrnkc": "mr13",
3638
+ "nlaw": "awzzzz",
3639
+ "nlcw": "cwzzzz",
3640
+ "nlsx": "sxzzzz",
3641
+ "no23": "no50",
3642
+ "nzn": "nzauk",
3643
+ "nzs": "nzcan",
3644
+ "omba": "ombj",
3645
+ "omsh": "omsj",
3646
+ "plds": "pl02",
3647
+ "plkp": "pl04",
3648
+ "pllb": "pl08",
3649
+ "plld": "pl10",
3650
+ "pllu": "pl06",
3651
+ "plma": "pl12",
3652
+ "plmz": "pl14",
3653
+ "plop": "pl16",
3654
+ "plpd": "pl20",
3655
+ "plpk": "pl18",
3656
+ "plpm": "pl22",
3657
+ "plsk": "pl26",
3658
+ "plsl": "pl24",
3659
+ "plwn": "pl28",
3660
+ "plwp": "pl30",
3661
+ "plzp": "pl32",
3662
+ "shta": "tazzzz",
3663
+ "tteto": "tttob",
3664
+ "ttrcm": "ttmrc",
3665
+ "ttwto": "tttob",
3666
+ "twkhq": "twkhh",
3667
+ "twtnq": "twtnn",
3668
+ "twtpq": "twnwt",
3669
+ "twtxq": "twtxg",
3670
+ "usas": "aszzzz",
3671
+ "usgu": "guzzzz",
3672
+ "usmp": "mpzzzz",
3673
+ "uspr": "przzzz",
3674
+ "usum": "umzzzz",
3675
+ "usvi": "vizzzz"
3676
+ };
3677
+ const extensionAlias = {
3678
+ "t": {
3679
+ "d0": { "name": "charname" },
3680
+ "m0": {
3681
+ "beta-metsehaf": "betamets",
3682
+ "ies-jes": "iesjes",
3683
+ "names": "prprname",
3684
+ "tekie-alibekit": "tekieali"
3685
+ }
3686
+ },
3687
+ "u": {
3688
+ "ca": {
3689
+ "ethiopic-amete-alem": "ethioaa",
3690
+ "islamicc": "islamic-civil"
3691
+ },
3692
+ "kb": { "yes": "true" },
3693
+ "kc": { "yes": "true" },
3694
+ "kh": { "yes": "true" },
3695
+ "kk": { "yes": "true" },
3696
+ "kn": { "yes": "true" },
3697
+ "ks": {
3698
+ "primary": "level1",
3699
+ "tertiary": "level3"
3700
+ },
3701
+ "ms": { "imperial": "uksystem" },
3702
+ "tz": {
3703
+ "aqams": "aqmcm",
3704
+ "aukns": "auhba",
3705
+ "caffs": "cawnp",
3706
+ "camtr": "cator",
3707
+ "canpg": "cator",
3708
+ "capnt": "caiql",
3709
+ "cathu": "cator",
3710
+ "cayzf": "caedm",
3711
+ "cet": "bebru",
3712
+ "cnckg": "cnsha",
3713
+ "cnhrb": "cnsha",
3714
+ "cnkhg": "cnurc",
3715
+ "cst6cdt": "uschi",
3716
+ "cuba": "cuhav",
3717
+ "eet": "grath",
3718
+ "egypt": "egcai",
3719
+ "eire": "iedub",
3720
+ "est": "papty",
3721
+ "est5edt": "usnyc",
3722
+ "factory": "unk",
3723
+ "gaza": "gazastrp",
3724
+ "gmt0": "gmt",
3725
+ "hongkong": "hkhkg",
3726
+ "hst": "ushnl",
3727
+ "iceland": "isrey",
3728
+ "iran": "irthr",
3729
+ "israel": "jeruslm",
3730
+ "jamaica": "jmkin",
3731
+ "japan": "jptyo",
3732
+ "libya": "lytip",
3733
+ "met": "bebru",
3734
+ "mncoq": "mnuln",
3735
+ "mst": "usphx",
3736
+ "mst7mdt": "usden",
3737
+ "mxstis": "mxtij",
3738
+ "navajo": "usden",
3739
+ "poland": "plwaw",
3740
+ "portugal": "ptlis",
3741
+ "prc": "cnsha",
3742
+ "pst8pdt": "uslax",
3743
+ "roc": "twtpe",
3744
+ "rok": "krsel",
3745
+ "turkey": "trist",
3746
+ "uaozh": "uaiev",
3747
+ "uauzh": "uaiev",
3748
+ "uct": "utc",
3749
+ "umjon": "ushnl",
3750
+ "usnavajo": "usden",
3751
+ "wet": "ptlis",
3752
+ "zulu": "utc"
3753
+ }
3754
+ }
3755
+ };
3756
+ //#endregion
3514
3757
  //#region packages/intl-localematcher/abstract/CanonicalizeUValue.ts
3515
3758
  function CanonicalizeUValue(ukey, uvalue) {
3516
- let lowerValue = uvalue.toLowerCase();
3517
3759
  invariant(ukey !== void 0, `ukey must be defined`);
3518
- return lowerValue;
3760
+ let canonicalized = "";
3761
+ for (let i = 0; i < uvalue.length; i++) {
3762
+ const code = uvalue.charCodeAt(i);
3763
+ canonicalized += code >= 65 && code <= 90 ? String.fromCharCode(code + 32) : uvalue[i];
3764
+ }
3765
+ const aliases = Object.prototype.hasOwnProperty.call(extensionAlias.u, ukey) ? extensionAlias.u[ukey] : void 0;
3766
+ if (aliases && Object.prototype.hasOwnProperty.call(aliases, canonicalized)) canonicalized = aliases[canonicalized];
3767
+ if ((ukey === "rg" || ukey === "sd") && Object.prototype.hasOwnProperty.call(subdivisionAlias, canonicalized)) canonicalized = subdivisionAlias[canonicalized];
3768
+ return canonicalized === "true" ? "" : canonicalized;
3519
3769
  }
3520
3770
  //#endregion
3521
3771
  //#region packages/intl-localematcher/abstract/CanonicalizeUnicodeLocaleId.ts
@@ -3525,7 +3775,7 @@ function CanonicalizeUnicodeLocaleId(locale) {
3525
3775
  //#endregion
3526
3776
  //#region packages/intl-localematcher/abstract/InsertUnicodeExtensionAndCanonicalize.ts
3527
3777
  function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
3528
- invariant(locale.indexOf("-u-") === -1, "Expected locale to not have a Unicode locale extension");
3778
+ invariant(splitUnicodeExtension(locale).extension === void 0, "Expected locale to not have a Unicode locale extension");
3529
3779
  let extension = "-u";
3530
3780
  for (const attr of attributes) extension += `-${attr}`;
3531
3781
  for (const kw of keywords) {
@@ -3578,11 +3828,11 @@ function BestAvailableLocale(availableLocales, locale) {
3578
3828
  function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
3579
3829
  const result = { locale: "" };
3580
3830
  for (const locale of requestedLocales) {
3581
- const noExtensionLocale = locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, "");
3831
+ const { locale: noExtensionLocale, extension } = splitUnicodeExtension(locale);
3582
3832
  const availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
3583
3833
  if (availableLocale) {
3584
3834
  result.locale = availableLocale;
3585
- if (locale !== noExtensionLocale) result.extension = locale.slice(noExtensionLocale.length, locale.length);
3835
+ if (extension) result.extension = extension;
3586
3836
  return result;
3587
3837
  }
3588
3838
  }
@@ -3642,10 +3892,9 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
3642
3892
  };
3643
3893
  let foundLocale = r.locale;
3644
3894
  let foundLocaleData = localeData[foundLocale];
3645
- const result = {
3646
- locale: "en",
3647
- dataLocale: foundLocale
3648
- };
3895
+ const result = Object.create(null);
3896
+ result.locale = "en";
3897
+ result.dataLocale = foundLocale;
3649
3898
  let components;
3650
3899
  let keywords;
3651
3900
  if (r.extension) {
@@ -3657,7 +3906,7 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
3657
3906
  let keyLocaleData = foundLocaleData?.[key] ?? [];
3658
3907
  invariant(Array.isArray(keyLocaleData), `keyLocaleData for ${key} must be an array`);
3659
3908
  let value = keyLocaleData[0];
3660
- invariant(value === void 0 || typeof value === "string", `value must be a string or undefined`);
3909
+ invariant(value == null || typeof value === "string", `value must be a string, null, or undefined`);
3661
3910
  let supportedKeyword;
3662
3911
  let entry = keywords.find((k) => k.key === key);
3663
3912
  if (entry) {
@@ -3684,7 +3933,7 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
3684
3933
  optionsValue = CanonicalizeUValue(key.toLowerCase(), optionsValue);
3685
3934
  if (optionsValue === "") optionsValue = "true";
3686
3935
  }
3687
- if (optionsValue !== value && keyLocaleData.indexOf(optionsValue) > -1) {
3936
+ if (optionsValue !== void 0 && optionsValue !== value && keyLocaleData.indexOf(optionsValue) > -1) {
3688
3937
  value = optionsValue;
3689
3938
  supportedKeyword = void 0;
3690
3939
  }
@@ -3709,8 +3958,8 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
3709
3958
  function LookupSupportedLocales(availableLocales, requestedLocales) {
3710
3959
  const subset = [];
3711
3960
  for (const locale of requestedLocales) {
3712
- const availableLocale = BestAvailableLocale(availableLocales, locale.replace(UNICODE_EXTENSION_SEQUENCE_REGEX, ""));
3713
- if (availableLocale) subset.push(availableLocale);
3961
+ const { locale: noExtensionLocale } = splitUnicodeExtension(locale);
3962
+ if (BestAvailableLocale(availableLocales, noExtensionLocale)) subset.push(locale);
3714
3963
  }
3715
3964
  return subset;
3716
3965
  }