@formatjs/intl-relativetimeformat 12.0.6 → 12.0.7

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 +5 -5
  2. package/polyfill.iife.js +102 -10
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/intl-relativetimeformat",
3
3
  "description": "Formats JavaScript dates to relative time strings.",
4
- "version": "12.0.6",
4
+ "version": "12.0.7",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
@@ -15,13 +15,13 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "tslib": "^2.8.0",
18
- "@formatjs/ecma402-abstract": "3.0.4",
19
- "@formatjs/intl-localematcher": "0.7.2"
18
+ "@formatjs/ecma402-abstract": "3.0.5",
19
+ "@formatjs/intl-localematcher": "0.7.3"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@formatjs/intl-getcanonicallocales": "3.0.4",
23
- "@formatjs/intl-locale": "5.0.5",
24
- "@formatjs/intl-pluralrules": "6.0.5"
23
+ "@formatjs/intl-locale": "5.0.6",
24
+ "@formatjs/intl-pluralrules": "6.0.6"
25
25
  },
26
26
  "bugs": "https://github.com/formatjs/formatjs/issues",
27
27
  "gitHead": "a7842673d8ad205171ad7c8cb8bb2f318b427c0c",
package/polyfill.iife.js CHANGED
@@ -6747,7 +6747,7 @@
6747
6747
  }
6748
6748
  throw new Error("No matching distance found");
6749
6749
  }
