@formatjs/ecma402-abstract 2.3.0 → 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 +27 -12
  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 +27 -12
  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
@@ -1,5 +1,4 @@
1
1
  import Decimal from 'decimal.js';
2
- import { TEN } from '../constants';
3
2
  import { S_UNICODE_REGEX } from '../regex.generated';
4
3
  import { digitMapping } from './digit-mapping.generated';
5
4
  import { GetUnsignedRoundingMode } from './GetUnsignedRoundingMode';
@@ -12,6 +11,7 @@ var CARET_S_UNICODE_REGEX = new RegExp("^".concat(S_UNICODE_REGEX.source));
12
11
  var S_DOLLAR_UNICODE_REGEX = new RegExp("".concat(S_UNICODE_REGEX.source, "$"));
13
12
  var CLDR_NUMBER_PATTERN = /[#0](?:[\.,][#0]+)*/g;
14
13
  export default function formatToParts(numberResult, data, pl, options) {
14
+ var _a;
15
15
  var sign = numberResult.sign, exponent = numberResult.exponent, magnitude = numberResult.magnitude;
16
16
  var notation = options.notation, style = options.style, numberingSystem = options.numberingSystem;
17
17
  var defaultNumberingSystem = data.numbers.nu[0];
@@ -120,7 +120,7 @@ export default function formatToParts(numberResult, data, pl, options) {
120
120
  // We only need to handle scientific and engineering notation here.
121
121
  numberParts.push.apply(numberParts, partitionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem,
122
122
  // If compact number pattern exists, do not insert group separators.
123
- !compactNumberPattern && Boolean(options.useGrouping), decimalNumberPattern, style, options.roundingIncrement, GetUnsignedRoundingMode(options.roundingMode, sign === -1)));
123
+ !compactNumberPattern && ((_a = options.useGrouping) !== null && _a !== void 0 ? _a : true), decimalNumberPattern, style, options.roundingIncrement, GetUnsignedRoundingMode(options.roundingMode, sign === -1)));
124
124
  break;
125
125
  }
126
126
  case '-':
