@formatjs/ecma402-abstract 2.2.4 → 2.3.0

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 (163) hide show
  1. package/262.d.ts +5 -4
  2. package/262.js +13 -17
  3. package/CanonicalizeLocaleList.d.ts +1 -1
  4. package/IsSanctionedSimpleUnitIdentifier.d.ts +1 -1
  5. package/NumberFormat/ApplyUnsignedRoundingMode.d.ts +2 -1
  6. package/NumberFormat/ApplyUnsignedRoundingMode.js +11 -16
  7. package/NumberFormat/ComputeExponent.d.ts +2 -1
  8. package/NumberFormat/ComputeExponent.js +13 -13
  9. package/NumberFormat/ComputeExponentForMagnitude.d.ts +2 -1
  10. package/NumberFormat/ComputeExponentForMagnitude.js +12 -3
  11. package/NumberFormat/FormatNumericRange.d.ts +2 -1
  12. package/NumberFormat/FormatNumericRangeToParts.d.ts +2 -1
  13. package/NumberFormat/FormatNumericToParts.d.ts +2 -1
  14. package/NumberFormat/FormatNumericToParts.js +1 -1
  15. package/NumberFormat/FormatNumericToString.d.ts +3 -2
  16. package/NumberFormat/FormatNumericToString.js +56 -14
  17. package/NumberFormat/InitializeNumberFormat.js +7 -36
  18. package/NumberFormat/PartitionNumberPattern.d.ts +4 -3
  19. package/NumberFormat/PartitionNumberPattern.js +25 -18
  20. package/NumberFormat/PartitionNumberRangePattern.d.ts +2 -1
  21. package/NumberFormat/PartitionNumberRangePattern.js +2 -3
  22. package/NumberFormat/SetNumberFormatDigitOptions.js +53 -24
  23. package/NumberFormat/ToRawFixed.d.ts +3 -2
  24. package/NumberFormat/ToRawFixed.js +55 -18
  25. package/NumberFormat/ToRawPrecision.d.ts +3 -2
  26. package/NumberFormat/ToRawPrecision.js +75 -42
  27. package/NumberFormat/format_to_parts.d.ts +5 -2
  28. package/NumberFormat/format_to_parts.js +20 -15
  29. package/constants.d.ts +4 -0
  30. package/constants.js +8 -0
  31. package/index.d.ts +2 -1
  32. package/index.js +3 -2
  33. package/lib/262.d.ts +5 -4
  34. package/lib/262.js +13 -17
  35. package/lib/CanonicalizeLocaleList.d.ts +1 -1
  36. package/lib/IsSanctionedSimpleUnitIdentifier.d.ts +1 -1
  37. package/lib/NumberFormat/ApplyUnsignedRoundingMode.d.ts +2 -1
  38. package/lib/NumberFormat/ApplyUnsignedRoundingMode.js +11 -16
  39. package/lib/NumberFormat/ComputeExponent.d.ts +2 -1
  40. package/lib/NumberFormat/ComputeExponent.js +13 -13
  41. package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts +2 -1
  42. package/lib/NumberFormat/ComputeExponentForMagnitude.js +11 -3
  43. package/lib/NumberFormat/FormatNumericRange.d.ts +2 -1
  44. package/lib/NumberFormat/FormatNumericRangeToParts.d.ts +2 -1
  45. package/lib/NumberFormat/FormatNumericToParts.d.ts +2 -1
  46. package/lib/NumberFormat/FormatNumericToParts.js +1 -1
  47. package/lib/NumberFormat/FormatNumericToString.d.ts +3 -2
  48. package/lib/NumberFormat/FormatNumericToString.js +57 -15
  49. package/lib/NumberFormat/InitializeNumberFormat.js +7 -36
  50. package/lib/NumberFormat/PartitionNumberPattern.d.ts +4 -3
  51. package/lib/NumberFormat/PartitionNumberPattern.js +25 -18
  52. package/lib/NumberFormat/PartitionNumberRangePattern.d.ts +2 -1
  53. package/lib/NumberFormat/PartitionNumberRangePattern.js +2 -3
  54. package/lib/NumberFormat/SetNumberFormatDigitOptions.js +53 -24
  55. package/lib/NumberFormat/ToRawFixed.d.ts +3 -2
  56. package/lib/NumberFormat/ToRawFixed.js +54 -18
  57. package/lib/NumberFormat/ToRawPrecision.d.ts +3 -2
  58. package/lib/NumberFormat/ToRawPrecision.js +75 -43
  59. package/lib/NumberFormat/format_to_parts.d.ts +5 -2
  60. package/lib/NumberFormat/format_to_parts.js +19 -15
  61. package/lib/constants.d.ts +4 -0
  62. package/lib/constants.js +4 -0
  63. package/lib/index.d.ts +2 -1
  64. package/lib/index.js +2 -1
  65. package/lib/types/number.d.ts +16 -9
  66. package/lib/utils.d.ts +0 -5
  67. package/lib/utils.js +0 -9
  68. package/package.json +4 -3
  69. package/types/number.d.ts +16 -9
  70. package/utils.d.ts +0 -5
  71. package/utils.js +0 -10
  72. package/262.js.map +0 -1
  73. package/CanonicalizeLocaleList.js.map +0 -1
  74. package/CanonicalizeTimeZoneName.js.map +0 -1
  75. package/CoerceOptionsToObject.js.map +0 -1
  76. package/DefaultNumberOption.js.map +0 -1
  77. package/GetNumberOption.js.map +0 -1
  78. package/GetOption.js.map +0 -1
  79. package/GetOptionsObject.js.map +0 -1
  80. package/GetStringOrBooleanOption.js.map +0 -1
  81. package/IsSanctionedSimpleUnitIdentifier.js.map +0 -1
  82. package/IsValidTimeZoneName.js.map +0 -1
  83. package/IsWellFormedCurrencyCode.js.map +0 -1
  84. package/IsWellFormedUnitIdentifier.js.map +0 -1
  85. package/NumberFormat/ApplyUnsignedRoundingMode.js.map +0 -1
  86. package/NumberFormat/CollapseNumberRange.js.map +0 -1
  87. package/NumberFormat/ComputeExponent.js.map +0 -1
  88. package/NumberFormat/ComputeExponentForMagnitude.js.map +0 -1
  89. package/NumberFormat/CurrencyDigits.js.map +0 -1
  90. package/NumberFormat/FormatApproximately.js.map +0 -1
  91. package/NumberFormat/FormatNumericRange.js.map +0 -1
  92. package/NumberFormat/FormatNumericRangeToParts.js.map +0 -1
  93. package/NumberFormat/FormatNumericToParts.js.map +0 -1
  94. package/NumberFormat/FormatNumericToString.js.map +0 -1
  95. package/NumberFormat/GetUnsignedRoundingMode.js.map +0 -1
  96. package/NumberFormat/InitializeNumberFormat.js.map +0 -1
  97. package/NumberFormat/PartitionNumberPattern.js.map +0 -1
  98. package/NumberFormat/PartitionNumberRangePattern.js.map +0 -1
  99. package/NumberFormat/SetNumberFormatDigitOptions.js.map +0 -1
  100. package/NumberFormat/SetNumberFormatUnitOptions.js.map +0 -1
  101. package/NumberFormat/ToRawFixed.js.map +0 -1
  102. package/NumberFormat/ToRawPrecision.js.map +0 -1
  103. package/NumberFormat/digit-mapping.generated.js.map +0 -1
  104. package/NumberFormat/format_to_parts.js.map +0 -1
  105. package/PartitionPattern.js.map +0 -1
  106. package/SupportedLocales.js.map +0 -1
  107. package/data.js.map +0 -1
  108. package/index.js.map +0 -1
  109. package/lib/262.js.map +0 -1
  110. package/lib/CanonicalizeLocaleList.js.map +0 -1
  111. package/lib/CanonicalizeTimeZoneName.js.map +0 -1
  112. package/lib/CoerceOptionsToObject.js.map +0 -1
  113. package/lib/DefaultNumberOption.js.map +0 -1
  114. package/lib/GetNumberOption.js.map +0 -1
  115. package/lib/GetOption.js.map +0 -1
  116. package/lib/GetOptionsObject.js.map +0 -1
  117. package/lib/GetStringOrBooleanOption.js.map +0 -1
  118. package/lib/IsSanctionedSimpleUnitIdentifier.js.map +0 -1
  119. package/lib/IsValidTimeZoneName.js.map +0 -1
  120. package/lib/IsWellFormedCurrencyCode.js.map +0 -1
  121. package/lib/IsWellFormedUnitIdentifier.js.map +0 -1
  122. package/lib/NumberFormat/ApplyUnsignedRoundingMode.js.map +0 -1
  123. package/lib/NumberFormat/CollapseNumberRange.js.map +0 -1
  124. package/lib/NumberFormat/ComputeExponent.js.map +0 -1
  125. package/lib/NumberFormat/ComputeExponentForMagnitude.js.map +0 -1
  126. package/lib/NumberFormat/CurrencyDigits.js.map +0 -1
  127. package/lib/NumberFormat/FormatApproximately.js.map +0 -1
  128. package/lib/NumberFormat/FormatNumericRange.js.map +0 -1
  129. package/lib/NumberFormat/FormatNumericRangeToParts.js.map +0 -1
  130. package/lib/NumberFormat/FormatNumericToParts.js.map +0 -1
  131. package/lib/NumberFormat/FormatNumericToString.js.map +0 -1
  132. package/lib/NumberFormat/GetUnsignedRoundingMode.js.map +0 -1
  133. package/lib/NumberFormat/InitializeNumberFormat.js.map +0 -1
  134. package/lib/NumberFormat/PartitionNumberPattern.js.map +0 -1
  135. package/lib/NumberFormat/PartitionNumberRangePattern.js.map +0 -1
  136. package/lib/NumberFormat/SetNumberFormatDigitOptions.js.map +0 -1
  137. package/lib/NumberFormat/SetNumberFormatUnitOptions.js.map +0 -1
  138. package/lib/NumberFormat/ToRawFixed.js.map +0 -1
  139. package/lib/NumberFormat/ToRawPrecision.js.map +0 -1
  140. package/lib/NumberFormat/digit-mapping.generated.js.map +0 -1
  141. package/lib/NumberFormat/format_to_parts.js.map +0 -1
  142. package/lib/PartitionPattern.js.map +0 -1
  143. package/lib/SupportedLocales.js.map +0 -1
  144. package/lib/data.js.map +0 -1
  145. package/lib/index.js.map +0 -1
  146. package/lib/regex.generated.js.map +0 -1
  147. package/lib/types/core.js.map +0 -1
  148. package/lib/types/date-time.js.map +0 -1
  149. package/lib/types/displaynames.js.map +0 -1
  150. package/lib/types/list.js.map +0 -1
  151. package/lib/types/number.js.map +0 -1
  152. package/lib/types/plural-rules.js.map +0 -1
  153. package/lib/types/relative-time.js.map +0 -1
  154. package/lib/utils.js.map +0 -1
  155. package/regex.generated.js.map +0 -1
  156. package/types/core.js.map +0 -1
  157. package/types/date-time.js.map +0 -1
  158. package/types/displaynames.js.map +0 -1
  159. package/types/list.js.map +0 -1
  160. package/types/number.js.map +0 -1
  161. package/types/plural-rules.js.map +0 -1
  162. package/types/relative-time.js.map +0 -1
  163. package/utils.js.map +0 -1
