@formatjs/intl-numberformat 9.0.5 → 9.0.6

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 +6 -6
  2. package/polyfill.iife.js +78 -19
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/intl-numberformat",
3
3
  "description": "Ponyfill for ES2020 Intl.NumberFormat",
4
- "version": "9.0.5",
4
+ "version": "9.0.6",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "types": "index.d.ts",
@@ -15,13 +15,13 @@
15
15
  "dependencies": {
16
16
  "decimal.js": "^10.4.3",
17
17
  "tslib": "^2.8.0",
18
- "@formatjs/ecma402-abstract": "3.0.3",
19
- "@formatjs/intl-localematcher": "0.7.2"
18
+ "@formatjs/intl-localematcher": "0.7.2",
19
+ "@formatjs/ecma402-abstract": "3.0.4"
20
20
  },
21
21
  "devDependencies": {
22
- "@formatjs/intl-pluralrules": "6.0.4",
23
- "@formatjs/intl-getcanonicallocales": "3.0.3",
24
- "@formatjs/intl-locale": "5.0.4"
22
+ "@formatjs/intl-getcanonicallocales": "3.0.4",
23
+ "@formatjs/intl-locale": "5.0.5",
24
+ "@formatjs/intl-pluralrules": "6.0.5"
25
25
  },
26
26
  "bugs": "https://github.com/formatjs/formatjs/issues",
27
27
  "contributors": [
package/polyfill.iife.js CHANGED
@@ -2899,6 +2899,11 @@
2899
2899
  return result;
2900
2900
  }
2901
2901
 
2902
+ // node_modules/.aspect_rules_js/@formatjs+ecma402-abstract@0.0.0/node_modules/@formatjs/ecma402-abstract/NumberFormat/decimal-cache.js
2903
+ var getPowerOf10 = memoize(function(exponent) {
2904
+ return Decimal.pow(10, exponent);
2905
+ });
2906
+
2902
2907
  // node_modules/.aspect_rules_js/@formatjs+ecma402-abstract@0.0.0/node_modules/@formatjs/ecma402-abstract/NumberFormat/ComputeExponentForMagnitude.js
