@formatjs/ecma402-abstract 2.3.1 → 2.3.2

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 (59) hide show
  1. package/262.d.ts +3 -2
  2. package/262.js +77 -15
  3. package/NumberFormat/ComputeExponent.d.ts +1 -3
  4. package/NumberFormat/ComputeExponent.js +7 -11
  5. package/NumberFormat/ComputeExponentForMagnitude.d.ts +1 -3
  6. package/NumberFormat/ComputeExponentForMagnitude.js +2 -5
  7. package/NumberFormat/FormatApproximately.d.ts +1 -3
  8. package/NumberFormat/FormatApproximately.js +1 -3
  9. package/NumberFormat/FormatNumeric.d.ts +3 -0
  10. package/NumberFormat/FormatNumeric.js +8 -0
  11. package/NumberFormat/FormatNumericToParts.js +1 -1
  12. package/NumberFormat/FormatNumericToString.d.ts +1 -1
  13. package/NumberFormat/FormatNumericToString.js +2 -1
  14. package/NumberFormat/InitializeNumberFormat.js +1 -1
  15. package/NumberFormat/PartitionNumberPattern.d.ts +2 -4
  16. package/NumberFormat/PartitionNumberPattern.js +53 -14
  17. package/NumberFormat/PartitionNumberRangePattern.js +27 -19
  18. package/NumberFormat/SetNumberFormatDigitOptions.js +73 -3
  19. package/NumberFormat/SetNumberFormatUnitOptions.d.ts +1 -3
  20. package/NumberFormat/SetNumberFormatUnitOptions.js +26 -15
  21. package/NumberFormat/ToRawFixed.d.ts +2 -3
  22. package/NumberFormat/ToRawFixed.js +39 -7
  23. package/NumberFormat/ToRawPrecision.d.ts +6 -0
  24. package/NumberFormat/ToRawPrecision.js +61 -19
  25. package/NumberFormat/format_to_parts.js +9 -4
  26. package/ToIntlMathematicalValue.d.ts +2 -0
  27. package/ToIntlMathematicalValue.js +31 -0
  28. package/index.d.ts +2 -0
  29. package/index.js +4 -1
  30. package/lib/262.d.ts +3 -2
  31. package/lib/262.js +76 -15
  32. package/lib/NumberFormat/ComputeExponent.d.ts +1 -3
  33. package/lib/NumberFormat/ComputeExponent.js +6 -11
  34. package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts +1 -3
  35. package/lib/NumberFormat/ComputeExponentForMagnitude.js +2 -5
  36. package/lib/NumberFormat/FormatApproximately.d.ts +1 -3
  37. package/lib/NumberFormat/FormatApproximately.js +1 -3
  38. package/lib/NumberFormat/FormatNumeric.d.ts +3 -0
  39. package/lib/NumberFormat/FormatNumeric.js +5 -0
  40. package/lib/NumberFormat/FormatNumericToParts.js +1 -1
  41. package/lib/NumberFormat/FormatNumericToString.d.ts +1 -1
  42. package/lib/NumberFormat/FormatNumericToString.js +2 -1
  43. package/lib/NumberFormat/InitializeNumberFormat.js +1 -1
  44. package/lib/NumberFormat/PartitionNumberPattern.d.ts +2 -4
  45. package/lib/NumberFormat/PartitionNumberPattern.js +53 -14
  46. package/lib/NumberFormat/PartitionNumberRangePattern.js +27 -19
  47. package/lib/NumberFormat/SetNumberFormatDigitOptions.js +73 -3
  48. package/lib/NumberFormat/SetNumberFormatUnitOptions.d.ts +1 -3
  49. package/lib/NumberFormat/SetNumberFormatUnitOptions.js +26 -15
  50. package/lib/NumberFormat/ToRawFixed.d.ts +2 -3
  51. package/lib/NumberFormat/ToRawFixed.js +39 -7
  52. package/lib/NumberFormat/ToRawPrecision.d.ts +6 -0
  53. package/lib/NumberFormat/ToRawPrecision.js +62 -20
  54. package/lib/NumberFormat/format_to_parts.js +9 -4
  55. package/lib/ToIntlMathematicalValue.d.ts +2 -0
  56. package/lib/ToIntlMathematicalValue.js +27 -0
  57. package/lib/index.d.ts +2 -0
  58. package/lib/index.js +2 -0
  59. package/package.json +3 -3
@@ -5,6 +5,7 @@ var DefaultNumberOption_1 = require("../DefaultNumberOption");
5
5
  var GetNumberOption_1 = require("../GetNumberOption");
6
6
  var GetOption_1 = require("../GetOption");
7
7
  var utils_1 = require("../utils");
8
+ //IMPL: Valid rounding increments as per implementation
8
9
  var VALID_ROUNDING_INCREMENTS = new Set([
9
10
  1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000,
10
11
  ]);