@@ -1,9 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = formatToParts;
4
+ var tslib_1 = require("tslib");
5
+ var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
+ var constants_1 = require("../constants");
4
7
  var regex_generated_1 = require("../regex.generated");
5
- var ToRawFixed_1 = require("./ToRawFixed");
6
8
  var digit_mapping_generated_1 = require("./digit-mapping.generated");
9
+ var GetUnsignedRoundingMode_1 = require("./GetUnsignedRoundingMode");
10
+ var ToRawFixed_1 = require("./ToRawFixed");
7
11
  // This is from: unicode-12.1.0/General_Category/Symbol/regex.js
8
12
  // IE11 does not support unicode flag, otherwise this is just /\p{S}/u.
9
13
  // /^\p{S}/u
@@ -118,9 +122,9 @@ function formatToParts(numberResult, data, pl, options) {
118
122
  switch (part) {
119
123
  case '{0}': {
120
124
  // We only need to handle scientific and engineering notation here.
121
- numberParts.push.apply(numberParts, paritionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem,
125
+ numberParts.push.apply(numberParts, partitionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem,
122
126
  // If compact number pattern exists, do not insert group separators.
123
- !compactNumberPattern && Boolean(options.useGrouping), decimalNumberPattern, style));
127
+ !compactNumberPattern && Boolean(options.useGrouping), decimalNumberPattern, style, options.roundingIncrement, (0, GetUnsignedRoundingMode_1.GetUnsignedRoundingMode)(options.roundingMode, sign === -1)));
124
128
  break;
125
129
  }
