@formatjs/ecma402-abstract 2.3.1 → 2.3.3

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
package/262.d.ts CHANGED
@@ -7,12 +7,12 @@ export declare function ToString(o: unknown): string;
7
7
  * https://tc39.es/ecma262/#sec-tonumber
8
8
  * @param val
9
9
  */
10
- export declare function ToNumber(val: any): Decimal;
10
+ export declare function ToNumber(arg: any): Decimal;
11
11
  /**
12
12
  * https://tc39.es/ecma262/#sec-timeclip
13
13
  * @param time
14
14
  */
15
- export declare function TimeClip(time: number): number;
15
+ export declare function TimeClip(time: Decimal): Decimal;
16
16
  /**
17
17
  * https://tc39.es/ecma262/#sec-toobject
18
18
  * @param arg
@@ -90,3 +90,4 @@ export declare function OrdinaryHasInstance(C: Object, O: any, internalSlots?: {
90
90
  boundTargetFunction: any;
91
91
  }): boolean;
92
92
  export declare function msFromTime(t: number): number;
93
+ export declare function ToPrimitive<T extends 'string' | 'number' = 'string' | 'number'>(input: any, preferredType: T): string | number | boolean | undefined | null;
package/262.js CHANGED
@@ -23,8 +23,10 @@ exports.MinFromTime = MinFromTime;
23
23
  exports.SecFromTime = SecFromTime;
24
24
  exports.OrdinaryHasInstance = OrdinaryHasInstance;
25
25
  exports.msFromTime = msFromTime;
26
+ exports.ToPrimitive = ToPrimitive;
26
27
  var decimal_js_1 = require("decimal.js");
27
28
  var constants_1 = require("./constants");
29
+ var utils_1 = require("./utils");
28
30
  /**
29
31
  * https://tc39.es/ecma262/#sec-tostring
30
32
  */