@@ -12,14 +13,23 @@ var VALID_ROUNDING_INCREMENTS = new Set([
12
13
  * https://tc39.es/ecma402/#sec-setnfdigitoptions
13
14
  */
14
15
  function SetNumberFormatDigitOptions(internalSlots, opts, mnfdDefault, mxfdDefault, notation) {
16
+ // 1. Let mnid be ? GetNumberOption(opts, "minimumIntegerDigits", 1, 21, 1).
15
17
  var mnid = (0, GetNumberOption_1.GetNumberOption)(opts, 'minimumIntegerDigits', 1, 21, 1);
18
+ // 2. Let mnfd be opts.[[MinimumFractionDigits]].
16
19
  var mnfd = opts.minimumFractionDigits;
20
+ // 3. Let mxfd be opts.[[MaximumFractionDigits]].
17
21
  var mxfd = opts.maximumFractionDigits;
22
+ // 4. Let mnsd be opts.[[MinimumSignificantDigits]].
18
23
  var mnsd = opts.minimumSignificantDigits;
24
+ // 5. Let mxsd be opts.[[MaximumSignificantDigits]].
19
25
  var mxsd = opts.maximumSignificantDigits;
26
+ // 6. Set internalSlots.[[MinimumIntegerDigits]] to mnid.
20
27
  internalSlots.minimumIntegerDigits = mnid;
28
+ // 7. Let roundingIncrement be ? GetNumberOption(opts, "roundingIncrement", 1, 5000, 1).
21
29
  var roundingIncrement = (0, GetNumberOption_1.GetNumberOption)(opts, 'roundingIncrement', 1, 5000, 1);
30
+ // 8. If roundingIncrement is not an element of the list {1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000}, throw a RangeError exception.
22
31
  (0, utils_1.invariant)(VALID_ROUNDING_INCREMENTS.has(roundingIncrement), "Invalid rounding increment value: ".concat(roundingIncrement, ".\nValid values are ").concat(Array.from(VALID_ROUNDING_INCREMENTS).join(', '), "."));
32
+ // 9. Let roundingMode be ? GetOption(opts, "roundingMode", "string", « "ceil", "floor", "expand", "trunc", "halfCeil", "halfFloor", "halfExpand", "halfTrunc", "halfEven" », "halfExpand").
23
33
  var roundingMode = (0, GetOption_1.GetOption)(opts, 'roundingMode', 'string', [
24
34
  'ceil',
25
35
  'floor',
@@ -31,82 +41,142 @@ function SetNumberFormatDigitOptions(internalSlots, opts, mnfdDefault, mxfdDefau
31
41
  'halfTrunc',
32
42
  'halfEven',
33
43
  ], 'halfExpand');
44
+ // 10. Let roundingPriority be ? GetOption(opts, "roundingPriority", "string", « "auto", "morePrecision", "lessPrecision" », "auto").
34
45
  var roundingPriority = (0, GetOption_1.GetOption)(opts, 'roundingPriority', 'string', ['auto', 'morePrecision', 'lessPrecision'], 'auto');
46
+ // 11. Let trailingZeroDisplay be ? GetOption(opts, "trailingZeroDisplay", "string", « "auto", "stripIfInteger" », "auto").
35
47
  var trailingZeroDisplay = (0, GetOption_1.GetOption)(opts, 'trailingZeroDisplay', 'string', ['auto', 'stripIfInteger'], 'auto');
48
+ // 12. If roundingIncrement is not 1, then
36
49
  if (roundingIncrement !== 1) {
50
+ // 12.a. Set mxfdDefault to mnfdDefault.
37
51
  mxfdDefault = mnfdDefault;
38
52
  }
53
+ // 13. Set internalSlots.[[RoundingIncrement]] to roundingIncrement.
39
54
  internalSlots.roundingIncrement = roundingIncrement;
55
+ // 14. Set internalSlots.[[RoundingMode]] to roundingMode.
40
56
  internalSlots.roundingMode = roundingMode;
57
+ // 15. Set internalSlots.[[TrailingZeroDisplay]] to trailingZeroDisplay.
41
58
  internalSlots.trailingZeroDisplay = trailingZeroDisplay;
59
+ // 16. Let hasSd be true if mnsd is not undefined or mxsd is not undefined; otherwise, let hasSd be false.
42
60
  var hasSd = mnsd !== undefined || mxsd !== undefined;
61
+ // 17. Let hasFd be true if mnfd is not undefined or mxfd is not undefined; otherwise, let hasFd be false.
43
62
  var hasFd = mnfd !== undefined || mxfd !== undefined;
63
+ // 18. Let needSd be true.
44
64
  var needSd = true;
65
+ // 19. Let needFd be true.
45
66
  var needFd = true;
67
+ // 20. If roundingPriority is "auto", then
46
68
  if (roundingPriority === 'auto') {
69
+ // 20.a. Set needSd to hasSd.
47
70
  needSd = hasSd;
71
+ // 20.b. If hasSd is true or hasFd is false and notation is "compact", then
48
72
  if (hasSd || (!hasFd && notation === 'compact')) {
73
+ // 20.b.i. Set needFd to false.
49
74
  needFd = false;
50
75
  }
51
76
  }
77
+ // 21. If needSd is true, then
52
78
  if (needSd) {
79
+ // 21.a. If hasSd is true, then
53
80
  if (hasSd) {
81
+ // 21.a.i. Set internalSlots.[[MinimumSignificantDigits]] to ? DefaultNumberOption(mnsd, 1, 21, 1).
54
82
  internalSlots.minimumSignificantDigits = (0, DefaultNumberOption_1.DefaultNumberOption)(mnsd, 1, 21, 1);
83
+ // 21.a.ii. Set internalSlots.[[MaximumSignificantDigits]] to ? DefaultNumberOption(mxsd, internalSlots.[[MinimumSignificantDigits]], 21, 21).
55
84
  internalSlots.maximumSignificantDigits = (0, DefaultNumberOption_1.DefaultNumberOption)(mxsd, internalSlots.minimumSignificantDigits, 21, 21);
56
85
  }
57
86
  else {
87
+ // 21.b. Else,
88
+ // 21.b.i. Set internalSlots.[[MinimumSignificantDigits]] to 1.
58
89
  internalSlots.minimumSignificantDigits = 1;
90
+ // 21.b.ii. Set internalSlots.[[MaximumSignificantDigits]] to 21.
59
91
  internalSlots.maximumSignificantDigits = 21;
60
92
  }
61
93
  }
94
+ // 22. If needFd is true, then
62
95
  if (needFd) {
96
+ // 22.a. If hasFd is true, then
63
97
  if (hasFd) {
98
+ // 22.a.i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 100, undefined).
64
99
  mnfd = (0, DefaultNumberOption_1.DefaultNumberOption)(mnfd, 0, 100, undefined);
100
+ // 22.a.ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 100, undefined).
65
101
  mxfd = (0, DefaultNumberOption_1.DefaultNumberOption)(mxfd, 0, 100, undefined);
102
+ // 22.a.iii. If mnfd is undefined, then
66
103
  if (mnfd === undefined) {
67
- mnfd = Math.min(mnfdDefault, mxfd !== null && mxfd !== void 0 ? mxfd : 0);
104
+ // 22.a.iii.1. Assert: mxfd is not undefined.
105
+ (0, utils_1.invariant)(mxfd !== undefined, 'maximumFractionDigits must be defined');
106
+ // 22.a.iii.2. Set mnfd to min(mnfdDefault, mxfd).
107
+ mnfd = Math.min(mnfdDefault, mxfd);
68
108
  }
69
109
  else if (mxfd === undefined) {
110
+ // 22.a.iv. Else if mxfd is undefined, then
111
+ // 22.a.iv.1. Set mxfd to max(mxfdDefault, mnfd).
70
112
  mxfd = Math.max(mxfdDefault, mnfd);
71
113
  }
72
114
  else if (mnfd > mxfd) {
115
+ // 22.a.v. Else if mnfd > mxfd, throw a RangeError exception.
73
116
  throw new RangeError("Invalid range, ".concat(mnfd, " > ").concat(mxfd));
74
117
  }
118
+ // 22.a.vi. Set internalSlots.[[MinimumFractionDigits]] to mnfd.
75
119
  internalSlots.minimumFractionDigits = mnfd;
120
+ // 22.a.vii. Set internalSlots.[[MaximumFractionDigits]] to mxfd.
76
121
  internalSlots.maximumFractionDigits = mxfd;
77
122
  }
78
123
  else {
124
+ // 22.b. Else,
125
+ // 22.b.i. Set internalSlots.[[MinimumFractionDigits]] to mnfdDefault.
79
126
  internalSlots.minimumFractionDigits = mnfdDefault;
127
+ // 22.b.ii. Set internalSlots.[[MaximumFractionDigits]] to mxfdDefault.
80
128
  internalSlots.maximumFractionDigits = mxfdDefault;
81
129
  }
82
130
  }
131
+ // 23. If needSd is false and needFd is false, then
83
132
  if (!needSd && !needFd) {
133
+ // 23.a. Set internalSlots.[[MinimumFractionDigits]] to 0.
84
134
  internalSlots.minimumFractionDigits = 0;
135
+ // 23.b. Set internalSlots.[[MaximumFractionDigits]] to 0.
85
136
  internalSlots.maximumFractionDigits = 0;
137
+ // 23.c. Set internalSlots.[[MinimumSignificantDigits]] to 1.
86
138
  internalSlots.minimumSignificantDigits = 1;
139
+ // 23.d. Set internalSlots.[[MaximumSignificantDigits]] to 2.
87
140
  internalSlots.maximumSignificantDigits = 2;
141
+ // 23.e. Set internalSlots.[[RoundingType]] to "morePrecision".
88
142
  internalSlots.roundingType = 'morePrecision';
143
+ // 23.f. Set internalSlots.[[RoundingPriority]] to "morePrecision".
89
144
  internalSlots.roundingPriority = 'morePrecision';
90
145
  }
91
146
  else if (roundingPriority === 'morePrecision') {
147
+ // 24. Else if roundingPriority is "morePrecision", then
148
+ // 24.a. Set internalSlots.[[RoundingType]] to "morePrecision".
92
149
  internalSlots.roundingType = 'morePrecision';
150
+ // 24.b. Set internalSlots.[[RoundingPriority]] to "morePrecision".
93
151
  internalSlots.roundingPriority = 'morePrecision';
94
152
  }
95
153
  else if (roundingPriority === 'lessPrecision') {
154
+ // 25. Else if roundingPriority is "lessPrecision", then
155
+ // 25.a. Set internalSlots.[[RoundingType]] to "lessPrecision".
96
156
  internalSlots.roundingType = 'lessPrecision';
157
+ // 25.b. Set internalSlots.[[RoundingPriority]] to "lessPrecision".
97
158
  internalSlots.roundingPriority = 'lessPrecision';
98
159
  }
99
160
  else if (hasSd) {
161
+ // 26. Else if hasSd is true, then
162
+ // 26.a. Set internalSlots.[[RoundingType]] to "significantDigits".
100
163
  internalSlots.roundingType = 'significantDigits';
164
+ // 26.b. Set internalSlots.[[RoundingPriority]] to "auto".
101
165
  internalSlots.roundingPriority = 'auto';
102
166
  }
103
167
  else {
168
+ // 27. Else,
169
+ // 27.a. Set internalSlots.[[RoundingType]] to "fractionDigits".
104
170
  internalSlots.roundingType = 'fractionDigits';
171
+ // 27.b. Set internalSlots.[[RoundingPriority]] to "auto".
105
172
  internalSlots.roundingPriority = 'auto';
106
173
  }
174
+ // 28. If roundingIncrement is not 1, then
107
175
  if (roundingIncrement !== 1) {
108
- (0, utils_1.invariant)(internalSlots.roundingType === 'fractionDigits', 'Invalid roundingType');
176
+ // 28.a. Assert: internalSlots.[[RoundingType]] is "fractionDigits".
177
+ (0, utils_1.invariant)(internalSlots.roundingType === 'fractionDigits', 'Invalid roundingType', TypeError);
178
+ // 28.b. Assert: internalSlots.[[MaximumFractionDigits]] is equal to internalSlots.[[MinimumFractionDigits]].
109
179
  (0, utils_1.invariant)(internalSlots.maximumFractionDigits ===
110
- internalSlots.minimumFractionDigits, 'With roundingIncrement > 1, maximumFractionDigits and minimumFractionDigits must be equal.');
180
+ internalSlots.minimumFractionDigits, 'With roundingIncrement > 1, maximumFractionDigits and minimumFractionDigits must be equal.', RangeError);
111
181
  }
112
182
  }
@@ -2,6 +2,4 @@ import { NumberFormatInternal, NumberFormatOptions } from '../types/number';
2
2
  /**
3
3
  * https://tc39.es/ecma402/#sec-setnumberformatunitoptions
4
4
  */
5
- export declare function SetNumberFormatUnitOptions(nf: Intl.NumberFormat, options: NumberFormatOptions | undefined, { getInternalSlots, }: {
6
- getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
7
- }): void;
5
+ export declare function SetNumberFormatUnitOptions(internalSlots: NumberFormatInternal, options?: NumberFormatOptions | undefined): void;
@@ -4,39 +4,50 @@ exports.SetNumberFormatUnitOptions = SetNumberFormatUnitOptions;
4
4
  var GetOption_1 = require("../GetOption");
5
5
  var IsWellFormedCurrencyCode_1 = require("../IsWellFormedCurrencyCode");
6
6
  var IsWellFormedUnitIdentifier_1 = require("../IsWellFormedUnitIdentifier");
7
+ var utils_1 = require("../utils");
7
8
  /**
8
9
  * https://tc39.es/ecma402/#sec-setnumberformatunitoptions
9
10
  */
10
- function SetNumberFormatUnitOptions(nf, options, _a) {
11
+ function SetNumberFormatUnitOptions(internalSlots, options) {
11
12
  if (options === void 0) { options = Object.create(null); }
12
- var getInternalSlots = _a.getInternalSlots;
13
- var internalSlots = getInternalSlots(nf);
13
+ // 1. Let style be ? GetOption(options, "style", "string", « "decimal", "percent", "currency", "unit" », "decimal").
14
14
  var style = (0, GetOption_1.GetOption)(options, 'style', 'string', ['decimal', 'percent', 'currency', 'unit'], 'decimal');
15
+ // 2. Set internalSlots.[[Style]] to style.
15
16
  internalSlots.style = style;
17
+ // 3. Let currency be ? GetOption(options, "currency", "string", undefined, undefined).
16
18
  var currency = (0, GetOption_1.GetOption)(options, 'currency', 'string', undefined, undefined);
17
- if (currency !== undefined && !(0, IsWellFormedCurrencyCode_1.IsWellFormedCurrencyCode)(currency)) {
18
- throw RangeError('Malformed currency code');
19
- }
20
- if (style === 'currency' && currency === undefined) {
21
- throw TypeError('currency cannot be undefined');
22
- }
19
+ // 4. If currency is not undefined, then
20
+ // a. If the result of IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
21
+ (0, utils_1.invariant)(currency === undefined || (0, IsWellFormedCurrencyCode_1.IsWellFormedCurrencyCode)(currency), 'Malformed currency code', RangeError);
22
+ // 5. If style is "currency" and currency is undefined, throw a TypeError exception.
23
+ (0, utils_1.invariant)(style !== 'currency' || currency !== undefined, 'currency cannot be undefined', TypeError);
24
+ // 6. Let currencyDisplay be ? GetOption(options, "currencyDisplay", "string", « "code", "symbol", "narrowSymbol", "name" », "symbol").
23
25
  var currencyDisplay = (0, GetOption_1.GetOption)(options, 'currencyDisplay', 'string', ['code', 'symbol', 'narrowSymbol', 'name'], 'symbol');
26
+ // 7. Let currencySign be ? GetOption(options, "currencySign", "string", « "standard", "accounting" », "standard").
24
27
  var currencySign = (0, GetOption_1.GetOption)(options, 'currencySign', 'string', ['standard', 'accounting'], 'standard');
28
+ // 8. Let unit be ? GetOption(options, "unit", "string", undefined, undefined).
25
29
  var unit = (0, GetOption_1.GetOption)(options, 'unit', 'string', undefined, undefined);
26
- if (unit !== undefined && !(0, IsWellFormedUnitIdentifier_1.IsWellFormedUnitIdentifier)(unit)) {
27
- throw RangeError('Invalid unit argument for Intl.NumberFormat()');
28
- }
29
- if (style === 'unit' && unit === undefined) {
30
- throw TypeError('unit cannot be undefined');
31
- }
30
+ // 9. If unit is not undefined, then
31
+ // a. If the result of IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
32
+ (0, utils_1.invariant)(unit === undefined || (0, IsWellFormedUnitIdentifier_1.IsWellFormedUnitIdentifier)(unit), 'Invalid unit argument for Intl.NumberFormat()', RangeError);
33
+ // 10. If style is "unit" and unit is undefined, throw a TypeError exception.
34
+ (0, utils_1.invariant)(style !== 'unit' || unit !== undefined, 'unit cannot be undefined', TypeError);
35
+ // 11. Let unitDisplay be ? GetOption(options, "unitDisplay", "string", « "short", "narrow", "long" », "short").
32
36
  var unitDisplay = (0, GetOption_1.GetOption)(options, 'unitDisplay', 'string', ['short', 'narrow', 'long'], 'short');
37
+ // 12. If style is "currency", then
33
38
  if (style === 'currency') {
39
+ // a. Set internalSlots.[[Currency]] to the result of converting currency to upper case as specified in 6.1.
34
40
  internalSlots.currency = currency.toUpperCase();
41
+ // b. Set internalSlots.[[CurrencyDisplay]] to currencyDisplay.
35
42
  internalSlots.currencyDisplay = currencyDisplay;
43
+ // c. Set internalSlots.[[CurrencySign]] to currencySign.
36
44
  internalSlots.currencySign = currencySign;
37
45
  }
46
+ // 13. If style is "unit", then
38
47
  if (style === 'unit') {
48
+ // a. Set internalSlots.[[Unit]] to unit.
39
49
  internalSlots.unit = unit;
50
+ // b. Set internalSlots.[[UnitDisplay]] to unitDisplay.
40
51
  internalSlots.unitDisplay = unitDisplay;
41
52
  }
42
53
  }
@@ -1,10 +1,9 @@
1
1
  import Decimal from 'decimal.js';
2
2
  import { RawNumberFormatResult, UnsignedRoundingModeType } from '../types/number';
3
3
  /**
4
- * TODO: dedup with intl-pluralrules and support BigInt
5
4
  * https://tc39.es/ecma402/#sec-torawfixed
6
5
  * @param x a finite non-negative Number or BigInt
7
- * @param minFraction and integer between 0 and 20
8
- * @param maxFraction and integer between 0 and 20
6
+ * @param minFraction an integer between 0 and 20
7
+ * @param maxFraction an integer between 0 and 20
9
8
  */
10
9
  export declare function ToRawFixed(x: Decimal, minFraction: number, maxFraction: number, roundingIncrement: number, unsignedRoundingMode: UnsignedRoundingModeType): RawNumberFormatResult;
@@ -3,17 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ToRawFixed = ToRawFixed;
4
4
  var tslib_1 = require("tslib");
5
5
  var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
- var constants_1 = require("../constants");
7
6
  var utils_1 = require("../utils");
8
7
  var ApplyUnsignedRoundingMode_1 = require("./ApplyUnsignedRoundingMode");
8
+ //IMPL: Setting Decimal configuration
9
9
  decimal_js_1.default.set({
10
10
  toExpPos: 100,
11
11
  });
12
+ //IMPL: Helper function to calculate raw fixed value
12
13
  function ToRawFixedFn(n, f) {
13
- return n.times(constants_1.TEN.pow(-f));
14
+ return n.times(decimal_js_1.default.pow(10, -f));
14
15
  }
16
+ //IMPL: Helper function to find n1 and r1
15
17
  function findN1R1(x, f, roundingIncrement) {
16
- var nx = x.times(constants_1.TEN.pow(f)).floor();
18
+ var nx = x.times(decimal_js_1.default.pow(10, f)).floor();
17
19
  var n1 = nx.div(roundingIncrement).floor().times(roundingIncrement);
18
20
  var r1 = ToRawFixedFn(n1, f);
19
21
  return {
@@ -21,8 +23,9 @@ function findN1R1(x, f, roundingIncrement) {
21
23
  r1: r1,
22
24
  };
23
25
  }
26
+ //IMPL: Helper function to find n2 and r2
24
27
  function findN2R2(x, f, roundingIncrement) {
25
- var nx = x.times(constants_1.TEN.pow(f)).ceil();
28
+ var nx = x.times(decimal_js_1.default.pow(10, f)).ceil();
26
29
  var n2 = nx.div(roundingIncrement).ceil().times(roundingIncrement);
27
30
  var r2 = ToRawFixedFn(n2, f);
28
31
  return {
@@ -31,57 +34,86 @@ function findN2R2(x, f, roundingIncrement) {
31
34
  };
32
35
  }
33
36
  /**
34
- * TODO: dedup with intl-pluralrules and support BigInt
35
37
  * https://tc39.es/ecma402/#sec-torawfixed
36
38
  * @param x a finite non-negative Number or BigInt
37
- * @param minFraction and integer between 0 and 20
38
- * @param maxFraction and integer between 0 and 20
39
+ * @param minFraction an integer between 0 and 20
40
+ * @param maxFraction an integer between 0 and 20
39
41
  */
40
42
  function ToRawFixed(x, minFraction, maxFraction, roundingIncrement, unsignedRoundingMode) {
43
+ // 1. Let f be maxFraction.
41
44
  var f = maxFraction;
45
+ // 2. Let n1 and r1 be the results of performing the maximized rounding of x to f fraction digits.
42
46
  var _a = findN1R1(x, f, roundingIncrement), n1 = _a.n1, r1 = _a.r1;
47
+ // 3. Let n2 and r2 be the results of performing the minimized rounding of x to f fraction digits.
43
48
  var _b = findN2R2(x, f, roundingIncrement), n2 = _b.n2, r2 = _b.r2;
49
+ // 4. Let r be ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode).
44
50
  var r = (0, ApplyUnsignedRoundingMode_1.ApplyUnsignedRoundingMode)(x, r1, r2, unsignedRoundingMode);
45
51
  var n, xFinal;
46
52
  var m;
53
+ // 5. If r is equal to r1, then
47
54
  if (r.eq(r1)) {
55
+ // a. Let n be n1.
48
56
  n = n1;
57
+ // b. Let xFinal be r1.
49
58
  xFinal = r1;
50
59
  }
51
60
  else {
61
+ // 6. Else,
62
+ // a. Let n be n2.
52
63
  n = n2;
64
+ // b. Let xFinal be r2.
53
65
  xFinal = r2;
54
66
  }
67
+ // 7. If n is 0, let m be "0".
55
68
  if (n.isZero()) {
56
69
  m = '0';
57
70
  }
58
71
  else {
72
+ // 8. Else, let m be the String representation of n.
59
73
  m = n.toString();
60
74
  }
61
75
  var int;
76
+ // 9. If f is not 0, then
62
77
  if (f !== 0) {
78
+ // a. Let k be the length of m.
63
79
  var k = m.length;
80
+ // b. If k < f, then
64
81
  if (k <= f) {
82
+ // i. Let z be the String value consisting of f + 1 - k occurrences of the character "0".
65
83
  var z = (0, utils_1.repeat)('0', f - k + 1);
84
+ // ii. Set m to the string-concatenation of z and m.
66
85
  m = z + m;
86
+ // iii. Set k to f + 1.
67
87
  k = f + 1;
68
88
  }
89
+ // c. Let a be the substring of m from 0 to k - f.
69
90
  var a = m.slice(0, k - f);
91
+ // d. Let b be the substring of m from k - f to k.
70
92
  var b = m.slice(m.length - f);
93
+ // e. Set m to the string-concatenation of a, ".", and b.
71
94
  m = a + '.' + b;
95
+ // f. Let int be the length of a.
72
96
  int = a.length;
73
97
  }
74
98
  else {
99
+ // 10. Else, let int be the length of m.
75
100
  int = m.length;
76
101
  }
102
+ // 11. Let cut be maxFraction - minFraction.
77
103
  var cut = maxFraction - minFraction;
104
+ // 12. Repeat, while cut > 0 and the last character of m is "0",
78
105
  while (cut > 0 && m[m.length - 1] === '0') {
106
+ // a. Remove the last character from m.
79
107
  m = m.slice(0, m.length - 1);
108
+ // b. Decrease cut by 1.
80
109
  cut--;
81
110
  }
111
+ // 13. If the last character of m is ".", then
82
112
  if (m[m.length - 1] === '\u002e') {
113
+ // a. Remove the last character from m.
83
114
  m = m.slice(0, m.length - 1);
84
115
  }
116
+ // 14. Return the Record { [[FormattedString]]: m, [[RoundedNumber]]: xFinal, [[IntegerDigitsCount]]: int, [[RoundingMagnitude]]: -f }.
85
117
  return {
86
118
  formattedString: m,
87
119
  roundedNumber: xFinal,
@@ -1,3 +1,9 @@
1
1
  import Decimal from 'decimal.js';
2
2
  import { RawNumberFormatResult, UnsignedRoundingModeType } from '../types/number';
3
+ /**
4
+ * https://tc39.es/ecma402/#sec-torawprecision
5
+ * @param x a finite non-negative Number or BigInt
6
+ * @param minPrecision an integer between 1 and 21
7
+ * @param maxPrecision an integer between 1 and 21
8
+ */
3
9
  export declare function ToRawPrecision(x: Decimal, minPrecision: number, maxPrecision: number, unsignedRoundingMode: UnsignedRoundingModeType): RawNumberFormatResult;
@@ -6,22 +6,16 @@ var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
6
  var constants_1 = require("../constants");
7
7
  var utils_1 = require("../utils");
8
8
  var ApplyUnsignedRoundingMode_1 = require("./ApplyUnsignedRoundingMode");
9
- decimal_js_1.default.set({
10
- toExpPos: 100,
11
- });
12
- function ToRawPrecisionFn(n, e, p) {
13
- (0, utils_1.invariant)(constants_1.TEN.pow(p - 1).lessThanOrEqualTo(n) && n.lessThan(constants_1.TEN.pow(p)), "n should be in the range ".concat(constants_1.TEN.pow(p - 1), " <= n < ").concat(constants_1.TEN.pow(p), " but got ").concat(n));
14
- // n * 10^(e - p + 1)
15
- return n.times(constants_1.TEN.pow(e.minus(p).plus(1)));
16
- }
9
+ //IMPL: Helper function to find n1, e1, and r1
17
10
  function findN1E1R1(x, p) {
18
- var maxN1 = constants_1.TEN.pow(p);
19
- var minN1 = constants_1.TEN.pow(p - 1);
11
+ var maxN1 = decimal_js_1.default.pow(10, p);
12
+ var minN1 = decimal_js_1.default.pow(10, p - 1);
20
13
  var maxE1 = x.div(minN1).log(10).plus(p).minus(1).ceil();
21
- for (var currentE1 = maxE1;; currentE1 = currentE1.minus(1)) {
22
- var currentN1 = x.div(constants_1.TEN.pow(currentE1.minus(p).plus(1))).floor();
14
+ var currentE1 = maxE1;
15
+ while (true) {
16
+ var currentN1 = x.div(decimal_js_1.default.pow(10, currentE1.minus(p).plus(1))).floor();
23
17
  if (currentN1.lessThan(maxN1) && currentN1.greaterThanOrEqualTo(minN1)) {
24
- var currentR1 = ToRawPrecisionFn(currentN1, currentE1, p);
18
+ var currentR1 = currentN1.times(decimal_js_1.default.pow(10, currentE1.minus(p).plus(1)));
25
19
  if (currentR1.lessThanOrEqualTo(x)) {
26
20
  return {
27
21
  n1: currentN1,
@@ -30,16 +24,19 @@ function findN1E1R1(x, p) {
30
24
  };
31
25
  }
32
26
  }
27
+ currentE1 = currentE1.minus(1);
33
28
  }
34
29
  }
30
+ //IMPL: Helper function to find n2, e2, and r2
35
31
  function findN2E2R2(x, p) {
36
- var maxN2 = constants_1.TEN.pow(p);
37
- var minN2 = constants_1.TEN.pow(p - 1);
32
+ var maxN2 = decimal_js_1.default.pow(10, p);
33
+ var minN2 = decimal_js_1.default.pow(10, p - 1);
38
34
  var minE2 = x.div(maxN2).log(10).plus(p).minus(1).floor();
39
- for (var currentE2 = minE2;; currentE2 = currentE2.plus(1)) {
40
- var currentN2 = x.div(constants_1.TEN.pow(currentE2.minus(p).plus(1))).ceil();
35
+ var currentE2 = minE2;
36
+ while (true) {
37
+ var currentN2 = x.div(decimal_js_1.default.pow(10, currentE2.minus(p).plus(1))).ceil();
41
38
  if (currentN2.lessThan(maxN2) && currentN2.greaterThanOrEqualTo(minN2)) {
42
- var currentR2 = ToRawPrecisionFn(currentN2, currentE2, p);
39
+ var currentR2 = currentN2.times(decimal_js_1.default.pow(10, currentE2.minus(p).plus(1)));
43
40
  if (currentR2.greaterThanOrEqualTo(x)) {
44
41
  return {
45
42
  n2: currentN2,
@@ -48,59 +45,104 @@ function findN2E2R2(x, p) {
48
45
  };
49
46
  }
50
47
  }
48
+ currentE2 = currentE2.plus(1);
51
49
  }
52
50
  }
51
+ /**
52
+ * https://tc39.es/ecma402/#sec-torawprecision
53
+ * @param x a finite non-negative Number or BigInt
54
+ * @param minPrecision an integer between 1 and 21
55
+ * @param maxPrecision an integer between 1 and 21
56
+ */
53
57
  function ToRawPrecision(x, minPrecision, maxPrecision, unsignedRoundingMode) {
58
+ // 1. Let p be maxPrecision.
54
59
  var p = maxPrecision;
55
60
  var m;
56
61
  var e;
57
62
  var xFinal;
63
+ // 2. If x = 0, then
58
64
  if (x.isZero()) {
65
+ // a. Let m be the String value consisting of p occurrences of the character "0".
59
66
  m = (0, utils_1.repeat)('0', p);
67
+ // b. Let e be 0.
60
68
  e = 0;
69
+ // c. Let xFinal be 0.
61
70
  xFinal = constants_1.ZERO;
62
71
  }
63
72
  else {
73
+ // 3. Else,
74
+ // a. Let {n1, e1, r1} be the result of findN1E1R1(x, p).
64
75
  var _a = findN1E1R1(x, p), n1 = _a.n1, e1 = _a.e1, r1 = _a.r1;
76
+ // b. Let {n2, e2, r2} be the result of findN2E2R2(x, p).
65
77
  var _b = findN2E2R2(x, p), n2 = _b.n2, e2 = _b.e2, r2 = _b.r2;
78
+ // c. Let r be ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode).
66
79
  var r = (0, ApplyUnsignedRoundingMode_1.ApplyUnsignedRoundingMode)(x, r1, r2, unsignedRoundingMode);
67
- var n = void 0;
80
+ var n
81
+ // d. If r = r1, then
82
+ = void 0;
83
+ // d. If r = r1, then
68
84
  if (r.eq(r1)) {
85
+ // i. Let n be n1.
69
86
  n = n1;
87
+ // ii. Let e be e1.
70
88
  e = e1.toNumber();
89
+ // iii. Let xFinal be r1.
71
90
  xFinal = r1;
72
91
  }
73
92
  else {
93
+ // e. Else,
94
+ // i. Let n be n2.
74
95
  n = n2;
96
+ // ii. Let e be e2.
75
97
  e = e2.toNumber();
98
+ // iii. Let xFinal be r2.
76
99
  xFinal = r2;
77
100
  }
101
+ // f. Let m be the String representation of n.
78
102
  m = n.toString();
79
103
  }
80
104
  var int;
105
+ // 4. If e ≥ p - 1, then
81
106
  if (e >= p - 1) {
107
+ // a. Let m be the string-concatenation of m and p - 1 - e occurrences of the character "0".
82
108
  m = m + (0, utils_1.repeat)('0', e - p + 1);
109
+ // b. Let int be e + 1.
83
110
  int = e + 1;
84
111
  }
85
112
  else if (e >= 0) {
113
+ // 5. Else if e ≥ 0, then
114
+ // a. Let m be the string-concatenation of the first e + 1 characters of m, ".", and the remaining p - (e + 1) characters of m.
86
115
  m = m.slice(0, e + 1) + '.' + m.slice(m.length - (p - (e + 1)));
116
+ // b. Let int be e + 1.
87
117
  int = e + 1;
88
118
  }
89
119
  else {
120
+ // 6. Else,
121
+ // a. Assert: e < 0.
90
122
  (0, utils_1.invariant)(e < 0, 'e should be less than 0');
123
+ // b. Let m be the string-concatenation of "0.", -e - 1 occurrences of the character "0", and m.
91
124
  m = '0.' + (0, utils_1.repeat)('0', -e - 1) + m;
125
+ // c. Let int be 1.
92
126
  int = 1;
93
127
  }
128
+ // 7. If m contains ".", and maxPrecision > minPrecision, then
94
129
  if (m.includes('.') && maxPrecision > minPrecision) {
130
+ // a. Let cut be maxPrecision - minPrecision.
95
131
  var cut = maxPrecision - minPrecision;
132
+ // b. Repeat, while cut > 0 and the last character of m is "0",
96
133
  while (cut > 0 && m[m.length - 1] === '0') {
134
+ // i. Remove the last character from m.
97
135
  m = m.slice(0, m.length - 1);
136
+ // ii. Decrease cut by 1.
98
137
  cut--;
99
138
  }
139
+ // c. If the last character of m is ".", then
100
140
  if (m[m.length - 1] === '.') {
141
+ // i. Remove the last character from m.
101
142
  m = m.slice(0, m.length - 1);
102
143
  }
103
144
  }
145
+ // 8. Return the Record { [[FormattedString]]: m, [[RoundedNumber]]: xFinal, [[IntegerDigitsCount]]: int, [[RoundingMagnitude]]: e }.
104
146
  return {
105
147
  formattedString: m,
106
148
  roundedNumber: xFinal,
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = formatToParts;
4
4
  var tslib_1 = require("tslib");
5
5
  var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
- var constants_1 = require("../constants");
7
6
  var regex_generated_1 = require("../regex.generated");
8
7
  var digit_mapping_generated_1 = require("./digit-mapping.generated");
9
8
  var GetUnsignedRoundingMode_1 = require("./GetUnsignedRoundingMode");
@@ -168,7 +167,9 @@ function formatToParts(numberResult, data, pl, options) {
168
167
  var unitName = void 0;
169
168
  var currencyNameData = data.currencies[options.currency];
170
169
  if (currencyNameData) {
171
- unitName = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), currencyNameData.displayName);
170
+ unitName = selectPlural(pl, numberResult.roundedNumber
171
+ .times(decimal_js_1.default.pow(10, exponent))
172
+ .toNumber(), currencyNameData.displayName);
172
173
  }
173
174
  else {
174
175
  // Fallback for unknown currency
@@ -205,7 +206,9 @@ function formatToParts(numberResult, data, pl, options) {
205
206
  var unitPattern = void 0;
206
207
  if (unitData) {
207
208
  // Simple unit pattern
208
- unitPattern = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), data.units.simple[unit][unitDisplay]);
209
+ unitPattern = selectPlural(pl, numberResult.roundedNumber
210
+ .times(decimal_js_1.default.pow(10, exponent))
211
+ .toNumber(), data.units.simple[unit][unitDisplay]);
209
212
  }
210
213
  else {
211
214
  // See: http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
@@ -213,7 +216,9 @@ function formatToParts(numberResult, data, pl, options) {
213
216
  // Implementation note: we are not following TR-35 here because we need to format to parts!
214
217
  var _c = unit.split('-per-'), numeratorUnit = _c[0], denominatorUnit = _c[1];
215
218
  unitData = data.units.simple[numeratorUnit];
216
- var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
219
+ var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber
220
+ .times(decimal_js_1.default.pow(10, exponent))
221
+ .toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
217
222
  var perUnitPattern = data.units.simple[denominatorUnit].perUnit[unitDisplay];
218
223
  if (perUnitPattern) {
219
224
  // perUnitPattern exists, combine it with numeratorUnitPattern
@@ -0,0 +1,2 @@
1
+ import Decimal from 'decimal.js';
2
+ export declare function ToIntlMathematicalValue(input: unknown): Decimal;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ToIntlMathematicalValue = ToIntlMathematicalValue;
4
+ var tslib_1 = require("tslib");
5
+ var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
+ var _262_1 = require("./262");
7
+ function ToIntlMathematicalValue(input) {
8
+ var primValue = (0, _262_1.ToPrimitive)(input, 'number');
9
+ if (typeof primValue === 'bigint') {
10
+ return new decimal_js_1.default(primValue);
11
+ }
12
+ // IMPL
13
+ if (primValue === undefined) {
14
+ return new decimal_js_1.default(NaN);
15
+ }
16
+ if (primValue === true) {
17
+ return new decimal_js_1.default(1);
18
+ }
19
+ if (primValue === false) {
20
+ return new decimal_js_1.default(0);
21
+ }
22
+ if (primValue === null) {
23
+ return new decimal_js_1.default(0);
24
+ }
25
+ try {
26
+ return new decimal_js_1.default(primValue);
27
+ }
28
+ catch (e) {
29
+ return new decimal_js_1.default(NaN);
30
+ }
31
+ }