2903
2908
  Decimal.set({
2904
2909
  toExpPos: 100
@@ -2927,7 +2932,7 @@
2927
2932
  if (!thresholdMap) {
2928
2933
  return 0;
2929
2934
  }
2930
- var num = Decimal.pow(10, magnitude).toString();
2935
+ var num = getPowerOf10(magnitude).toString();
2931
2936
  var thresholds = Object.keys(thresholdMap);
2932
2937
  if (num < thresholds[0]) {
2933
2938
  return 0;
@@ -2984,10 +2989,10 @@
2984
2989
  toExpPos: 100
2985
2990
  });
2986
2991
  function ToRawFixedFn(n, f) {
2987
- return n.times(Decimal.pow(10, -f));
2992
+ return n.times(getPowerOf10(-f));
2988
2993
  }
2989
2994
  function findN1R1(x, f, roundingIncrement) {
2990
- var nx = x.times(Decimal.pow(10, f)).floor();
2995
+ var nx = x.times(getPowerOf10(f)).floor();
2991
2996
  var n1 = nx.div(roundingIncrement).floor().times(roundingIncrement);
2992
2997
  var r1 = ToRawFixedFn(n1, f);
2993
2998
  return {
@@ -2996,7 +3001,7 @@
2996
3001
  };
2997
3002
  }
2998
3003
  function findN2R2(x, f, roundingIncrement) {
2999
- var nx = x.times(Decimal.pow(10, f)).ceil();
3004
+ var nx = x.times(getPowerOf10(f)).ceil();
3000
3005
  var n2 = nx.div(roundingIncrement).ceil().times(roundingIncrement);
3001
3006
  var r2 = ToRawFixedFn(n2, f);
3002
3007
  return {
@@ -3056,14 +3061,34 @@
3056
3061
 
3057
3062
  // node_modules/.aspect_rules_js/@formatjs+ecma402-abstract@0.0.0/node_modules/@formatjs/ecma402-abstract/NumberFormat/ToRawPrecision.js
3058
3063
  function findN1E1R1(x, p) {
3059
- var maxN1 = Decimal.pow(10, p);
3060
- var minN1 = Decimal.pow(10, p - 1);
3064
+ var maxN1 = getPowerOf10(p);
3065
+ var minN1 = getPowerOf10(p - 1);
3066
+ var log10x = x.log(10);
3067
+ var e1 = log10x.floor();
3068
+ var divisor = getPowerOf10(e1.minus(p).plus(1));
3069
+ var n1 = x.div(divisor).floor();
3070
+ var r1 = n1.times(divisor);
3071
+ if (n1.greaterThanOrEqualTo(maxN1)) {
3072
+ e1 = e1.plus(1);
3073
+ var newDivisor = getPowerOf10(e1.minus(p).plus(1));
3074
+ n1 = x.div(newDivisor).floor();
3075
+ r1 = n1.times(newDivisor);
3076
+ } else if (n1.lessThan(minN1)) {
3077
+ e1 = e1.minus(1);
3078
+ var newDivisor = getPowerOf10(e1.minus(p).plus(1));
3079
+ n1 = x.div(newDivisor).floor();
3080
+ r1 = n1.times(newDivisor);
3081
+ }
3082
+ if (r1.lessThanOrEqualTo(x) && n1.lessThan(maxN1) && n1.greaterThanOrEqualTo(minN1)) {
3083
+ return { n1, e1, r1 };
3084
+ }
3061
3085
  var maxE1 = x.div(minN1).log(10).plus(p).minus(1).ceil();
3062
3086
  var currentE1 = maxE1;
3063
3087
  while (true) {
3064
- var currentN1 = x.div(Decimal.pow(10, currentE1.minus(p).plus(1))).floor();
3088
+ var currentDivisor = getPowerOf10(currentE1.minus(p).plus(1));
3089
+ var currentN1 = x.div(currentDivisor).floor();
3065
3090
  if (currentN1.lessThan(maxN1) && currentN1.greaterThanOrEqualTo(minN1)) {
3066
- var currentR1 = currentN1.times(Decimal.pow(10, currentE1.minus(p).plus(1)));
3091
+ var currentR1 = currentN1.times(currentDivisor);
3067
3092
  if (currentR1.lessThanOrEqualTo(x)) {
3068
3093
  return {
3069
3094
  n1: currentN1,
@@ -3076,14 +3101,34 @@
3076
3101
  }
3077
3102
  }
3078
3103
  function findN2E2R2(x, p) {
3079
- var maxN2 = Decimal.pow(10, p);
3080
- var minN2 = Decimal.pow(10, p - 1);
3104
+ var maxN2 = getPowerOf10(p);
3105
+ var minN2 = getPowerOf10(p - 1);
3106
+ var log10x = x.log(10);
3107
+ var e2 = log10x.floor();
3108
+ var divisor = getPowerOf10(e2.minus(p).plus(1));
3109
+ var n2 = x.div(divisor).ceil();
3110
+ var r2 = n2.times(divisor);
3111
+ if (n2.greaterThanOrEqualTo(maxN2)) {
3112
+ e2 = e2.plus(1);
3113
+ var newDivisor = getPowerOf10(e2.minus(p).plus(1));
3114
+ n2 = x.div(newDivisor).ceil();
3115
+ r2 = n2.times(newDivisor);
3116
+ } else if (n2.lessThan(minN2)) {
3117
+ e2 = e2.minus(1);
3118
+ var newDivisor = getPowerOf10(e2.minus(p).plus(1));
3119
+ n2 = x.div(newDivisor).ceil();
3120
+ r2 = n2.times(newDivisor);
3121
+ }
3122
+ if (r2.greaterThanOrEqualTo(x) && n2.lessThan(maxN2) && n2.greaterThanOrEqualTo(minN2)) {
3123
+ return { n2, e2, r2 };
3124
+ }
3081
3125
  var minE2 = x.div(maxN2).log(10).plus(p).minus(1).floor();
3082
3126
  var currentE2 = minE2;
3083
3127
  while (true) {
3084
- var currentN2 = x.div(Decimal.pow(10, currentE2.minus(p).plus(1))).ceil();
3128
+ var currentDivisor = getPowerOf10(currentE2.minus(p).plus(1));
3129
+ var currentN2 = x.div(currentDivisor).ceil();
3085
3130
  if (currentN2.lessThan(maxN2) && currentN2.greaterThanOrEqualTo(minN2)) {
3086
- var currentR2 = currentN2.times(Decimal.pow(10, currentE2.minus(p).plus(1)));
3131
+ var currentR2 = currentN2.times(currentDivisor);
3087
3132
  if (currentR2.greaterThanOrEqualTo(x)) {
3088
3133
  return {
3089
3134
  n2: currentN2,
@@ -3229,14 +3274,28 @@
3229
3274
  if (x.isNegative()) {
3230
3275
  x = x.negated();
3231
3276
  }
3232
- var magnitude = x.log(10).floor();
3277
+ var xNum = x.toNumber();
3278
+ var magnitude;
3279
+ if (Number.isFinite(xNum) && Number.isSafeInteger(xNum) && xNum > 0 && xNum <= 999999) {
3280
+ var magNum = Math.floor(Math.log10(xNum));
3281
+ magnitude = new Decimal(magNum);
3282
+ } else {
3283
+ magnitude = x.log(10).floor();
3284
+ }
3233
3285
  var exponent = ComputeExponentForMagnitude(internalSlots, magnitude);
3234
- x = x.times(Decimal.pow(10, -exponent));
3286
+ x = x.times(getPowerOf10(-exponent));
3235
3287
  var formatNumberResult = FormatNumericToString(internalSlots, x);
3236
3288
  if (formatNumberResult.roundedNumber.isZero()) {
3237
3289
  return [exponent, magnitude.toNumber()];
3238
3290
  }
3239
- var newMagnitude = formatNumberResult.roundedNumber.log(10).floor();
3291
+ var roundedNum = formatNumberResult.roundedNumber.toNumber();
3292
+ var newMagnitude;
3293
+ if (Number.isFinite(roundedNum) && Number.isSafeInteger(roundedNum) && roundedNum > 0 && roundedNum <= 999999) {
3294
+ var newMagNum = Math.floor(Math.log10(roundedNum));
3295
+ newMagnitude = new Decimal(newMagNum);
3296
+ } else {
3297
+ newMagnitude = formatNumberResult.roundedNumber.log(10).floor();
3298
+ }
3240
3299
  if (newMagnitude.eq(magnitude.minus(exponent))) {
3241
3300
  return [exponent, magnitude.toNumber()];
3242
3301
  }
@@ -4155,7 +4214,7 @@
4155
4214
  var unitName = void 0;
4156
4215
  var currencyNameData = data2.currencies[options.currency];
4157
4216
  if (currencyNameData) {
4158
- unitName = selectPlural(pl, numberResult.roundedNumber.times(Decimal.pow(10, exponent)).toNumber(), currencyNameData.displayName);
4217
+ unitName = selectPlural(pl, numberResult.roundedNumber.times(getPowerOf10(exponent)).toNumber(), currencyNameData.displayName);
4159
4218
  } else {
4160
4219
  unitName = options.currency;
4161
4220
  }
@@ -4187,11 +4246,11 @@
4187
4246
  var unitData = data2.units.simple[unit];
4188
4247
  var unitPattern = void 0;
4189
4248
  if (unitData) {
4190
- unitPattern = selectPlural(pl, numberResult.roundedNumber.times(Decimal.pow(10, exponent)).toNumber(), data2.units.simple[unit][unitDisplay]);
4249
+ unitPattern = selectPlural(pl, numberResult.roundedNumber.times(getPowerOf10(exponent)).toNumber(), data2.units.simple[unit][unitDisplay]);
4191
4250
  } else {
4192
4251
  var _c = unit.split("-per-"), numeratorUnit = _c[0], denominatorUnit = _c[1];
4193
4252
  unitData = data2.units.simple[numeratorUnit];
4194
- var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber.times(Decimal.pow(10, exponent)).toNumber(), data2.units.simple[numeratorUnit][unitDisplay]);
4253
+ var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber.times(getPowerOf10(exponent)).toNumber(), data2.units.simple[numeratorUnit][unitDisplay]);
4195
4254
  var perUnitPattern = data2.units.simple[denominatorUnit].perUnit[unitDisplay];
4196
4255
  if (perUnitPattern) {
4197
4256
  unitPattern = perUnitPattern.replace("{0}", numeratorUnitPattern);
@@ -4382,7 +4441,7 @@
4382
4441
  ;
4383
4442
  _a = ComputeExponent(internalSlots, x), exponent = _a[0], // IMPL: We need to record the magnitude of the number
4384
4443
  magnitude = _a[1];
4385
- x = x.times(Decimal.pow(10, -exponent));
4444
+ x = x.times(getPowerOf10(-exponent));
4386
4445
  }
4387
4446
  var formatNumberResult = FormatNumericToString(internalSlots, x);
4388
4447
  n = formatNumberResult.formattedString;