@@ -39,20 +41,32 @@ function ToString(o) {
39
41
  * https://tc39.es/ecma262/#sec-tonumber
40
42
  * @param val
41
43
  */
42
- function ToNumber(val) {
43
- if (val === undefined) {
44
+ function ToNumber(arg) {
45
+ if (typeof arg === 'number') {
46
+ return new decimal_js_1.Decimal(arg);
47
+ }
48
+ (0, utils_1.invariant)(typeof arg !== 'bigint' && typeof arg !== 'symbol', 'BigInt and Symbol are not supported', TypeError);
49
+ if (arg === undefined) {
44
50
  return new decimal_js_1.Decimal(NaN);
45
51
  }
46
- if (val === null) {
52
+ if (arg === null || arg === 0) {
47
53
  return constants_1.ZERO;
48
54
  }
49
- if (typeof val === 'boolean') {
50
- return new decimal_js_1.Decimal(val ? 1 : 0);
51
- }
52
- if (typeof val === 'symbol' || typeof val === 'bigint') {
53
- throw new TypeError('Cannot convert symbol/bigint to number');
54
- }
55
- return new decimal_js_1.Decimal(Number(val));
55
+ if (arg === true) {
56
+ return new decimal_js_1.Decimal(1);
57
+ }
58
+ if (typeof arg === 'string') {
59
+ try {
60
+ return new decimal_js_1.Decimal(arg);
61
+ }
62
+ catch (e) {
63
+ return new decimal_js_1.Decimal(NaN);
64
+ }
65
+ }
66
+ (0, utils_1.invariant)(typeof arg === 'object', 'object expected', TypeError);
67
+ var primValue = ToPrimitive(arg, 'number');
68
+ (0, utils_1.invariant)(typeof primValue !== 'object', 'object expected', TypeError);
69
+ return ToNumber(primValue);
56
70
  }
57
71
  /**
58
72
  * https://tc39.es/ecma262/#sec-tointeger
@@ -77,13 +91,13 @@ function ToInteger(n) {
77
91
  * @param time
78
92
  */
79
93
  function TimeClip(time) {
80
- if (!isFinite(time)) {
81
- return NaN;
94
+ if (!time.isFinite()) {
95
+ return new decimal_js_1.Decimal(NaN);
82
96
  }
83
- if (Math.abs(time) > 8.64 * 1e15) {
84
- return NaN;
97
+ if (time.abs().greaterThan(8.64 * 1e15)) {
98
+ return new decimal_js_1.Decimal(NaN);
85
99
  }
86
- return ToInteger(time).toNumber();
100
+ return ToInteger(time);
87
101
  }
88
102
  /**
89
103
  * https://tc39.es/ecma262/#sec-toobject
@@ -355,3 +369,51 @@ function OrdinaryHasInstance(C, O, internalSlots) {
355
369
  function msFromTime(t) {
356
370
  return mod(t, MS_PER_SECOND);
357
371
  }
372
+ function OrdinaryToPrimitive(O, hint) {
373
+ var methodNames;
374
+ if (hint === 'string') {
375
+ methodNames = ['toString', 'valueOf'];
376
+ }
377
+ else {
378
+ methodNames = ['valueOf', 'toString'];
379
+ }
380
+ for (var _i = 0, methodNames_1 = methodNames; _i < methodNames_1.length; _i++) {
381
+ var name_1 = methodNames_1[_i];
382
+ var method = O[name_1];
383
+ if (IsCallable(method)) {
384
+ var result = method.call(O);
385
+ if (typeof result !== 'object') {
386
+ return result;
387
+ }
388
+ }
389
+ }
390
+ throw new TypeError('Cannot convert object to primitive value');
391
+ }
392
+ function ToPrimitive(input, preferredType) {
393
+ if (typeof input === 'object' && input != null) {
394
+ var exoticToPrim = Symbol.toPrimitive in input ? input[Symbol.toPrimitive] : undefined;
395
+ var hint = void 0;
396
+ if (exoticToPrim !== undefined) {
397
+ if (preferredType === undefined) {
398
+ hint = 'default';
399
+ }
400
+ else if (preferredType === 'string') {
401
+ hint = 'string';
402
+ }
403
+ else {
404
+ (0, utils_1.invariant)(preferredType === 'number', 'preferredType must be "string" or "number"');
405
+ hint = 'number';
406
+ }
407
+ var result = exoticToPrim.call(input, hint);
408
+ if (typeof result !== 'object') {
409
+ return result;
410
+ }
411
+ throw new TypeError('Cannot convert exotic object to primitive.');
412
+ }
413
+ if (preferredType === undefined) {
414
+ preferredType = 'number';
415
+ }
416
+ return OrdinaryToPrimitive(input, preferredType);
417
+ }
418
+ return input;
419
+ }
@@ -7,6 +7,4 @@ import { NumberFormatInternal } from '../types/number';
7
7
  *
8
8
  * NOT IN SPEC: it returns [exponent, magnitude].
9
9
  */
10
- export declare function ComputeExponent(numberFormat: Intl.NumberFormat, x: Decimal, { getInternalSlots, }: {
11
- getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
12
- }): [number, number];
10
+ export declare function ComputeExponent(internalSlots: NumberFormatInternal, x: Decimal): [number, number];
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ComputeExponent = ComputeExponent;
4
- var constants_1 = require("../constants");
4
+ var tslib_1 = require("tslib");
5
+ var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
5
6
  var ComputeExponentForMagnitude_1 = require("./ComputeExponentForMagnitude");
6
7
  var FormatNumericToString_1 = require("./FormatNumericToString");
7
8
  /**
@@ -11,8 +12,7 @@ var FormatNumericToString_1 = require("./FormatNumericToString");
11
12
  *
12
13
  * NOT IN SPEC: it returns [exponent, magnitude].
13
14
  */
14
- function ComputeExponent(numberFormat, x, _a) {
15
- var getInternalSlots = _a.getInternalSlots;
15
+ function ComputeExponent(internalSlots, x) {
16
16
  if (x.isZero()) {
17
17
  return [0, 0];
18
18
  }
@@ -20,12 +20,10 @@ function ComputeExponent(numberFormat, x, _a) {
20
20
  x = x.negated();
21
21
  }
22
22
  var magnitude = x.log(10).floor();
23
- var exponent = (0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(numberFormat, magnitude, {
24
- getInternalSlots: getInternalSlots,
25
- });
23
+ var exponent = (0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(internalSlots, magnitude);
26
24
  // Preserve more precision by doing multiplication when exponent is negative.
27
- x = x.times(constants_1.TEN.pow(-exponent));
28
- var formatNumberResult = (0, FormatNumericToString_1.FormatNumericToString)(getInternalSlots(numberFormat), x);
25
+ x = x.times(decimal_js_1.default.pow(10, -exponent));
26
+ var formatNumberResult = (0, FormatNumericToString_1.FormatNumericToString)(internalSlots, x);
29
27
  if (formatNumberResult.roundedNumber.isZero()) {
30
28
  return [exponent, magnitude.toNumber()];
31
29
  }
@@ -34,9 +32,7 @@ function ComputeExponent(numberFormat, x, _a) {
34
32
  return [exponent, magnitude.toNumber()];
35
33
  }
36
34
  return [
37
- (0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(numberFormat, magnitude.plus(1), {
38
- getInternalSlots: getInternalSlots,
39
- }),
35
+ (0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(internalSlots, magnitude.plus(1)),
40
36
  magnitude.plus(1).toNumber(),
41
37
  ];
42
38
  }
@@ -5,6 +5,4 @@ import { NumberFormatInternal } from '../types/number';
5
5
  * number of the given magnitude (power of ten of the most significant digit) according to the
6
6
  * locale and the desired notation (scientific, engineering, or compact).
7
7
  */
8
- export declare function ComputeExponentForMagnitude(numberFormat: Intl.NumberFormat, magnitude: Decimal, { getInternalSlots, }: {
9
- getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
10
- }): number;
8
+ export declare function ComputeExponentForMagnitude(internalSlots: NumberFormatInternal, magnitude: Decimal): number;
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ComputeExponentForMagnitude = ComputeExponentForMagnitude;
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
  decimal_js_1.default.set({
9
8
  toExpPos: 100,
@@ -13,9 +12,7 @@ decimal_js_1.default.set({
13
12
  * number of the given magnitude (power of ten of the most significant digit) according to the
14
13
  * locale and the desired notation (scientific, engineering, or compact).
15
14
  */
16
- function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
17
- var getInternalSlots = _a.getInternalSlots;
18
- var internalSlots = getInternalSlots(numberFormat);
15
+ function ComputeExponentForMagnitude(internalSlots, magnitude) {
19
16
  var notation = internalSlots.notation, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
20
17
  switch (notation) {
21
18
  case 'standard':
@@ -44,7 +41,7 @@ function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
44
41
  if (!thresholdMap) {
45
42
  return 0;
46
43
  }
47
- var num = constants_1.TEN.pow(magnitude).toString();
44
+ var num = decimal_js_1.default.pow(10, magnitude).toString();
48
45
  var thresholds = Object.keys(thresholdMap); // TODO: this can be pre-processed
49
46
  if (num < thresholds[0]) {
50
47
  return 0;
@@ -2,6 +2,4 @@ import { NumberFormatInternal, NumberFormatPart } from '../types/number';
2
2
  /**
3
3
  * https://tc39.es/ecma402/#sec-formatapproximately
4
4
  */
5
- export declare function FormatApproximately(numberFormat: Intl.NumberFormat, result: NumberFormatPart[], { getInternalSlots, }: {
6
- getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
7
- }): NumberFormatPart[];
5
+ export declare function FormatApproximately(internalSlots: NumberFormatInternal, result: NumberFormatPart[]): NumberFormatPart[];
@@ -4,9 +4,7 @@ exports.FormatApproximately = FormatApproximately;
4
4
  /**
5
5
  * https://tc39.es/ecma402/#sec-formatapproximately
6
6
  */
7
- function FormatApproximately(numberFormat, result, _a) {
8
- var getInternalSlots = _a.getInternalSlots;
9
- var internalSlots = getInternalSlots(numberFormat);
7
+ function FormatApproximately(internalSlots, result) {
10
8
  var symbols = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem];
11
9
  var approximatelySign = symbols.approximatelySign;
12
10
  result.push({ type: 'approximatelySign', value: approximatelySign });
@@ -0,0 +1,3 @@
1
+ import Decimal from 'decimal.js';
2
+ import { NumberFormatInternal } from '../types/number';
3
+ export declare function FormatNumeric(internalSlots: NumberFormatInternal, x: Decimal): string;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FormatNumeric = FormatNumeric;
4
+ var PartitionNumberPattern_1 = require("./PartitionNumberPattern");
5
+ function FormatNumeric(internalSlots, x) {
6
+ var parts = (0, PartitionNumberPattern_1.PartitionNumberPattern)(internalSlots, x);
7
+ return parts.map(function (p) { return p.value; }).join('');
8
+ }
@@ -4,7 +4,7 @@ exports.FormatNumericToParts = FormatNumericToParts;
4
4
  var _262_1 = require("../262");
5
5
  var PartitionNumberPattern_1 = require("./PartitionNumberPattern");
6
6
  function FormatNumericToParts(nf, x, implDetails) {
7
- var parts = (0, PartitionNumberPattern_1.PartitionNumberPattern)(nf, x, implDetails);
7
+ var parts = (0, PartitionNumberPattern_1.PartitionNumberPattern)(implDetails.getInternalSlots(nf), x);
8
8
  var result = (0, _262_1.ArrayCreate)(0);
9
9
  for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
10
10
  var part = parts_1[_i];
@@ -3,7 +3,7 @@ import { NumberFormatDigitInternalSlots } from '../types/number';
3
3
  /**
4
4
  * https://tc39.es/ecma402/#sec-formatnumberstring
5
5
  */
6
- export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits' | 'roundingIncrement' | 'roundingMode' | 'trailingZeroDisplay'>, x: Decimal): {
6
+ export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits' | 'roundingIncrement' | 'roundingMode' | 'trailingZeroDisplay'>, _x: Decimal): {
7
7
  roundedNumber: Decimal;
8
8
  formattedString: string;
9
9
  };
@@ -9,7 +9,8 @@ var ToRawPrecision_1 = require("./ToRawPrecision");
9
9
  /**
10
10
  * https://tc39.es/ecma402/#sec-formatnumberstring
11
11
  */
12
- function FormatNumericToString(intlObject, x) {
12
+ function FormatNumericToString(intlObject, _x) {
13
+ var x = _x;
13
14
  var sign;
14
15
  // -0
15
16
  if (x.isZero() && x.isNegative()) {
@@ -38,7 +38,7 @@ function InitializeNumberFormat(nf, locales, opts, _a) {
38
38
  internalSlots.dataLocale = r.dataLocale;
39
39
  internalSlots.numberingSystem = r.nu;
40
40
  internalSlots.dataLocaleData = dataLocaleData;
41
- (0, SetNumberFormatUnitOptions_1.SetNumberFormatUnitOptions)(nf, options, { getInternalSlots: getInternalSlots });
41
+ (0, SetNumberFormatUnitOptions_1.SetNumberFormatUnitOptions)(internalSlots, options);
42
42
  var style = internalSlots.style;
43
43
  var notation = (0, GetOption_1.GetOption)(options, 'notation', 'string', ['standard', 'scientific', 'engineering', 'compact'], 'standard');
44
44
  internalSlots.notation = notation;
@@ -1,8 +1,6 @@
1
1
  import Decimal from 'decimal.js';
2
2
  import { NumberFormatInternal, NumberFormatPart } from '../types/number';
3
3
  /**
4
- * https://tc39.es/ecma402/#sec-formatnumberstring
4
+ * https://tc39.es/ecma402/#sec-partitionnumberpattern
5
5
  */
6
- export declare function PartitionNumberPattern(numberFormat: Intl.NumberFormat, x: Decimal, { getInternalSlots, }: {
7
- getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
8
- }): NumberFormatPart[];
6
+ export declare function PartitionNumberPattern(internalSlots: NumberFormatInternal, _x: Decimal): NumberFormatPart[];
@@ -2,90 +2,129 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PartitionNumberPattern = PartitionNumberPattern;
4
4
  var tslib_1 = require("tslib");
5
- var constants_1 = require("../constants");
5
+ var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
6
  var utils_1 = require("../utils");
7
7
  var ComputeExponent_1 = require("./ComputeExponent");
8
8
  var format_to_parts_1 = tslib_1.__importDefault(require("./format_to_parts"));
9
9
  var FormatNumericToString_1 = require("./FormatNumericToString");
10
10
  /**
11
- * https://tc39.es/ecma402/#sec-formatnumberstring
11
+ * https://tc39.es/ecma402/#sec-partitionnumberpattern
12
12
  */
13
- function PartitionNumberPattern(numberFormat, x, _a) {
14
- var _b;
15
- var getInternalSlots = _a.getInternalSlots;
16
- var internalSlots = getInternalSlots(numberFormat);
13
+ function PartitionNumberPattern(internalSlots, _x) {
14
+ var _a;
15
+ var x = _x;
16
+ // IMPL: We need to record the magnitude of the number
17
+ var magnitude = 0;
18
+ // 2. Let dataLocaleData be internalSlots.[[dataLocaleData]].
17
19
  var pl = internalSlots.pl, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
20
+ // 3. Let symbols be dataLocaleData.[[numbers]].[[symbols]][internalSlots.[[numberingSystem]]].
18
21
  var symbols = dataLocaleData.numbers.symbols[numberingSystem] ||
19
22
  dataLocaleData.numbers.symbols[dataLocaleData.numbers.nu[0]];
20
- var magnitude = 0;
23
+ // 4. Let exponent be 0.
21
24
  var exponent = 0;
25
+ // 5. Let n be ! ToString(x).
22
26
  var n;
27
+ // 6. If x is NaN, then
23
28
  if (x.isNaN()) {
29
+ // 6.a. Let n be symbols.[[nan]].
24
30
  n = symbols.nan;
25
31
  }
26
32
  else if (!x.isFinite()) {
33
+ // 7. Else if x is a non-finite Number, then
34
+ // 7.a. Let n be symbols.[[infinity]].
27
35
  n = symbols.infinity;
28
36
  }
29
37
  else {
38
+ // 8. Else,
30
39
  if (!x.isZero()) {
40
+ // 8.a. If x < 0, let x be -x.
31
41
  (0, utils_1.invariant)(x.isFinite(), 'Input must be a mathematical value');
42
+ // 8.b. If internalSlots.[[style]] is "percent", let x be 100 × x.
32
43
  if (internalSlots.style == 'percent') {
33
44
  x = x.times(100);
34
45
  }
46
+ // 8.c. Let exponent be ComputeExponent(numberFormat, x).
35
47
  ;
36
- _b = (0, ComputeExponent_1.ComputeExponent)(numberFormat, x, {
37
- getInternalSlots: getInternalSlots,
38
- }), exponent = _b[0], magnitude = _b[1];
39
- x = x.times(constants_1.TEN.pow(-exponent));
48
+ _a = (0, ComputeExponent_1.ComputeExponent)(internalSlots, x), exponent = _a[0],
49
+ // IMPL: We need to record the magnitude of the number
50
+ magnitude = _a[1];
51
+ // 8.d. Let x be x × 10^(-exponent).
52
+ x = x.times(decimal_js_1.default.pow(10, -exponent));
40
53
  }
54
+ // 8.e. Let formatNumberResult be FormatNumericToString(internalSlots, x).
41
55
  var formatNumberResult = (0, FormatNumericToString_1.FormatNumericToString)(internalSlots, x);
56
+ // 8.f. Let n be formatNumberResult.[[formattedString]].
42
57
  n = formatNumberResult.formattedString;
58
+ // 8.g. Let x be formatNumberResult.[[roundedNumber]].
43
59
  x = formatNumberResult.roundedNumber;
44
60
  }
45
- // Based on https://tc39.es/ecma402/#sec-getnumberformatpattern
46
- // We need to do this before `x` is rounded.
61
+ // 9. Let sign be 0.
47
62
  var sign;
63
+ // 10. If x is negative, then
48
64
  var signDisplay = internalSlots.signDisplay;
49
65
  switch (signDisplay) {
50
66
  case 'never':
67
+ // 10.a. If internalSlots.[[signDisplay]] is "never", then
68
+ // 10.a.i. Let sign be 0.
51
69
  sign = 0;
52
70
  break;
53
71
  case 'auto':
72
+ // 10.b. Else if internalSlots.[[signDisplay]] is "auto", then
54
73
  if (x.isPositive() || x.isNaN()) {
74
+ // 10.b.i. If x is positive or x is NaN, let sign be 0.
55
75
  sign = 0;
56
76
  }
57
77
  else {
78
+ // 10.b.ii. Else, let sign be -1.
58
79
  sign = -1;
59
80
  }
60
81
  break;
61
82
  case 'always':
83
+ // 10.c. Else if internalSlots.[[signDisplay]] is "always", then
62
84
  if (x.isPositive() || x.isNaN()) {
85
+ // 10.c.i. If x is positive or x is NaN, let sign be 1.
63
86
  sign = 1;
64
87
  }
65
88
  else {
89
+ // 10.c.ii. Else, let sign be -1.
66
90
  sign = -1;
67
91
  }
68
92
  break;
69
93
  case 'exceptZero':
94
+ // 10.d. Else if internalSlots.[[signDisplay]] is "exceptZero", then
70
95
  if (x.isZero()) {
96
+ // 10.d.i. If x is 0, let sign be 0.
71
97
  sign = 0;
72
98
  }
73
99
  else if (x.isNegative()) {
100
+ // 10.d.ii. Else if x is negative, let sign be -1.
74
101
  sign = -1;
75
102
  }
76
103
  else {
104
+ // 10.d.iii. Else, let sign be 1.
77
105
  sign = 1;
78
106
  }
79
107
  break;
80
108
  default:
109
+ // 10.e. Else,
81
110
  (0, utils_1.invariant)(signDisplay === 'negative', 'signDisplay must be "negative"');
82
111
  if (x.isNegative() && !x.isZero()) {
112
+ // 10.e.i. If x is negative and x is not 0, let sign be -1.
83
113
  sign = -1;
84
114
  }
85
115
  else {
116
+ // 10.e.ii. Else, let sign be 0.
86
117
  sign = 0;
87
118
  }
88
119
  break;
89
120
  }
90
- return (0, format_to_parts_1.default)({ roundedNumber: x, formattedString: n, exponent: exponent, magnitude: magnitude, sign: sign }, internalSlots.dataLocaleData, pl, internalSlots);
121
+ // 11. Return ? FormatNumberToParts(numberFormat, x, n, exponent, sign).
122
+ return (0, format_to_parts_1.default)({
123
+ roundedNumber: x,
124
+ formattedString: n,
125
+ exponent: exponent,
126
+ // IMPL: We're returning this for our implementation of formatToParts
127
+ magnitude: magnitude,
128
+ sign: sign,
129
+ }, internalSlots.dataLocaleData, pl, internalSlots);
91
130
  }
@@ -4,32 +4,40 @@ exports.PartitionNumberRangePattern = PartitionNumberRangePattern;
4
4
  var utils_1 = require("../utils");
5
5
  var CollapseNumberRange_1 = require("./CollapseNumberRange");
6
6
  var FormatApproximately_1 = require("./FormatApproximately");
7
+ var FormatNumeric_1 = require("./FormatNumeric");
7
8
  var PartitionNumberPattern_1 = require("./PartitionNumberPattern");
8
9
  /**
9
10
  * https://tc39.es/ecma402/#sec-partitionnumberrangepattern
10
11
  */
11
12
  function PartitionNumberRangePattern(numberFormat, x, y, _a) {
12
13
  var getInternalSlots = _a.getInternalSlots;
13
- (0, utils_1.invariant)(!x.isNaN() && !y.isNaN(), 'Input must be a number');
14
- var result = [];
15
- var xResult = (0, PartitionNumberPattern_1.PartitionNumberPattern)(numberFormat, x, { getInternalSlots: getInternalSlots });
16
- var yResult = (0, PartitionNumberPattern_1.PartitionNumberPattern)(numberFormat, y, { getInternalSlots: getInternalSlots });
17
- if (xResult === yResult) {
18
- return (0, FormatApproximately_1.FormatApproximately)(numberFormat, xResult, { getInternalSlots: getInternalSlots });
19
- }
20
- for (var _i = 0, xResult_1 = xResult; _i < xResult_1.length; _i++) {
21
- var r = xResult_1[_i];
22
- r.source = 'startRange';
23
- }
24
- result = result.concat(xResult);
14
+ // 1. Assert: x and y are both mathematical values.
15
+ (0, utils_1.invariant)(!x.isNaN() && !y.isNaN(), 'Input must be a number', RangeError);
25
16
  var internalSlots = getInternalSlots(numberFormat);
26
- var symbols = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem];
27
- result.push({ type: 'literal', value: symbols.rangeSign, source: 'shared' });
28
- for (var _b = 0, yResult_1 = yResult; _b < yResult_1.length; _b++) {
29
- var r = yResult_1[_b];
30
- r.source = 'endRange';
17
+ // 3. Let xResult be ? PartitionNumberPattern(numberFormat, x).
18
+ var xResult = (0, PartitionNumberPattern_1.PartitionNumberPattern)(internalSlots, x);
19
+ // 4. Let yResult be ? PartitionNumberPattern(numberFormat, y).
20
+ var yResult = (0, PartitionNumberPattern_1.PartitionNumberPattern)(internalSlots, y);
21
+ if ((0, FormatNumeric_1.FormatNumeric)(internalSlots, x) === (0, FormatNumeric_1.FormatNumeric)(internalSlots, y)) {
22
+ var appxResult = (0, FormatApproximately_1.FormatApproximately)(internalSlots, xResult);
23
+ appxResult.forEach(function (el) {
24
+ el.source = 'shared';
25
+ });
26
+ return appxResult;
31
27
  }
32
- result = result.concat(yResult);
28
+ var result = [];
29
+ xResult.forEach(function (el) {
30
+ el.source = 'startRange';
31
+ result.push(el);
32
+ });
33
+ // 9. Let symbols be internalSlots.[[dataLocaleData]].[[numbers]].[[symbols]][internalSlots.[[numberingSystem]]].
34
+ var rangeSeparator = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem]
35
+ .rangeSign;
36
+ result.push({ type: 'literal', value: rangeSeparator, source: 'shared' });
37
+ yResult.forEach(function (el) {
38
+ el.source = 'endRange';
39
+ result.push(el);
40
+ });
41
+ // 13. Return ? CollapseNumberRange(numberFormat, result).
33
42
  return (0, CollapseNumberRange_1.CollapseNumberRange)(numberFormat, result, { getInternalSlots: getInternalSlots });
34
- // TODO: Needs to implement Range Pattern Processing https://unicode-org.github.io/cldr/ldml/tr35-numbers.html#range-pattern-processing
35
43
  }