@formatjs/intl-segmenter 12.0.3 → 12.0.5

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 (2) hide show
  1. package/package.json +3 -3
  2. package/polyfill.iife.js +107 -10
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/intl-segmenter",
3
3
  "description": "Polyfill for Intl.Segmenter",
4
- "version": "12.0.3",
4
+ "version": "12.0.5",
5
5
  "license": "MIT",
6
6
  "author": "Matija Gaspar <matijagaspar@gmail.com>",
7
7
  "type": "module",
@@ -12,8 +12,8 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "tslib": "^2.8.0",
15
- "@formatjs/ecma402-abstract": "3.0.3",
16
- "@formatjs/intl-localematcher": "0.7.2"
15
+ "@formatjs/intl-localematcher": "0.7.3",
16
+ "@formatjs/ecma402-abstract": "3.0.5"
17
17
  },
18
18
  "bugs": "https://github.com/formatjs/formatjs/issues",
19
19
  "homepage": "https://github.com/formatjs/formatjs",
package/polyfill.iife.js CHANGED
@@ -2679,6 +2679,11 @@
2679
2679
  }
2680
2680
  var SIMPLE_UNITS = SANCTIONED_UNITS.map(removeUnitNamespace);
2681
2681
 
2682
+ // node_modules/.aspect_rules_js/@formatjs+ecma402-abstract@0.0.0/node_modules/@formatjs/ecma402-abstract/NumberFormat/decimal-cache.js
2683
+ var getPowerOf10 = memoize(function(exponent) {
2684
+ return Decimal.pow(10, exponent);
2685
+ });
2686
+
2682
2687
  // node_modules/.aspect_rules_js/@formatjs+ecma402-abstract@0.0.0/node_modules/@formatjs/ecma402-abstract/NumberFormat/ComputeExponentForMagnitude.js
2683
2688
  Decimal.set({
2684
2689
  toExpPos: 100
@@ -6735,7 +6740,7 @@
6735
6740
  }
6736
6741
  throw new Error("No matching distance found");
6737
6742
  }