@@ -163,7 +163,9 @@ export default function formatToParts(numberResult, data, pl, options) {
163
163
  var unitName = void 0;
164
164
  var currencyNameData = data.currencies[options.currency];
165
165
  if (currencyNameData) {
166
- unitName = selectPlural(pl, numberResult.roundedNumber.times(TEN.pow(exponent)).toNumber(), currencyNameData.displayName);
166
+ unitName = selectPlural(pl, numberResult.roundedNumber
167
+ .times(Decimal.pow(10, exponent))
168
+ .toNumber(), currencyNameData.displayName);
167
169
  }
168
170
  else {
169
171
  // Fallback for unknown currency
@@ -172,8 +174,8 @@ export default function formatToParts(numberResult, data, pl, options) {
172
174
  // Do {0} and {1} substitution
173
175
  var unitPatternParts = unitPattern.split(/(\{[01]\})/g);
174
176
  var result = [];
175
- for (var _a = 0, unitPatternParts_1 = unitPatternParts; _a < unitPatternParts_1.length; _a++) {
176
- var part = unitPatternParts_1[_a];
177
+ for (var _b = 0, unitPatternParts_1 = unitPatternParts; _b < unitPatternParts_1.length; _b++) {
178
+ var part = unitPatternParts_1[_b];
177
179
  switch (part) {
178
180
  case '{0}':
179
181
  result.push.apply(result, numberParts);
@@ -200,15 +202,19 @@ export default function formatToParts(numberResult, data, pl, options) {
200
202
  var unitPattern = void 0;
201
203
  if (unitData) {
202
204
  // Simple unit pattern
203
- unitPattern = selectPlural(pl, numberResult.roundedNumber.times(TEN.pow(exponent)).toNumber(), data.units.simple[unit][unitDisplay]);
205
+ unitPattern = selectPlural(pl, numberResult.roundedNumber
206
+ .times(Decimal.pow(10, exponent))
207
+ .toNumber(), data.units.simple[unit][unitDisplay]);
204
208
  }
205
209
  else {
206
210
  // See: http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
207
211
  // If cannot find unit in the simple pattern, it must be "per" compound pattern.
208
212
  // Implementation note: we are not following TR-35 here because we need to format to parts!
209
- var _b = unit.split('-per-'), numeratorUnit = _b[0], denominatorUnit = _b[1];
213
+ var _c = unit.split('-per-'), numeratorUnit = _c[0], denominatorUnit = _c[1];
210
214
  unitData = data.units.simple[numeratorUnit];
211
- var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber.times(TEN.pow(exponent)).toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
215
+ var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber
216
+ .times(Decimal.pow(10, exponent))
217
+ .toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
212
218
  var perUnitPattern = data.units.simple[denominatorUnit].perUnit[unitDisplay];
213
219
  if (perUnitPattern) {
214
220
  // perUnitPattern exists, combine it with numeratorUnitPattern
@@ -226,8 +232,8 @@ export default function formatToParts(numberResult, data, pl, options) {
226
232
  }
227
233
  var result = [];
228
234
  // We need spacing around "{0}" because they are not treated as "unit" parts, but "literal".
229
- for (var _c = 0, _d = unitPattern.split(/(\s*\{0\}\s*)/); _c < _d.length; _c++) {
230
- var part = _d[_c];
235
+ for (var _d = 0, _e = unitPattern.split(/(\s*\{0\}\s*)/); _d < _e.length; _d++) {
236
+ var part = _e[_d];
231
237
  var interpolateMatch = /^(\s*)\{0\}(\s*)$/.exec(part);
232
238
  if (interpolateMatch) {
233
239
  // Space before "{0}"
@@ -294,8 +300,17 @@ decimalNumberPattern, style, roundingIncrement, unsignedRoundingMode) {
294
300
  // unless the rounded number is greater than 10000:
295
301
  // NumberFormat('de', {notation: 'compact', compactDisplay: 'short'}).format(1234) //=> "1234"
296
302
  // NumberFormat('de').format(1234) //=> "1.234"
297
- if (useGrouping &&
298
- (notation !== 'compact' || x.greaterThanOrEqualTo(10000))) {
303
+ var shouldUseGrouping = false;
304
+ if (useGrouping === 'always') {
305
+ shouldUseGrouping = true;
306
+ }
307
+ else if (useGrouping === 'min2') {
308
+ shouldUseGrouping = x.greaterThanOrEqualTo(10000);
309
+ }
310
+ else if (useGrouping === 'auto' || useGrouping) {
311
+ shouldUseGrouping = notation !== 'compact' || x.greaterThanOrEqualTo(10000);
312
+ }
313
+ if (shouldUseGrouping) {
299
314
  // a. Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator.
300
315
  // For currency we should use `currencyGroup` instead of generic `group`
301
316
  var groupSepSymbol = style === 'currency' && symbols.currencyGroup != null
@@ -0,0 +1,2 @@
1
+ import Decimal from 'decimal.js';
2
+ export declare function ToIntlMathematicalValue(input: unknown): Decimal;
@@ -0,0 +1,27 @@
1
+ import Decimal from 'decimal.js';
2
+ import { ToPrimitive } from './262';
3
+ export function ToIntlMathematicalValue(input) {
4
+ var primValue = ToPrimitive(input, 'number');
5
+ if (typeof primValue === 'bigint') {
6
+ return new Decimal(primValue);
7
+ }
8
+ // IMPL
9
+ if (primValue === undefined) {
10
+ return new Decimal(NaN);
11
+ }
12
+ if (primValue === true) {
13
+ return new Decimal(1);
14
+ }
15
+ if (primValue === false) {
16
+ return new Decimal(0);
17
+ }
18
+ if (primValue === null) {
19
+ return new Decimal(0);
20
+ }
21
+ try {
22
+ return new Decimal(primValue);
23
+ }
24
+ catch (e) {
25
+ return new Decimal(NaN);
26
+ }
27
+ }
package/lib/index.d.ts CHANGED
@@ -16,6 +16,7 @@ export * from './NumberFormat/ComputeExponentForMagnitude';
16
16
  export * from './NumberFormat/CurrencyDigits';
17
17
  export { default as _formatToParts } from './NumberFormat/format_to_parts';
18
18
  export * from './NumberFormat/FormatApproximately';
19
+ export * from './NumberFormat/FormatNumeric';
19
20
  export * from './NumberFormat/FormatNumericRange';
20
21
  export * from './NumberFormat/FormatNumericRangeToParts';
21
22
  export * from './NumberFormat/FormatNumericToParts';
@@ -43,3 +44,4 @@ export * from './types/plural-rules';
43
44
  export * from './types/relative-time';
44
45
  export { createMemoizedDateTimeFormat, createMemoizedListFormat, createMemoizedLocale, createMemoizedNumberFormat, createMemoizedPluralRules, invariant, } from './utils';
45
46
  export { ZERO } from './constants';
47
+ export { ToIntlMathematicalValue } from './ToIntlMathematicalValue';
package/lib/index.js CHANGED
@@ -16,6 +16,7 @@ export * from './NumberFormat/ComputeExponentForMagnitude';
16
16
  export * from './NumberFormat/CurrencyDigits';
17
17
  export { default as _formatToParts } from './NumberFormat/format_to_parts';
18
18
  export * from './NumberFormat/FormatApproximately';
19
+ export * from './NumberFormat/FormatNumeric';
19
20
  export * from './NumberFormat/FormatNumericRange';
20
21
  export * from './NumberFormat/FormatNumericRangeToParts';
21
22
  export * from './NumberFormat/FormatNumericToParts';
@@ -41,3 +42,4 @@ export * from './types/plural-rules';
41
42
  export * from './types/relative-time';
42
43
  export { createMemoizedDateTimeFormat, createMemoizedListFormat, createMemoizedLocale, createMemoizedNumberFormat, createMemoizedPluralRules, invariant, } from './utils';
43
44
  export { ZERO } from './constants';
45
+ export { ToIntlMathematicalValue } from './ToIntlMathematicalValue';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formatjs/ecma402-abstract",
3
- "version": "2.3.0",
3
+ "version": "2.3.2",
4
4
  "description": "A collection of implementation for ECMAScript abstract operations",
5
5
  "keywords": [
6
6
  "intl",
@@ -16,8 +16,8 @@
16
16
  "dependencies": {
17
17
  "decimal.js": "10",
18
18
  "tslib": "2",
19
- "@formatjs/fast-memoize": "2.2.5",
20
- "@formatjs/intl-localematcher": "0.5.9"
19
+ "@formatjs/intl-localematcher": "0.5.10",
20
+ "@formatjs/fast-memoize": "2.2.6"
21
21
  },
22
22
  "author": "Long Ho <holevietlong@gmail.com",
23
23
  "bugs": {