6750
- function findMatchingDistance(desired, supported) {
6750
+ function findMatchingDistanceImpl(desired, supported) {
6751
6751
  var desiredLocale = new Intl.Locale(desired).maximize();
6752
6752
  var supportedLocale = new Intl.Locale(supported).maximize();
6753
6753
  var desiredLSR = {
@@ -6789,7 +6789,26 @@
6789
6789
  }
6790
6790
  return matchingDistance;
6791
6791
  }
6792
+ var findMatchingDistance = memoize(findMatchingDistanceImpl, {
6793
+ serializer: function(args) {
6794
+ return "".concat(args[0], "|").concat(args[1]);
6795
+ }
6796
+ });
6797
+ function getFallbackCandidates(locale) {
6798
+ var candidates = [];
6799
+ var current = locale;
6800
+ while (current) {
6801
+ candidates.push(current);
6802
+ var lastDash = current.lastIndexOf("-");
6803
+ if (lastDash === -1)
6804
+ break;
6805
+ current = current.substring(0, lastDash);
6806
+ }
6807
+ return candidates;
6808
+ }
6809
+ var canonicalizedSupportedCache = /* @__PURE__ */ new WeakMap();
6792
6810
  function findBestMatch(requestedLocales, supportedLocales2, threshold) {
6811
+ var _a;
6793
6812
  if (threshold === void 0) {
6794
6813
  threshold = DEFAULT_MATCHING_THRESHOLD;
6795
6814
  }
@@ -6798,17 +6817,84 @@
6798
6817
  matchedDesiredLocale: "",
6799
6818
  distances: {}
6800
6819
  };
6801
- requestedLocales.forEach(function(desired, i) {
6802
- if (!result.distances[desired]) {
6803
- result.distances[desired] = {};
6804
- }
6805
- supportedLocales2.forEach(function(supported) {
6806
- var distance = findMatchingDistance(desired, supported) + 0 + i * 40;
6807
- result.distances[desired][supported] = distance;
6820
+ var canonicalizedSupportedLocales = canonicalizedSupportedCache.get(supportedLocales2);
6821
+ if (!canonicalizedSupportedLocales) {
6822
+ canonicalizedSupportedLocales = supportedLocales2.map(function(locale) {
6823
+ try {
6824
+ var canonical = Intl.getCanonicalLocales([locale]);
6825
+ return canonical[0] || locale;
6826
+ } catch (_a2) {
6827
+ return locale;
6828
+ }
6829
+ });
6830
+ canonicalizedSupportedCache.set(supportedLocales2, canonicalizedSupportedLocales);
6831
+ }
6832
+ var supportedSet = new Set(canonicalizedSupportedLocales);
6833
+ for (var i = 0; i < requestedLocales.length; i++) {
6834
+ var desired = requestedLocales[i];
6835
+ if (supportedSet.has(desired)) {
6836
+ var distance = 0 + i * 40;
6837
+ result.distances[desired] = (_a = {}, _a[desired] = distance, _a);
6808
6838
  if (distance < lowestDistance) {
6809
6839
  lowestDistance = distance;
6810
6840
  result.matchedDesiredLocale = desired;
6811
- result.matchedSupportedLocale = supported;
6841
+ result.matchedSupportedLocale = desired;
6842
+ }
6843
+ if (i === 0) {
6844
+ return result;
6845
+ }
6846
+ }
6847
+ }
6848
+ for (var i = 0; i < requestedLocales.length; i++) {
6849
+ var desired = requestedLocales[i];
6850
+ try {
6851
+ var maximized = new Intl.Locale(desired).maximize().toString();
6852
+ if (maximized !== desired) {
6853
+ var maximizedCandidates = getFallbackCandidates(maximized);
6854
+ for (var j = 0; j < maximizedCandidates.length; j++) {
6855
+ var candidate = maximizedCandidates[j];
6856
+ if (candidate === desired)
6857
+ continue;
6858
+ if (supportedSet.has(candidate)) {
6859
+ var distance = void 0;
6860
+ try {
6861
+ var candidateMaximized = new Intl.Locale(candidate).maximize().toString();
6862
+ distance = candidateMaximized === maximized ? 0 + i * 40 : j * 10 + i * 40;
6863
+ } catch (_b) {
6864
+ distance = j * 10 + i * 40;
6865
+ }
6866
+ if (!result.distances[desired]) {
6867
+ result.distances[desired] = {};
6868
+ }
6869
+ result.distances[desired][candidate] = distance;
6870
+ if (distance < lowestDistance) {
6871
+ lowestDistance = distance;
6872
+ result.matchedDesiredLocale = desired;
6873
+ result.matchedSupportedLocale = candidate;
6874
+ }
6875
+ break;
6876
+ }
6877
+ }
6878
+ }
6879
+ } catch (_c) {
6880
+ }
6881
+ }
6882
+ if (result.matchedSupportedLocale && lowestDistance === 0) {
6883
+ return result;
6884
+ }
6885
+ requestedLocales.forEach(function(desired2, i2) {
6886
+ if (!result.distances[desired2]) {
6887
+ result.distances[desired2] = {};
6888
+ }
6889
+ canonicalizedSupportedLocales.forEach(function(canonicalLocale, supportedIndex) {
6890
+ var originalSupported = supportedLocales2[supportedIndex];
6891
+ var distance2 = findMatchingDistance(desired2, canonicalLocale);
6892
+ var finalDistance = distance2 + 0 + i2 * 40;
6893
+ result.distances[desired2][originalSupported] = finalDistance;
6894
+ if (finalDistance < lowestDistance) {
6895
+ lowestDistance = finalDistance;
6896
+ result.matchedDesiredLocale = desired2;
6897
+ result.matchedSupportedLocale = originalSupported;
6812
6898
  }
6813
6899
  });
6814
6900
  });
@@ -6889,10 +6975,16 @@
6889
6975
  }
6890
6976
 
6891
6977
  // node_modules/.aspect_rules_js/@formatjs+intl-localematcher@0.0.0/node_modules/@formatjs/intl-localematcher/abstract/BestAvailableLocale.js
6978
+ var availableLocalesSetCache = /* @__PURE__ */ new WeakMap();
6892
6979
  function BestAvailableLocale(availableLocales, locale) {
6980
+ var availableSet = availableLocalesSetCache.get(availableLocales);
6981
+ if (!availableSet) {
6982
+ availableSet = new Set(availableLocales);
6983
+ availableLocalesSetCache.set(availableLocales, availableSet);
6984
+ }
6893
6985
  var candidate = locale;
6894
6986
  while (true) {
6895
- if (availableLocales.indexOf(candidate) > -1) {
6987
+ if (availableSet.has(candidate)) {
6896
6988
  return candidate;
6897
6989
  }
6898
6990
  var pos = candidate.lastIndexOf("-");