126
130
  case '-':
@@ -163,7 +167,7 @@ function formatToParts(numberResult, data, pl, options) {
163
167
  var unitName = void 0;
164
168
  var currencyNameData = data.currencies[options.currency];
165
169
  if (currencyNameData) {
166
- unitName = selectPlural(pl, numberResult.roundedNumber * Math.pow(10, exponent), currencyNameData.displayName);
170
+ unitName = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), currencyNameData.displayName);
167
171
  }
168
172
  else {
169
173
  // Fallback for unknown currency
@@ -200,7 +204,7 @@ function formatToParts(numberResult, data, pl, options) {
200
204
  var unitPattern = void 0;
201
205
  if (unitData) {
202
206
  // Simple unit pattern
203
- unitPattern = selectPlural(pl, numberResult.roundedNumber * Math.pow(10, exponent), data.units.simple[unit][unitDisplay]);
207
+ unitPattern = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), data.units.simple[unit][unitDisplay]);
204
208
  }
205
209
  else {
206
210
  // See: http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
@@ -208,7 +212,7 @@ function formatToParts(numberResult, data, pl, options) {
208
212
  // Implementation note: we are not following TR-35 here because we need to format to parts!
209
213
  var _b = unit.split('-per-'), numeratorUnit = _b[0], denominatorUnit = _b[1];
210
214
  unitData = data.units.simple[numeratorUnit];
211
- var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber * Math.pow(10, exponent), data.units.simple[numeratorUnit][unitDisplay]);
215
+ var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
212
216
  var perUnitPattern = data.units.simple[denominatorUnit].perUnit[unitDisplay];
213
217
  if (perUnitPattern) {
214
218
  // perUnitPattern exists, combine it with numeratorUnitPattern
@@ -254,7 +258,7 @@ function formatToParts(numberResult, data, pl, options) {
254
258
  }
255
259
  // A subset of https://tc39.es/ecma402/#sec-partitionnotationsubpattern
256
260
  // Plus the exponent parts handling.
257
- function paritionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem, useGrouping,
261
+ function partitionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem, useGrouping,
258
262
  /**
259
263
  * This is the decimal number pattern without signs or symbols.
260
264
  * It is used to infer the group size when `useGrouping` is true.
@@ -262,14 +266,14 @@ function paritionNumberIntoParts(symbols, numberResult, notation, exponent, numb
262
266
  * A typical value looks like "#,##0.00" (primary group size is 3).
263
267
  * Some locales like Hindi has secondary group size of 2 (e.g. "#,##,##0.00").
264
268
  */
265
- decimalNumberPattern, style) {
269
+ decimalNumberPattern, style, roundingIncrement, unsignedRoundingMode) {
266
270
  var result = [];
267
271
  // eslint-disable-next-line prefer-const
268
272
  var n = numberResult.formattedString, x = numberResult.roundedNumber;
269
- if (isNaN(x)) {
273
+ if (x.isNaN()) {
270
274
  return [{ type: 'nan', value: n }];
271
275
  }
272
- else if (!isFinite(x)) {
276
+ else if (!x.isFinite()) {
273
277
  return [{ type: 'infinity', value: n }];
274
278
  }
275
279
  var digitReplacementTable = digit_mapping_generated_1.digitMapping[numberingSystem];
@@ -294,7 +298,8 @@ decimalNumberPattern, style) {
294
298
  // unless the rounded number is greater than 10000:
295
299
  // NumberFormat('de', {notation: 'compact', compactDisplay: 'short'}).format(1234) //=> "1234"
296
300
  // NumberFormat('de').format(1234) //=> "1.234"
297
- if (useGrouping && (notation !== 'compact' || x >= 10000)) {
301
+ if (useGrouping &&
302
+ (notation !== 'compact' || x.greaterThanOrEqualTo(10000))) {
298
303
  // a. Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator.
299
304
  // For currency we should use `currencyGroup` instead of generic `group`
300
305
  var groupSepSymbol = style === 'currency' && symbols.currencyGroup != null
@@ -349,13 +354,13 @@ decimalNumberPattern, style) {
349
354
  result.push({ type: 'decimal', value: decimalSepSymbol }, { type: 'fraction', value: fraction });
350
355
  }
351
356
  if ((notation === 'scientific' || notation === 'engineering') &&
352
- isFinite(x)) {
357
+ x.isFinite()) {
353
358
  result.push({ type: 'exponentSeparator', value: symbols.exponential });
354
359
  if (exponent < 0) {
355
360
  result.push({ type: 'exponentMinusSign', value: symbols.minusSign });
356
361
  exponent = -exponent;
357
362
  }
358
- var exponentResult = (0, ToRawFixed_1.ToRawFixed)(exponent, 0, 0);
363
+ var exponentResult = (0, ToRawFixed_1.ToRawFixed)(new decimal_js_1.default(exponent), 0, 0, roundingIncrement, unsignedRoundingMode);
359
364
  result.push({
360
365
  type: 'exponentInteger',
361
366
  value: exponentResult.formattedString,
@@ -401,7 +406,7 @@ function getCompactDisplayPattern(numberResult, pl, data, style, compactDisplay,
401
406
  if (!compactPluralRules) {
402
407
  return null;
403
408
  }
404
- pattern = selectPlural(pl, roundedNumber, compactPluralRules);
409
+ pattern = selectPlural(pl, roundedNumber.toNumber(), compactPluralRules);
405
410
  }
406
411
  else {
407
412
  var byNumberingSystem = data.numbers.decimal;
@@ -411,7 +416,7 @@ function getCompactDisplayPattern(numberResult, pl, data, style, compactDisplay,
411
416
  if (!compactPlaralRule) {
412
417
  return null;
413
418
  }
414
- pattern = selectPlural(pl, roundedNumber, compactPlaralRule);
419
+ pattern = selectPlural(pl, roundedNumber.toNumber(), compactPlaralRule);
415
420
  }
416
421
  // See https://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats
417
422
  // > If the value is precisely “0”, either explicit or defaulted, then the normal number format
package/constants.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import Decimal from 'decimal.js';
2
+ export declare const TEN: Decimal;
3
+ export declare const ZERO: Decimal;
4
+ export declare const NEGATIVE_ZERO: Decimal;
package/constants.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NEGATIVE_ZERO = exports.ZERO = exports.TEN = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
6
+ exports.TEN = new decimal_js_1.default(10);
7
+ exports.ZERO = new decimal_js_1.default(0);
8
+ exports.NEGATIVE_ZERO = new decimal_js_1.default(-0);
package/index.d.ts CHANGED
@@ -30,7 +30,7 @@ export * from './NumberFormat/ToRawFixed';
30
30
  export * from './NumberFormat/ToRawPrecision';
31
31
  export * from './PartitionPattern';
32
32
  export * from './SupportedLocales';
33
- export { createDataProperty, defineProperty, getInternalSlot, getMagnitude, getMultiInternalSlots, isLiteralPart, setInternalSlot, setMultiInternalSlots, } from './utils';
33
+ export { createDataProperty, defineProperty, getInternalSlot, getMultiInternalSlots, isLiteralPart, setInternalSlot, setMultiInternalSlots, } from './utils';
34
34
  export type { LiteralPart } from './utils';
35
35
  export * from './262';
36
36
  export { isMissingLocaleDataError } from './data';
@@ -42,3 +42,4 @@ export * from './types/number';
42
42
  export * from './types/plural-rules';
43
43
  export * from './types/relative-time';
44
44
  export { createMemoizedDateTimeFormat, createMemoizedListFormat, createMemoizedLocale, createMemoizedNumberFormat, createMemoizedPluralRules, invariant, } from './utils';
45
+ export { ZERO } from './constants';
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.invariant = exports.createMemoizedPluralRules = exports.createMemoizedNumberFormat = exports.createMemoizedLocale = exports.createMemoizedListFormat = exports.createMemoizedDateTimeFormat = exports.isMissingLocaleDataError = exports.setMultiInternalSlots = exports.setInternalSlot = exports.isLiteralPart = exports.getMultiInternalSlots = exports.getMagnitude = exports.getInternalSlot = exports.defineProperty = exports.createDataProperty = exports._formatToParts = void 0;
3
+ exports.ZERO = exports.invariant = exports.createMemoizedPluralRules = exports.createMemoizedNumberFormat = exports.createMemoizedLocale = exports.createMemoizedListFormat = exports.createMemoizedDateTimeFormat = exports.isMissingLocaleDataError = exports.setMultiInternalSlots = exports.setInternalSlot = exports.isLiteralPart = exports.getMultiInternalSlots = exports.getInternalSlot = exports.defineProperty = exports.createDataProperty = exports._formatToParts = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  tslib_1.__exportStar(require("./CanonicalizeLocaleList"), exports);
6
6
  tslib_1.__exportStar(require("./CanonicalizeTimeZoneName"), exports);
@@ -39,7 +39,6 @@ var utils_1 = require("./utils");
39
39
  Object.defineProperty(exports, "createDataProperty", { enumerable: true, get: function () { return utils_1.createDataProperty; } });
40
40
  Object.defineProperty(exports, "defineProperty", { enumerable: true, get: function () { return utils_1.defineProperty; } });
41
41
  Object.defineProperty(exports, "getInternalSlot", { enumerable: true, get: function () { return utils_1.getInternalSlot; } });
42
- Object.defineProperty(exports, "getMagnitude", { enumerable: true, get: function () { return utils_1.getMagnitude; } });
43
42
  Object.defineProperty(exports, "getMultiInternalSlots", { enumerable: true, get: function () { return utils_1.getMultiInternalSlots; } });
44
43
  Object.defineProperty(exports, "isLiteralPart", { enumerable: true, get: function () { return utils_1.isLiteralPart; } });
45
44
  Object.defineProperty(exports, "setInternalSlot", { enumerable: true, get: function () { return utils_1.setInternalSlot; } });
@@ -60,3 +59,5 @@ Object.defineProperty(exports, "createMemoizedLocale", { enumerable: true, get:
60
59
  Object.defineProperty(exports, "createMemoizedNumberFormat", { enumerable: true, get: function () { return utils_2.createMemoizedNumberFormat; } });
61
60
  Object.defineProperty(exports, "createMemoizedPluralRules", { enumerable: true, get: function () { return utils_2.createMemoizedPluralRules; } });
62
61
  Object.defineProperty(exports, "invariant", { enumerable: true, get: function () { return utils_2.invariant; } });
62
+ var constants_1 = require("./constants");
63
+ Object.defineProperty(exports, "ZERO", { enumerable: true, get: function () { return constants_1.ZERO; } });
package/lib/262.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Decimal } from 'decimal.js';
1
2
  /**
2
3
  * https://tc39.es/ecma262/#sec-tostring
3
4
  */
@@ -6,7 +7,7 @@ export declare function ToString(o: unknown): string;
6
7
  * https://tc39.es/ecma262/#sec-tonumber
7
8
  * @param val
8
9
  */
9
- export declare function ToNumber(val: any): number;
10
+ export declare function ToNumber(val: any): Decimal;
10
11
  /**
11
12
  * https://tc39.es/ecma262/#sec-timeclip
12
13
  * @param time
@@ -27,7 +28,7 @@ export declare function SameValue(x: any, y: any): boolean;
27
28
  * https://www.ecma-international.org/ecma-262/11.0/index.html#sec-arraycreate
28
29
  * @param len
29
30
  */
30
- export declare function ArrayCreate(len: number): any[];
31
+ export declare function ArrayCreate<T = any>(len: number): T[];
31
32
  /**
32
33
  * https://www.ecma-international.org/ecma-262/11.0/index.html#sec-hasownproperty
33
34
  * @param o
@@ -38,7 +39,7 @@ export declare function HasOwnProperty(o: object, prop: string): boolean;
38
39
  * https://www.ecma-international.org/ecma-262/11.0/index.html#sec-type
39
40
  * @param x
40
41
  */
41
- export declare function Type(x: any): "Null" | "Undefined" | "Object" | "Number" | "Boolean" | "String" | "Symbol" | "BigInt" | undefined;
42
+ export declare function Type(x: any): 'Null' | 'Undefined' | 'Object' | 'Number' | 'Boolean' | 'String' | 'Symbol' | 'BigInt' | undefined;
42
43
  /**
43
44
  * https://tc39.es/ecma262/#eqn-Day
44
45
  * @param t
@@ -71,7 +72,7 @@ export declare function InLeapYear(t: number): 0 | 1;
71
72
  * https://tc39.es/ecma262/#sec-month-number
72
73
  * @param t
73
74
  */
74
- export declare function MonthFromTime(t: number): 0 | 1 | 2 | 3 | 4 | 8 | 5 | 6 | 10 | 7 | 11 | 9;
75
+ export declare function MonthFromTime(t: number): 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
75
76
  export declare function DateFromTime(t: number): number;
76
77
  export declare function HourFromTime(t: number): number;
77
78
  export declare function MinFromTime(t: number): number;
package/lib/262.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { Decimal } from 'decimal.js';
2
+ import { ZERO } from './constants';
1
3
  /**
2
4
  * https://tc39.es/ecma262/#sec-tostring
3
5
  */
@@ -14,21 +16,18 @@ export function ToString(o) {
14
16
  */
15
17
  export function ToNumber(val) {
16
18
  if (val === undefined) {
17
- return NaN;
19
+ return new Decimal(NaN);
18
20
  }
19
21
  if (val === null) {
20
- return +0;
22
+ return ZERO;
21
23
  }
22
24
  if (typeof val === 'boolean') {
23
- return val ? 1 : +0;
24
- }
25
- if (typeof val === 'number') {
26
- return val;
25
+ return new Decimal(val ? 1 : 0);
27
26
  }
28
27
  if (typeof val === 'symbol' || typeof val === 'bigint') {
29
28
  throw new TypeError('Cannot convert symbol/bigint to number');
30
29
  }
31
- return Number(val);
30
+ return new Decimal(Number(val));
32
31
  }
33
32
  /**
34
33
  * https://tc39.es/ecma262/#sec-tointeger
@@ -36,18 +35,15 @@ export function ToNumber(val) {
36
35
  */
37
36
  function ToInteger(n) {
38
37
  var number = ToNumber(n);
39
- if (isNaN(number) || SameValue(number, -0)) {
40
- return 0;
38
+ if (number.isNaN() || number.isZero()) {
39
+ return ZERO;
41
40
  }
42
- if (isFinite(number)) {
41
+ if (number.isFinite()) {
43
42
  return number;
44
43
  }
45
- var integer = Math.floor(Math.abs(number));
46
- if (number < 0) {
47
- integer = -integer;
48
- }
49
- if (SameValue(integer, -0)) {
50
- return 0;
44
+ var integer = number.abs().floor();
45
+ if (number.isNegative()) {
46
+ integer = integer.negated();
51
47
  }
52
48
  return integer;
53
49
  }
@@ -62,7 +58,7 @@ export function TimeClip(time) {
62
58
  if (Math.abs(time) > 8.64 * 1e15) {
63
59
  return NaN;
64
60
  }
65
- return ToInteger(time);
61
+ return ToInteger(time).toNumber();
66
62
  }
67
63
  /**
68
64
  * https://tc39.es/ecma262/#sec-toobject
@@ -2,4 +2,4 @@
2
2
  * http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist
3
3
  * @param locales
4
4
  */
5
- export declare function CanonicalizeLocaleList(locales?: string | string[]): string[];
5
+ export declare function CanonicalizeLocaleList(locales?: string | ReadonlyArray<string>): string[];
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * https://tc39.es/ecma402/#table-sanctioned-simple-unit-identifiers
3
3
  */
4
- export declare const SANCTIONED_UNITS: string[];
4
+ export declare const SANCTIONED_UNITS: readonly ["angle-degree", "area-acre", "area-hectare", "concentr-percent", "digital-bit", "digital-byte", "digital-gigabit", "digital-gigabyte", "digital-kilobit", "digital-kilobyte", "digital-megabit", "digital-megabyte", "digital-petabyte", "digital-terabit", "digital-terabyte", "duration-day", "duration-hour", "duration-millisecond", "duration-minute", "duration-month", "duration-second", "duration-week", "duration-year", "length-centimeter", "length-foot", "length-inch", "length-kilometer", "length-meter", "length-mile-scandinavian", "length-mile", "length-millimeter", "length-yard", "mass-gram", "mass-kilogram", "mass-ounce", "mass-pound", "mass-stone", "temperature-celsius", "temperature-fahrenheit", "volume-fluid-ounce", "volume-gallon", "volume-liter", "volume-milliliter"];
5
5
  export declare function removeUnitNamespace(unit: string): string;
6
6
  /**
7
7
  * https://tc39.es/ecma402/#table-sanctioned-simple-unit-identifiers
@@ -1,2 +1,3 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { UnsignedRoundingModeType } from '../types/number';
2
- export declare function ApplyUnsignedRoundingMode(x: number, r1: number, r2: number, unsignedRoundingMode: UnsignedRoundingModeType): number;
3
+ export declare function ApplyUnsignedRoundingMode(x: Decimal, r1: Decimal, r2: Decimal, unsignedRoundingMode: UnsignedRoundingModeType): Decimal;
@@ -1,37 +1,32 @@
1
+ import { invariant } from '../utils';
1
2
  export function ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode) {
2
- if (x === r1)
3
+ if (x.eq(r1))
3
4
  return r1;
4
- if (unsignedRoundingMode === undefined) {
5
- throw new Error('unsignedRoundingMode is mandatory');
6
- }
5
+ invariant(r1.lessThan(x) && x.lessThan(r2), "x should be between r1 and r2 but x=".concat(x, ", r1=").concat(r1, ", r2=").concat(r2));
7
6
  if (unsignedRoundingMode === 'zero') {
8
7
  return r1;
9
8
  }
10
9
  if (unsignedRoundingMode === 'infinity') {
11
10
  return r2;
12
11
  }
13
- var d1 = x - r1;
14
- var d2 = r2 - x;
15
- if (d1 < d2) {
12
+ var d1 = x.minus(r1);
13
+ var d2 = r2.minus(x);
14
+ if (d1.lessThan(d2)) {
16
15
  return r1;
17
16
  }
18
- if (d2 < d1) {
17
+ if (d2.lessThan(d1)) {
19
18
  return r2;
20
19
  }
21
- if (d1 !== d2) {
22
- throw new Error('Unexpected error');
23
- }
20
+ invariant(d1.eq(d2), 'd1 should be equal to d2');
24
21
  if (unsignedRoundingMode === 'half-zero') {
25
22
  return r1;
26
23
  }
27
24
  if (unsignedRoundingMode === 'half-infinity') {
28
25
  return r2;
29
26
  }
30
- if (unsignedRoundingMode !== 'half-even') {
31
- throw new Error("Unexpected value for unsignedRoundingMode: ".concat(unsignedRoundingMode));
32
- }
33
- var cardinality = (r1 / (r2 - r1)) % 2;
34
- if (cardinality === 0) {
27
+ invariant(unsignedRoundingMode === 'half-even', 'unsignedRoundingMode should be half-even');
28
+ var cardinality = r1.div(r2.minus(r1)).mod(2);
29
+ if (cardinality.isZero()) {
35
30
  return r1;
36
31
  }
37
32
  return r2;
@@ -1,3 +1,4 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { NumberFormatInternal } from '../types/number';
2
3
  /**
3
4
  * The abstract operation ComputeExponent computes an exponent (power of ten) by which to scale x
@@ -6,6 +7,6 @@ import { NumberFormatInternal } from '../types/number';
6
7
  *
7
8
  * NOT IN SPEC: it returns [exponent, magnitude].
8
9
  */
9
- export declare function ComputeExponent(numberFormat: Intl.NumberFormat, x: number, { getInternalSlots, }: {
10
+ export declare function ComputeExponent(numberFormat: Intl.NumberFormat, x: Decimal, { getInternalSlots, }: {
10
11
  getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
11
12
  }): [number, number];
@@ -1,4 +1,4 @@
1
- import { getMagnitude } from '../utils';
1
+ import { TEN } from '../constants';
2
2
  import { ComputeExponentForMagnitude } from './ComputeExponentForMagnitude';
3
3
  import { FormatNumericToString } from './FormatNumericToString';
4
4
  /**
@@ -10,30 +10,30 @@ import { FormatNumericToString } from './FormatNumericToString';
10
10
  */
11
11
  export function ComputeExponent(numberFormat, x, _a) {
12
12
  var getInternalSlots = _a.getInternalSlots;
13
- if (x === 0) {
13
+ if (x.isZero()) {
14
14
  return [0, 0];
15
15
  }
16
- if (x < 0) {
17
- x = -x;
16
+ if (x.isNegative()) {
17
+ x = x.negated();
18
18
  }
19
- var magnitude = getMagnitude(x);
19
+ var magnitude = x.log(10).floor();
20
20
  var exponent = ComputeExponentForMagnitude(numberFormat, magnitude, {
21
21
  getInternalSlots: getInternalSlots,
22
22
  });
23
23
  // Preserve more precision by doing multiplication when exponent is negative.
24
- x = exponent < 0 ? x * Math.pow(10, -exponent) : x / Math.pow(10, exponent);
24
+ x = x.times(TEN.pow(-exponent));
25
25
  var formatNumberResult = FormatNumericToString(getInternalSlots(numberFormat), x);
26
- if (formatNumberResult.roundedNumber === 0) {
27
- return [exponent, magnitude];
26
+ if (formatNumberResult.roundedNumber.isZero()) {
27
+ return [exponent, magnitude.toNumber()];
28
28
  }
29
- var newMagnitude = getMagnitude(formatNumberResult.roundedNumber);
30
- if (newMagnitude === magnitude - exponent) {
31
- return [exponent, magnitude];
29
+ var newMagnitude = formatNumberResult.roundedNumber.log(10).floor();
30
+ if (newMagnitude.eq(magnitude.minus(exponent))) {
31
+ return [exponent, magnitude.toNumber()];
32
32
  }
33
33
  return [
34
- ComputeExponentForMagnitude(numberFormat, magnitude + 1, {
34
+ ComputeExponentForMagnitude(numberFormat, magnitude.plus(1), {
35
35
  getInternalSlots: getInternalSlots,
36
36
  }),
37
- magnitude + 1,
37
+ magnitude.plus(1).toNumber(),
38
38
  ];
39
39
  }
@@ -1,9 +1,10 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { NumberFormatInternal } from '../types/number';
2
3
  /**
3
4
  * The abstract operation ComputeExponentForMagnitude computes an exponent by which to scale a
4
5
  * number of the given magnitude (power of ten of the most significant digit) according to the
5
6
  * locale and the desired notation (scientific, engineering, or compact).
6
7
  */
7
- export declare function ComputeExponentForMagnitude(numberFormat: Intl.NumberFormat, magnitude: number, { getInternalSlots, }: {
8
+ export declare function ComputeExponentForMagnitude(numberFormat: Intl.NumberFormat, magnitude: Decimal, { getInternalSlots, }: {
8
9
  getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
9
10
  }): number;
@@ -1,3 +1,9 @@
1
+ import Decimal from 'decimal.js';
2
+ import { TEN } from '../constants';
3
+ import { invariant } from '../utils';
4
+ Decimal.set({
5
+ toExpPos: 100,
6
+ });
1
7
  /**
2
8
  * The abstract operation ComputeExponentForMagnitude computes an exponent by which to scale a
3
9
  * number of the given magnitude (power of ten of the most significant digit) according to the
@@ -11,10 +17,12 @@ export function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
11
17
  case 'standard':
12
18
  return 0;
13
19
  case 'scientific':
14
- return magnitude;
20
+ return magnitude.toNumber();
15
21
  case 'engineering':
16
- return Math.floor(magnitude / 3) * 3;
22
+ var thousands = magnitude.div(3).floor();
23
+ return thousands.times(3).toNumber();
17
24
  default: {
25
+ invariant(notation === 'compact', 'Invalid notation');
18
26
  // Let exponent be an implementation- and locale-dependent (ILD) integer by which to scale a
19
27
  // number of the given magnitude in compact notation for the current locale.
20
28
  var compactDisplay = internalSlots.compactDisplay, style = internalSlots.style, currencyDisplay = internalSlots.currencyDisplay;
@@ -32,7 +40,7 @@ export function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
32
40
  if (!thresholdMap) {
33
41
  return 0;
34
42
  }
35
- var num = String(Math.pow(10, magnitude));
43
+ var num = TEN.pow(magnitude).toString();
36
44
  var thresholds = Object.keys(thresholdMap); // TODO: this can be pre-processed
37
45
  if (num < thresholds[0]) {
38
46
  return 0;
@@ -1,7 +1,8 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { NumberFormatInternal } from '../types/number';
2
3
  /**
3
4
  * https://tc39.es/ecma402/#sec-formatnumericrange
4
5
  */
5
- export declare function FormatNumericRange(numberFormat: Intl.NumberFormat, x: number, y: number, { getInternalSlots, }: {
6
+ export declare function FormatNumericRange(numberFormat: Intl.NumberFormat, x: Decimal, y: Decimal, { getInternalSlots, }: {
6
7
  getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
7
8
  }): string;
@@ -1,7 +1,8 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { NumberFormatInternal, NumberRangeToParts } from '../types/number';
2
3
  /**
3
4
  * https://tc39.es/ecma402/#sec-formatnumericrangetoparts
4
5
  */
5
- export declare function FormatNumericRangeToParts(numberFormat: Intl.NumberFormat, x: number, y: number, { getInternalSlots, }: {
6
+ export declare function FormatNumericRangeToParts(numberFormat: Intl.NumberFormat, x: Decimal, y: Decimal, { getInternalSlots, }: {
6
7
  getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
7
8
  }): NumberRangeToParts[];
@@ -1,4 +1,5 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { NumberFormatInternal, NumberFormatPart } from '../types/number';
2
- export declare function FormatNumericToParts(nf: Intl.NumberFormat, x: number, implDetails: {
3
+ export declare function FormatNumericToParts(nf: Intl.NumberFormat, x: Decimal, implDetails: {
3
4
  getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
4
5
  }): NumberFormatPart[];
@@ -1,5 +1,5 @@
1
- import { PartitionNumberPattern } from './PartitionNumberPattern';
2
1
  import { ArrayCreate } from '../262';
2
+ import { PartitionNumberPattern } from './PartitionNumberPattern';
3
3
  export function FormatNumericToParts(nf, x, implDetails) {
4
4
  var parts = PartitionNumberPattern(nf, x, implDetails);
5
5
  var result = ArrayCreate(0);
@@ -1,8 +1,9 @@
1
+ import Decimal from 'decimal.js';
1
2
  import { NumberFormatDigitInternalSlots } from '../types/number';
2
3
  /**
3
4
  * https://tc39.es/ecma402/#sec-formatnumberstring
4
5
  */
5
- export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits'>, x: number): {
6
- roundedNumber: number;
6
+ export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits' | 'roundingIncrement' | 'roundingMode' | 'trailingZeroDisplay'>, x: Decimal): {
7
+ roundedNumber: Decimal;
7
8
  formattedString: string;
8
9
  };
@@ -1,41 +1,83 @@
1
- import { SameValue } from '../262';
2
- import { ToRawPrecision } from './ToRawPrecision';
3
- import { repeat } from '../utils';
1
+ import { NEGATIVE_ZERO, ZERO } from '../constants';
2
+ import { invariant, repeat } from '../utils';
3
+ import { GetUnsignedRoundingMode } from './GetUnsignedRoundingMode';
4
4
  import { ToRawFixed } from './ToRawFixed';
5
+ import { ToRawPrecision } from './ToRawPrecision';
5
6
  /**
6
7
  * https://tc39.es/ecma402/#sec-formatnumberstring
7
8
  */
8
9
  export function FormatNumericToString(intlObject, x) {
9
- var isNegative = x < 0 || SameValue(x, -0);
10
- if (isNegative) {
11
- x = -x;
10
+ var sign;
11
+ // -0
12
+ if (x.isZero() && x.isNegative()) {
13
+ sign = 'negative';
14
+ x = ZERO;
15
+ }
16
+ else {
17
+ invariant(x.isFinite(), 'NumberFormatDigitInternalSlots value is not finite');
18
+ if (x.lessThan(0)) {
19
+ sign = 'negative';
20
+ }
21
+ else {
22
+ sign = 'positive';
23
+ }
24
+ if (sign === 'negative') {
25
+ x = x.negated();
26
+ }
12
27
  }
13
28
  var result;
14
- var rourndingType = intlObject.roundingType;
15
- switch (rourndingType) {
29
+ var roundingType = intlObject.roundingType;
30
+ var unsignedRoundingMode = GetUnsignedRoundingMode(intlObject.roundingMode, sign === 'negative');
31
+ switch (roundingType) {
16
32
  case 'significantDigits':
17
- result = ToRawPrecision(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits);
33
+ result = ToRawPrecision(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits, unsignedRoundingMode);
18
34
  break;
19
35
  case 'fractionDigits':
20
- result = ToRawFixed(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits);
36
+ result = ToRawFixed(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits, intlObject.roundingIncrement, unsignedRoundingMode);
21
37
  break;
22
38
  default:
23
- result = ToRawPrecision(x, 1, 2);
24
- if (result.integerDigitsCount > 1) {
25
- result = ToRawFixed(x, 0, 0);
39
+ var sResult = ToRawPrecision(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits, unsignedRoundingMode);
40
+ var fResult = ToRawFixed(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits, intlObject.roundingIncrement, unsignedRoundingMode);
41
+ if (intlObject.roundingType === 'morePrecision') {
42
+ if (sResult.roundingMagnitude <= fResult.roundingMagnitude) {
43
+ result = sResult;
44
+ }
45
+ else {
46
+ result = fResult;
47
+ }
48
+ }
49
+ else {
50
+ invariant(intlObject.roundingType === 'lessPrecision', 'Invalid roundingType');
51
+ if (sResult.roundingMagnitude <= fResult.roundingMagnitude) {
52
+ result = fResult;
53
+ }
54
+ else {
55
+ result = sResult;
56
+ }
26
57
  }
27
58
  break;
28
59
  }
29
60
  x = result.roundedNumber;
30
61
  var string = result.formattedString;
62
+ if (intlObject.trailingZeroDisplay === 'stripIfInteger' && x.isInteger()) {
63
+ var i = string.indexOf('.');
64
+ if (i > -1) {
65
+ string = string.slice(0, i);
66
+ }
67
+ }
31
68
  var int = result.integerDigitsCount;
32
69
  var minInteger = intlObject.minimumIntegerDigits;
33
70
  if (int < minInteger) {
34
71
  var forwardZeros = repeat('0', minInteger - int);
35
72
  string = forwardZeros + string;
36
73
  }
37
- if (isNegative) {
38
- x = -x;
74
+ if (sign === 'negative') {
75
+ if (x.isZero()) {
76
+ x = NEGATIVE_ZERO;
77
+ }
78
+ else {
79
+ x = x.negated();
80
+ }
39
81
  }
40
82
  return { roundedNumber: x, formattedString: string };
41
83
  }