6738
- function findMatchingDistance(desired, supported) {
6743
+ function findMatchingDistanceImpl(desired, supported) {
6739
6744
  var desiredLocale = new Intl.Locale(desired).maximize();
6740
6745
  var supportedLocale = new Intl.Locale(supported).maximize();
6741
6746
  var desiredLSR = {
@@ -6777,7 +6782,26 @@
6777
6782
  }
6778
6783
  return matchingDistance;
6779
6784
  }
6785
+ var findMatchingDistance = memoize(findMatchingDistanceImpl, {
6786
+ serializer: function(args) {
6787
+ return "".concat(args[0], "|").concat(args[1]);
6788
+ }
6789
+ });
6790
+ function getFallbackCandidates(locale) {
6791
+ var candidates = [];
6792
+ var current = locale;
6793
+ while (current) {
6794
+ candidates.push(current);
6795
+ var lastDash = current.lastIndexOf("-");
6796
+ if (lastDash === -1)
6797
+ break;
6798
+ current = current.substring(0, lastDash);
6799
+ }
6800
+ return candidates;
6801
+ }
6802
+ var canonicalizedSupportedCache = /* @__PURE__ */ new WeakMap();
6780
6803
  function findBestMatch(requestedLocales, supportedLocales, threshold) {
6804
+ var _a;
6781
6805
  if (threshold === void 0) {
6782
6806
  threshold = DEFAULT_MATCHING_THRESHOLD;
6783
6807
  }
@@ -6786,17 +6810,84 @@
6786
6810
  matchedDesiredLocale: "",
6787
6811
  distances: {}
6788
6812
  };
6789
- requestedLocales.forEach(function(desired, i) {
6790
- if (!result.distances[desired]) {
6791
- result.distances[desired] = {};
6792
- }
6793
- supportedLocales.forEach(function(supported) {
6794
- var distance = findMatchingDistance(desired, supported) + 0 + i * 40;
6795
- result.distances[desired][supported] = distance;
6813
+ var canonicalizedSupportedLocales = canonicalizedSupportedCache.get(supportedLocales);
6814
+ if (!canonicalizedSupportedLocales) {
6815
+ canonicalizedSupportedLocales = supportedLocales.map(function(locale) {
6816
+ try {
6817
+ var canonical = Intl.getCanonicalLocales([locale]);
6818
+ return canonical[0] || locale;
6819
+ } catch (_a2) {
6820
+ return locale;
6821
+ }
6822
+ });
6823
+ canonicalizedSupportedCache.set(supportedLocales, canonicalizedSupportedLocales);
6824
+ }
6825
+ var supportedSet = new Set(canonicalizedSupportedLocales);
6826
+ for (var i = 0; i < requestedLocales.length; i++) {
6827
+ var desired = requestedLocales[i];
6828
+ if (supportedSet.has(desired)) {
6829
+ var distance = 0 + i * 40;
6830
+ result.distances[desired] = (_a = {}, _a[desired] = distance, _a);
6796
6831
  if (distance < lowestDistance) {
6797
6832
  lowestDistance = distance;
6798
6833
  result.matchedDesiredLocale = desired;
6799
- result.matchedSupportedLocale = supported;
6834
+ result.matchedSupportedLocale = desired;
6835
+ }
6836
+ if (i === 0) {
6837
+ return result;
6838
+ }
6839
+ }
6840
+ }
6841
+ for (var i = 0; i < requestedLocales.length; i++) {
6842
+ var desired = requestedLocales[i];
6843
+ try {
6844
+ var maximized = new Intl.Locale(desired).maximize().toString();
6845
+ if (maximized !== desired) {
6846
+ var maximizedCandidates = getFallbackCandidates(maximized);
6847
+ for (var j = 0; j < maximizedCandidates.length; j++) {
6848
+ var candidate = maximizedCandidates[j];
6849
+ if (candidate === desired)
6850
+ continue;
6851
+ if (supportedSet.has(candidate)) {
6852
+ var distance = void 0;
6853
+ try {
6854
+ var candidateMaximized = new Intl.Locale(candidate).maximize().toString();
6855
+ distance = candidateMaximized === maximized ? 0 + i * 40 : j * 10 + i * 40;
6856
+ } catch (_b) {
6857
+ distance = j * 10 + i * 40;
6858
+ }
6859
+ if (!result.distances[desired]) {
6860
+ result.distances[desired] = {};
6861
+ }
6862
+ result.distances[desired][candidate] = distance;
6863
+ if (distance < lowestDistance) {
6864
+ lowestDistance = distance;
6865
+ result.matchedDesiredLocale = desired;
6866
+ result.matchedSupportedLocale = candidate;
6867
+ }
6868
+ break;
6869
+ }
6870
+ }
6871
+ }
6872
+ } catch (_c) {
6873
+ }
6874
+ }
6875
+ if (result.matchedSupportedLocale && lowestDistance === 0) {
6876
+ return result;
6877
+ }
6878
+ requestedLocales.forEach(function(desired2, i2) {
6879
+ if (!result.distances[desired2]) {
6880
+ result.distances[desired2] = {};
6881
+ }
6882
+ canonicalizedSupportedLocales.forEach(function(canonicalLocale, supportedIndex) {
6883
+ var originalSupported = supportedLocales[supportedIndex];
6884
+ var distance2 = findMatchingDistance(desired2, canonicalLocale);
6885
+ var finalDistance = distance2 + 0 + i2 * 40;
6886
+ result.distances[desired2][originalSupported] = finalDistance;
6887
+ if (finalDistance < lowestDistance) {
6888
+ lowestDistance = finalDistance;
6889
+ result.matchedDesiredLocale = desired2;
6890
+ result.matchedSupportedLocale = originalSupported;
6800
6891
  }
6801
6892
  });
6802
6893
  });
@@ -6877,10 +6968,16 @@
6877
6968
  }
6878
6969
 
6879
6970
  // node_modules/.aspect_rules_js/@formatjs+intl-localematcher@0.0.0/node_modules/@formatjs/intl-localematcher/abstract/BestAvailableLocale.js
6971
+ var availableLocalesSetCache = /* @__PURE__ */ new WeakMap();
6880
6972
  function BestAvailableLocale(availableLocales, locale) {
6973
+ var availableSet = availableLocalesSetCache.get(availableLocales);
6974
+ if (!availableSet) {
6975
+ availableSet = new Set(availableLocales);
6976
+ availableLocalesSetCache.set(availableLocales, availableSet);
6977
+ }
6881
6978
  var candidate = locale;
6882
6979
  while (true) {
6883
- if (availableLocales.indexOf(candidate) > -1) {
6980
+ if (availableSet.has(candidate)) {
6884
6981
  return candidate;
6885
6982
  }
6886
6983
  var pos = candidate.lastIndexOf("-");