@formatjs/ecma402-abstract 2.3.5 → 3.0.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 (177) hide show
  1. package/262.d.ts +1 -1
  2. package/262.js +42 -68
  3. package/CanonicalizeLocaleList.js +1 -4
  4. package/CanonicalizeTimeZoneName.js +1 -4
  5. package/CoerceOptionsToObject.js +3 -6
  6. package/DefaultNumberOption.js +1 -4
  7. package/GetNumberOption.js +3 -6
  8. package/GetOption.js +3 -6
  9. package/GetOptionsObject.js +1 -4
  10. package/GetStringOrBooleanOption.js +3 -6
  11. package/IsSanctionedSimpleUnitIdentifier.js +5 -10
  12. package/IsValidTimeZoneName.js +1 -4
  13. package/IsWellFormedCurrencyCode.js +1 -4
  14. package/IsWellFormedUnitIdentifier.js +5 -8
  15. package/NumberFormat/ApplyUnsignedRoundingMode.d.ts +2 -2
  16. package/NumberFormat/ApplyUnsignedRoundingMode.js +5 -8
  17. package/NumberFormat/CollapseNumberRange.d.ts +1 -1
  18. package/NumberFormat/CollapseNumberRange.js +1 -4
  19. package/NumberFormat/ComputeExponent.d.ts +2 -2
  20. package/NumberFormat/ComputeExponent.js +8 -12
  21. package/NumberFormat/ComputeExponentForMagnitude.d.ts +2 -2
  22. package/NumberFormat/ComputeExponentForMagnitude.js +6 -10
  23. package/NumberFormat/CurrencyDigits.js +3 -6
  24. package/NumberFormat/FormatApproximately.d.ts +1 -1
  25. package/NumberFormat/FormatApproximately.js +1 -4
  26. package/NumberFormat/FormatNumeric.d.ts +2 -2
  27. package/NumberFormat/FormatNumeric.js +3 -6
  28. package/NumberFormat/FormatNumericRange.d.ts +2 -2
  29. package/NumberFormat/FormatNumericRange.js +3 -6
  30. package/NumberFormat/FormatNumericRangeToParts.d.ts +2 -2
  31. package/NumberFormat/FormatNumericRangeToParts.js +3 -6
  32. package/NumberFormat/FormatNumericToParts.d.ts +2 -2
  33. package/NumberFormat/FormatNumericToParts.js +5 -8
  34. package/NumberFormat/FormatNumericToString.d.ts +2 -2
  35. package/NumberFormat/FormatNumericToString.js +16 -19
  36. package/NumberFormat/GetUnsignedRoundingMode.d.ts +1 -1
  37. package/NumberFormat/GetUnsignedRoundingMode.js +1 -4
  38. package/NumberFormat/InitializeNumberFormat.d.ts +1 -1
  39. package/NumberFormat/InitializeNumberFormat.js +23 -26
  40. package/NumberFormat/PartitionNumberPattern.d.ts +2 -2
  41. package/NumberFormat/PartitionNumberPattern.js +12 -16
  42. package/NumberFormat/PartitionNumberRangePattern.d.ts +2 -2
  43. package/NumberFormat/PartitionNumberRangePattern.js +12 -15
  44. package/NumberFormat/SetNumberFormatDigitOptions.d.ts +1 -1
  45. package/NumberFormat/SetNumberFormatDigitOptions.js +18 -21
  46. package/NumberFormat/SetNumberFormatUnitOptions.d.ts +1 -1
  47. package/NumberFormat/SetNumberFormatUnitOptions.js +15 -18
  48. package/NumberFormat/ToRawFixed.d.ts +2 -2
  49. package/NumberFormat/ToRawFixed.js +10 -14
  50. package/NumberFormat/ToRawPrecision.d.ts +2 -2
  51. package/NumberFormat/ToRawPrecision.js +19 -23
  52. package/NumberFormat/digit-mapping.generated.js +1 -4
  53. package/NumberFormat/format_to_parts.d.ts +2 -2
  54. package/NumberFormat/format_to_parts.js +18 -22
  55. package/PartitionPattern.d.ts +1 -0
  56. package/PartitionPattern.js +4 -6
  57. package/SupportedLocales.js +8 -11
  58. package/ToIntlMathematicalValue.d.ts +1 -1
  59. package/ToIntlMathematicalValue.js +12 -16
  60. package/constants.d.ts +1 -1
  61. package/constants.js +4 -8
  62. package/data.js +3 -6
  63. package/index.d.ts +47 -47
  64. package/index.js +45 -66
  65. package/package.json +7 -5
  66. package/regex.generated.js +1 -4
  67. package/types/core.js +1 -2
  68. package/types/date-time.js +2 -5
  69. package/types/displaynames.d.ts +1 -1
  70. package/types/displaynames.js +1 -2
  71. package/types/list.d.ts +1 -1
  72. package/types/list.js +1 -2
  73. package/types/number.d.ts +3 -3
  74. package/types/number.js +1 -2
  75. package/types/plural-rules.d.ts +2 -2
  76. package/types/plural-rules.js +1 -2
  77. package/types/relative-time.d.ts +2 -2
  78. package/types/relative-time.js +1 -2
  79. package/utils.js +28 -40
  80. package/lib/262.d.ts +0 -93
  81. package/lib/262.js +0 -400
  82. package/lib/CanonicalizeLocaleList.d.ts +0 -5
  83. package/lib/CanonicalizeLocaleList.js +0 -8
  84. package/lib/CanonicalizeTimeZoneName.d.ts +0 -8
  85. package/lib/CanonicalizeTimeZoneName.js +0 -17
  86. package/lib/CoerceOptionsToObject.d.ts +0 -6
  87. package/lib/CoerceOptionsToObject.js +0 -12
  88. package/lib/DefaultNumberOption.d.ts +0 -8
  89. package/lib/DefaultNumberOption.js +0 -18
  90. package/lib/GetNumberOption.d.ts +0 -9
  91. package/lib/GetNumberOption.js +0 -13
  92. package/lib/GetOption.d.ts +0 -9
  93. package/lib/GetOption.js +0 -31
  94. package/lib/GetOptionsObject.d.ts +0 -6
  95. package/lib/GetOptionsObject.js +0 -14
  96. package/lib/GetStringOrBooleanOption.d.ts +0 -10
  97. package/lib/GetStringOrBooleanOption.js +0 -31
  98. package/lib/IsSanctionedSimpleUnitIdentifier.d.ts +0 -13
  99. package/lib/IsSanctionedSimpleUnitIdentifier.js +0 -63
  100. package/lib/IsValidTimeZoneName.d.ts +0 -9
  101. package/lib/IsValidTimeZoneName.js +0 -17
  102. package/lib/IsWellFormedCurrencyCode.d.ts +0 -4
  103. package/lib/IsWellFormedCurrencyCode.js +0 -21
  104. package/lib/IsWellFormedUnitIdentifier.d.ts +0 -5
  105. package/lib/IsWellFormedUnitIdentifier.js +0 -28
  106. package/lib/NumberFormat/ApplyUnsignedRoundingMode.d.ts +0 -3
  107. package/lib/NumberFormat/ApplyUnsignedRoundingMode.js +0 -33
  108. package/lib/NumberFormat/CollapseNumberRange.d.ts +0 -8
  109. package/lib/NumberFormat/CollapseNumberRange.js +0 -50
  110. package/lib/NumberFormat/ComputeExponent.d.ts +0 -10
  111. package/lib/NumberFormat/ComputeExponent.js +0 -34
  112. package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts +0 -8
  113. package/lib/NumberFormat/ComputeExponentForMagnitude.js +0 -65
  114. package/lib/NumberFormat/CurrencyDigits.d.ts +0 -6
  115. package/lib/NumberFormat/CurrencyDigits.js +0 -10
  116. package/lib/NumberFormat/FormatApproximately.d.ts +0 -5
  117. package/lib/NumberFormat/FormatApproximately.js +0 -9
  118. package/lib/NumberFormat/FormatNumeric.d.ts +0 -3
  119. package/lib/NumberFormat/FormatNumeric.js +0 -5
  120. package/lib/NumberFormat/FormatNumericRange.d.ts +0 -8
  121. package/lib/NumberFormat/FormatNumericRange.js +0 -11
  122. package/lib/NumberFormat/FormatNumericRangeToParts.d.ts +0 -8
  123. package/lib/NumberFormat/FormatNumericRangeToParts.js +0 -16
  124. package/lib/NumberFormat/FormatNumericToParts.d.ts +0 -5
  125. package/lib/NumberFormat/FormatNumericToParts.js +0 -14
  126. package/lib/NumberFormat/FormatNumericToString.d.ts +0 -9
  127. package/lib/NumberFormat/FormatNumericToString.js +0 -84
  128. package/lib/NumberFormat/GetUnsignedRoundingMode.d.ts +0 -2
  129. package/lib/NumberFormat/GetUnsignedRoundingMode.js +0 -28
  130. package/lib/NumberFormat/InitializeNumberFormat.d.ts +0 -12
  131. package/lib/NumberFormat/InitializeNumberFormat.js +0 -66
  132. package/lib/NumberFormat/PartitionNumberPattern.d.ts +0 -6
  133. package/lib/NumberFormat/PartitionNumberPattern.js +0 -126
  134. package/lib/NumberFormat/PartitionNumberRangePattern.d.ts +0 -8
  135. package/lib/NumberFormat/PartitionNumberRangePattern.js +0 -40
  136. package/lib/NumberFormat/SetNumberFormatDigitOptions.d.ts +0 -5
  137. package/lib/NumberFormat/SetNumberFormatDigitOptions.js +0 -179
  138. package/lib/NumberFormat/SetNumberFormatUnitOptions.d.ts +0 -5
  139. package/lib/NumberFormat/SetNumberFormatUnitOptions.js +0 -50
  140. package/lib/NumberFormat/ToRawFixed.d.ts +0 -9
  141. package/lib/NumberFormat/ToRawFixed.js +0 -119
  142. package/lib/NumberFormat/ToRawPrecision.d.ts +0 -9
  143. package/lib/NumberFormat/ToRawPrecision.js +0 -148
  144. package/lib/NumberFormat/digit-mapping.generated.d.ts +0 -1
  145. package/lib/NumberFormat/digit-mapping.generated.js +0 -782
  146. package/lib/NumberFormat/format_to_parts.d.ts +0 -24
  147. package/lib/NumberFormat/format_to_parts.js +0 -447
  148. package/lib/PartitionPattern.d.ts +0 -8
  149. package/lib/PartitionPattern.js +0 -35
  150. package/lib/SupportedLocales.d.ts +0 -9
  151. package/lib/SupportedLocales.js +0 -20
  152. package/lib/ToIntlMathematicalValue.d.ts +0 -2
  153. package/lib/ToIntlMathematicalValue.js +0 -27
  154. package/lib/constants.d.ts +0 -4
  155. package/lib/constants.js +0 -4
  156. package/lib/data.d.ts +0 -5
  157. package/lib/data.js +0 -13
  158. package/lib/index.d.ts +0 -47
  159. package/lib/index.js +0 -45
  160. package/lib/regex.generated.d.ts +0 -1
  161. package/lib/regex.generated.js +0 -2
  162. package/lib/types/core.d.ts +0 -10
  163. package/lib/types/core.js +0 -1
  164. package/lib/types/date-time.d.ts +0 -135
  165. package/lib/types/date-time.js +0 -6
  166. package/lib/types/displaynames.d.ts +0 -65
  167. package/lib/types/displaynames.js +0 -1
  168. package/lib/types/list.d.ts +0 -18
  169. package/lib/types/list.js +0 -1
  170. package/lib/types/number.d.ts +0 -173
  171. package/lib/types/number.js +0 -1
  172. package/lib/types/plural-rules.d.ts +0 -16
  173. package/lib/types/plural-rules.js +0 -1
  174. package/lib/types/relative-time.d.ts +0 -40
  175. package/lib/types/relative-time.js +0 -1
  176. package/lib/utils.d.ts +0 -30
  177. package/lib/utils.js +0 -135
@@ -1,66 +0,0 @@
1
- import { ResolveLocale } from '@formatjs/intl-localematcher';
2
- import { CanonicalizeLocaleList } from '../CanonicalizeLocaleList';
3
- import { CoerceOptionsToObject } from '../CoerceOptionsToObject';
4
- import { GetOption } from '../GetOption';
5
- import { GetStringOrBooleanOption } from '../GetStringOrBooleanOption';
6
- import { invariant } from '../utils';
7
- import { CurrencyDigits } from './CurrencyDigits';
8
- import { SetNumberFormatDigitOptions } from './SetNumberFormatDigitOptions';
9
- import { SetNumberFormatUnitOptions } from './SetNumberFormatUnitOptions';
10
- /**
11
- * https://tc39.es/ecma402/#sec-initializenumberformat
12
- */
13
- export function InitializeNumberFormat(nf, locales, opts, _a) {
14
- var getInternalSlots = _a.getInternalSlots, localeData = _a.localeData, availableLocales = _a.availableLocales, numberingSystemNames = _a.numberingSystemNames, getDefaultLocale = _a.getDefaultLocale, currencyDigitsData = _a.currencyDigitsData;
15
- var requestedLocales = CanonicalizeLocaleList(locales);
16
- var options = CoerceOptionsToObject(opts);
17
- var opt = Object.create(null);
18
- var matcher = GetOption(options, 'localeMatcher', 'string', ['lookup', 'best fit'], 'best fit');
19
- opt.localeMatcher = matcher;
20
- var numberingSystem = GetOption(options, 'numberingSystem', 'string', undefined, undefined);
21
- if (numberingSystem !== undefined &&
22
- numberingSystemNames.indexOf(numberingSystem) < 0) {
23
- // 8.a. If numberingSystem does not match the Unicode Locale Identifier type nonterminal,
24
- // throw a RangeError exception.
25
- throw RangeError("Invalid numberingSystems: ".concat(numberingSystem));
26
- }
27
- opt.nu = numberingSystem;
28
- var r = ResolveLocale(Array.from(availableLocales), requestedLocales, opt,
29
- // [[RelevantExtensionKeys]] slot, which is a constant
30
- ['nu'], localeData, getDefaultLocale);
31
- var dataLocaleData = localeData[r.dataLocale];
32
- invariant(!!dataLocaleData, "Missing locale data for ".concat(r.dataLocale));
33
- var internalSlots = getInternalSlots(nf);
34
- internalSlots.locale = r.locale;
35
- internalSlots.dataLocale = r.dataLocale;
36
- internalSlots.numberingSystem = r.nu;
37
- internalSlots.dataLocaleData = dataLocaleData;
38
- SetNumberFormatUnitOptions(internalSlots, options);
39
- var style = internalSlots.style;
40
- var notation = GetOption(options, 'notation', 'string', ['standard', 'scientific', 'engineering', 'compact'], 'standard');
41
- internalSlots.notation = notation;
42
- var mnfdDefault;
43
- var mxfdDefault;
44
- if (style === 'currency' && notation === 'standard') {
45
- var currency = internalSlots.currency;
46
- var cDigits = CurrencyDigits(currency, { currencyDigitsData: currencyDigitsData });
47
- mnfdDefault = cDigits;
48
- mxfdDefault = cDigits;
49
- }
50
- else {
51
- mnfdDefault = 0;
52
- mxfdDefault = style === 'percent' ? 0 : 3;
53
- }
54
- SetNumberFormatDigitOptions(internalSlots, options, mnfdDefault, mxfdDefault, notation);
55
- var compactDisplay = GetOption(options, 'compactDisplay', 'string', ['short', 'long'], 'short');
56
- var defaultUseGrouping = 'auto';
57
- if (notation === 'compact') {
58
- internalSlots.compactDisplay = compactDisplay;
59
- defaultUseGrouping = 'min2';
60
- }
61
- var useGrouping = GetStringOrBooleanOption(options, 'useGrouping', ['min2', 'auto', 'always'], 'always', false, defaultUseGrouping);
62
- internalSlots.useGrouping = useGrouping;
63
- var signDisplay = GetOption(options, 'signDisplay', 'string', ['auto', 'never', 'always', 'exceptZero', 'negative'], 'auto');
64
- internalSlots.signDisplay = signDisplay;
65
- return nf;
66
- }
@@ -1,6 +0,0 @@
1
- import Decimal from 'decimal.js';
2
- import { NumberFormatInternal, NumberFormatPart } from '../types/number';
3
- /**
4
- * https://tc39.es/ecma402/#sec-partitionnumberpattern
5
- */
6
- export declare function PartitionNumberPattern(internalSlots: NumberFormatInternal, _x: Decimal): NumberFormatPart[];
@@ -1,126 +0,0 @@
1
- import Decimal from 'decimal.js';
2
- import { invariant } from '../utils';
3
- import { ComputeExponent } from './ComputeExponent';
4
- import formatToParts from './format_to_parts';
5
- import { FormatNumericToString } from './FormatNumericToString';
6
- /**
7
- * https://tc39.es/ecma402/#sec-partitionnumberpattern
8
- */
9
- export function PartitionNumberPattern(internalSlots, _x) {
10
- var _a;
11
- var x = _x;
12
- // IMPL: We need to record the magnitude of the number
13
- var magnitude = 0;
14
- // 2. Let dataLocaleData be internalSlots.[[dataLocaleData]].
15
- var pl = internalSlots.pl, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
16
- // 3. Let symbols be dataLocaleData.[[numbers]].[[symbols]][internalSlots.[[numberingSystem]]].
17
- var symbols = dataLocaleData.numbers.symbols[numberingSystem] ||
18
- dataLocaleData.numbers.symbols[dataLocaleData.numbers.nu[0]];
19
- // 4. Let exponent be 0.
20
- var exponent = 0;
21
- // 5. Let n be ! ToString(x).
22
- var n;
23
- // 6. If x is NaN, then
24
- if (x.isNaN()) {
25
- // 6.a. Let n be symbols.[[nan]].
26
- n = symbols.nan;
27
- }
28
- else if (!x.isFinite()) {
29
- // 7. Else if x is a non-finite Number, then
30
- // 7.a. Let n be symbols.[[infinity]].
31
- n = symbols.infinity;
32
- }
33
- else {
34
- // 8. Else,
35
- if (!x.isZero()) {
36
- // 8.a. If x < 0, let x be -x.
37
- invariant(x.isFinite(), 'Input must be a mathematical value');
38
- // 8.b. If internalSlots.[[style]] is "percent", let x be 100 × x.
39
- if (internalSlots.style == 'percent') {
40
- x = x.times(100);
41
- }
42
- // 8.c. Let exponent be ComputeExponent(numberFormat, x).
43
- ;
44
- _a = ComputeExponent(internalSlots, x), exponent = _a[0],
45
- // IMPL: We need to record the magnitude of the number
46
- magnitude = _a[1];
47
- // 8.d. Let x be x × 10^(-exponent).
48
- x = x.times(Decimal.pow(10, -exponent));
49
- }
50
- // 8.e. Let formatNumberResult be FormatNumericToString(internalSlots, x).
51
- var formatNumberResult = FormatNumericToString(internalSlots, x);
52
- // 8.f. Let n be formatNumberResult.[[formattedString]].
53
- n = formatNumberResult.formattedString;
54
- // 8.g. Let x be formatNumberResult.[[roundedNumber]].
55
- x = formatNumberResult.roundedNumber;
56
- }
57
- // 9. Let sign be 0.
58
- var sign;
59
- // 10. If x is negative, then
60
- var signDisplay = internalSlots.signDisplay;
61
- switch (signDisplay) {
62
- case 'never':
63
- // 10.a. If internalSlots.[[signDisplay]] is "never", then
64
- // 10.a.i. Let sign be 0.
65
- sign = 0;
66
- break;
67
- case 'auto':
68
- // 10.b. Else if internalSlots.[[signDisplay]] is "auto", then
69
- if (x.isPositive() || x.isNaN()) {
70
- // 10.b.i. If x is positive or x is NaN, let sign be 0.
71
- sign = 0;
72
- }
73
- else {
74
- // 10.b.ii. Else, let sign be -1.
75
- sign = -1;
76
- }
77
- break;
78
- case 'always':
79
- // 10.c. Else if internalSlots.[[signDisplay]] is "always", then
80
- if (x.isPositive() || x.isNaN()) {
81
- // 10.c.i. If x is positive or x is NaN, let sign be 1.
82
- sign = 1;
83
- }
84
- else {
85
- // 10.c.ii. Else, let sign be -1.
86
- sign = -1;
87
- }
88
- break;
89
- case 'exceptZero':
90
- // 10.d. Else if internalSlots.[[signDisplay]] is "exceptZero", then
91
- if (x.isZero()) {
92
- // 10.d.i. If x is 0, let sign be 0.
93
- sign = 0;
94
- }
95
- else if (x.isNegative()) {
96
- // 10.d.ii. Else if x is negative, let sign be -1.
97
- sign = -1;
98
- }
99
- else {
100
- // 10.d.iii. Else, let sign be 1.
101
- sign = 1;
102
- }
103
- break;
104
- default:
105
- // 10.e. Else,
106
- invariant(signDisplay === 'negative', 'signDisplay must be "negative"');
107
- if (x.isNegative() && !x.isZero()) {
108
- // 10.e.i. If x is negative and x is not 0, let sign be -1.
109
- sign = -1;
110
- }
111
- else {
112
- // 10.e.ii. Else, let sign be 0.
113
- sign = 0;
114
- }
115
- break;
116
- }
117
- // 11. Return ? FormatNumberToParts(numberFormat, x, n, exponent, sign).
118
- return formatToParts({
119
- roundedNumber: x,
120
- formattedString: n,
121
- exponent: exponent,
122
- // IMPL: We're returning this for our implementation of formatToParts
123
- magnitude: magnitude,
124
- sign: sign,
125
- }, internalSlots.dataLocaleData, pl, internalSlots);
126
- }
@@ -1,8 +0,0 @@
1
- import Decimal from 'decimal.js';
2
- import { NumberFormatInternal, NumberFormatPart } from '../types/number';
3
- /**
4
- * https://tc39.es/ecma402/#sec-partitionnumberrangepattern
5
- */
6
- export declare function PartitionNumberRangePattern(numberFormat: Intl.NumberFormat, x: Decimal, y: Decimal, { getInternalSlots, }: {
7
- getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
8
- }): NumberFormatPart[];
@@ -1,40 +0,0 @@
1
- import { invariant } from '../utils';
2
- import { CollapseNumberRange } from './CollapseNumberRange';
3
- import { FormatApproximately } from './FormatApproximately';
4
- import { FormatNumeric } from './FormatNumeric';
5
- import { PartitionNumberPattern } from './PartitionNumberPattern';
6
- /**
7
- * https://tc39.es/ecma402/#sec-partitionnumberrangepattern
8
- */
9
- export function PartitionNumberRangePattern(numberFormat, x, y, _a) {
10
- var getInternalSlots = _a.getInternalSlots;
11
- // 1. Assert: x and y are both mathematical values.
12
- invariant(!x.isNaN() && !y.isNaN(), 'Input must be a number', RangeError);
13
- var internalSlots = getInternalSlots(numberFormat);
14
- // 3. Let xResult be ? PartitionNumberPattern(numberFormat, x).
15
- var xResult = PartitionNumberPattern(internalSlots, x);
16
- // 4. Let yResult be ? PartitionNumberPattern(numberFormat, y).
17
- var yResult = PartitionNumberPattern(internalSlots, y);
18
- if (FormatNumeric(internalSlots, x) === FormatNumeric(internalSlots, y)) {
19
- var appxResult = FormatApproximately(internalSlots, xResult);
20
- appxResult.forEach(function (el) {
21
- el.source = 'shared';
22
- });
23
- return appxResult;
24
- }
25
- var result = [];
26
- xResult.forEach(function (el) {
27
- el.source = 'startRange';
28
- result.push(el);
29
- });
30
- // 9. Let symbols be internalSlots.[[dataLocaleData]].[[numbers]].[[symbols]][internalSlots.[[numberingSystem]]].
31
- var rangeSeparator = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem]
32
- .rangeSign;
33
- result.push({ type: 'literal', value: rangeSeparator, source: 'shared' });
34
- yResult.forEach(function (el) {
35
- el.source = 'endRange';
36
- result.push(el);
37
- });
38
- // 13. Return ? CollapseNumberRange(numberFormat, result).
39
- return CollapseNumberRange(numberFormat, result, { getInternalSlots: getInternalSlots });
40
- }
@@ -1,5 +0,0 @@
1
- import { NumberFormatDigitInternalSlots, NumberFormatDigitOptions, NumberFormatNotation } from '../types/number';
2
- /**
3
- * https://tc39.es/ecma402/#sec-setnfdigitoptions
4
- */
5
- export declare function SetNumberFormatDigitOptions(internalSlots: NumberFormatDigitInternalSlots, opts: NumberFormatDigitOptions, mnfdDefault: number, mxfdDefault: number, notation: NumberFormatNotation): void;
@@ -1,179 +0,0 @@
1
- import { DefaultNumberOption } from '../DefaultNumberOption';
2
- import { GetNumberOption } from '../GetNumberOption';
3
- import { GetOption } from '../GetOption';
4
- import { invariant } from '../utils';
5
- //IMPL: Valid rounding increments as per implementation
6
- var VALID_ROUNDING_INCREMENTS = new Set([
7
- 1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000,
8
- ]);
9
- /**
10
- * https://tc39.es/ecma402/#sec-setnfdigitoptions
11
- */
12
- export function SetNumberFormatDigitOptions(internalSlots, opts, mnfdDefault, mxfdDefault, notation) {
13
- // 1. Let mnid be ? GetNumberOption(opts, "minimumIntegerDigits", 1, 21, 1).
14
- var mnid = GetNumberOption(opts, 'minimumIntegerDigits', 1, 21, 1);
15
- // 2. Let mnfd be opts.[[MinimumFractionDigits]].
16
- var mnfd = opts.minimumFractionDigits;
17
- // 3. Let mxfd be opts.[[MaximumFractionDigits]].
18
- var mxfd = opts.maximumFractionDigits;
19
- // 4. Let mnsd be opts.[[MinimumSignificantDigits]].
20
- var mnsd = opts.minimumSignificantDigits;
21
- // 5. Let mxsd be opts.[[MaximumSignificantDigits]].
22
- var mxsd = opts.maximumSignificantDigits;
23
- // 6. Set internalSlots.[[MinimumIntegerDigits]] to mnid.
24
- internalSlots.minimumIntegerDigits = mnid;
25
- // 7. Let roundingIncrement be ? GetNumberOption(opts, "roundingIncrement", 1, 5000, 1).
26
- var roundingIncrement = GetNumberOption(opts, 'roundingIncrement', 1, 5000, 1);
27
- // 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.
28
- invariant(VALID_ROUNDING_INCREMENTS.has(roundingIncrement), "Invalid rounding increment value: ".concat(roundingIncrement, ".\nValid values are ").concat(Array.from(VALID_ROUNDING_INCREMENTS).join(', '), "."));
29
- // 9. Let roundingMode be ? GetOption(opts, "roundingMode", "string", « "ceil", "floor", "expand", "trunc", "halfCeil", "halfFloor", "halfExpand", "halfTrunc", "halfEven" », "halfExpand").
30
- var roundingMode = GetOption(opts, 'roundingMode', 'string', [
31
- 'ceil',
32
- 'floor',
33
- 'expand',
34
- 'trunc',
35
- 'halfCeil',
36
- 'halfFloor',
37
- 'halfExpand',
38
- 'halfTrunc',
39
- 'halfEven',
40
- ], 'halfExpand');
41
- // 10. Let roundingPriority be ? GetOption(opts, "roundingPriority", "string", « "auto", "morePrecision", "lessPrecision" », "auto").
42
- var roundingPriority = GetOption(opts, 'roundingPriority', 'string', ['auto', 'morePrecision', 'lessPrecision'], 'auto');
43
- // 11. Let trailingZeroDisplay be ? GetOption(opts, "trailingZeroDisplay", "string", « "auto", "stripIfInteger" », "auto").
44
- var trailingZeroDisplay = GetOption(opts, 'trailingZeroDisplay', 'string', ['auto', 'stripIfInteger'], 'auto');
45
- // 12. If roundingIncrement is not 1, then
46
- if (roundingIncrement !== 1) {
47
- // 12.a. Set mxfdDefault to mnfdDefault.
48
- mxfdDefault = mnfdDefault;
49
- }
50
- // 13. Set internalSlots.[[RoundingIncrement]] to roundingIncrement.
51
- internalSlots.roundingIncrement = roundingIncrement;
52
- // 14. Set internalSlots.[[RoundingMode]] to roundingMode.
53
- internalSlots.roundingMode = roundingMode;
54
- // 15. Set internalSlots.[[TrailingZeroDisplay]] to trailingZeroDisplay.
55
- internalSlots.trailingZeroDisplay = trailingZeroDisplay;
56
- // 16. Let hasSd be true if mnsd is not undefined or mxsd is not undefined; otherwise, let hasSd be false.
57
- var hasSd = mnsd !== undefined || mxsd !== undefined;
58
- // 17. Let hasFd be true if mnfd is not undefined or mxfd is not undefined; otherwise, let hasFd be false.
59
- var hasFd = mnfd !== undefined || mxfd !== undefined;
60
- // 18. Let needSd be true.
61
- var needSd = true;
62
- // 19. Let needFd be true.
63
- var needFd = true;
64
- // 20. If roundingPriority is "auto", then
65
- if (roundingPriority === 'auto') {
66
- // 20.a. Set needSd to hasSd.
67
- needSd = hasSd;
68
- // 20.b. If hasSd is true or hasFd is false and notation is "compact", then
69
- if (hasSd || (!hasFd && notation === 'compact')) {
70
- // 20.b.i. Set needFd to false.
71
- needFd = false;
72
- }
73
- }
74
- // 21. If needSd is true, then
75
- if (needSd) {
76
- // 21.a. If hasSd is true, then
77
- if (hasSd) {
78
- // 21.a.i. Set internalSlots.[[MinimumSignificantDigits]] to ? DefaultNumberOption(mnsd, 1, 21, 1).
79
- internalSlots.minimumSignificantDigits = DefaultNumberOption(mnsd, 1, 21, 1);
80
- // 21.a.ii. Set internalSlots.[[MaximumSignificantDigits]] to ? DefaultNumberOption(mxsd, internalSlots.[[MinimumSignificantDigits]], 21, 21).
81
- internalSlots.maximumSignificantDigits = DefaultNumberOption(mxsd, internalSlots.minimumSignificantDigits, 21, 21);
82
- }
83
- else {
84
- // 21.b. Else,
85
- // 21.b.i. Set internalSlots.[[MinimumSignificantDigits]] to 1.
86
- internalSlots.minimumSignificantDigits = 1;
87
- // 21.b.ii. Set internalSlots.[[MaximumSignificantDigits]] to 21.
88
- internalSlots.maximumSignificantDigits = 21;
89
- }
90
- }
91
- // 22. If needFd is true, then
92
- if (needFd) {
93
- // 22.a. If hasFd is true, then
94
- if (hasFd) {
95
- // 22.a.i. Set mnfd to ? DefaultNumberOption(mnfd, 0, 100, undefined).
96
- mnfd = DefaultNumberOption(mnfd, 0, 100, undefined);
97
- // 22.a.ii. Set mxfd to ? DefaultNumberOption(mxfd, 0, 100, undefined).
98
- mxfd = DefaultNumberOption(mxfd, 0, 100, undefined);
99
- // 22.a.iii. If mnfd is undefined, then
100
- if (mnfd === undefined) {
101
- // 22.a.iii.1. Assert: mxfd is not undefined.
102
- invariant(mxfd !== undefined, 'maximumFractionDigits must be defined');
103
- // 22.a.iii.2. Set mnfd to min(mnfdDefault, mxfd).
104
- mnfd = Math.min(mnfdDefault, mxfd);
105
- }
106
- else if (mxfd === undefined) {
107
- // 22.a.iv. Else if mxfd is undefined, then
108
- // 22.a.iv.1. Set mxfd to max(mxfdDefault, mnfd).
109
- mxfd = Math.max(mxfdDefault, mnfd);
110
- }
111
- else if (mnfd > mxfd) {
112
- // 22.a.v. Else if mnfd > mxfd, throw a RangeError exception.
113
- throw new RangeError("Invalid range, ".concat(mnfd, " > ").concat(mxfd));
114
- }
115
- // 22.a.vi. Set internalSlots.[[MinimumFractionDigits]] to mnfd.
116
- internalSlots.minimumFractionDigits = mnfd;
117
- // 22.a.vii. Set internalSlots.[[MaximumFractionDigits]] to mxfd.
118
- internalSlots.maximumFractionDigits = mxfd;
119
- }
120
- else {
121
- // 22.b. Else,
122
- // 22.b.i. Set internalSlots.[[MinimumFractionDigits]] to mnfdDefault.
123
- internalSlots.minimumFractionDigits = mnfdDefault;
124
- // 22.b.ii. Set internalSlots.[[MaximumFractionDigits]] to mxfdDefault.
125
- internalSlots.maximumFractionDigits = mxfdDefault;
126
- }
127
- }
128
- // 23. If needSd is false and needFd is false, then
129
- if (!needSd && !needFd) {
130
- // 23.a. Set internalSlots.[[MinimumFractionDigits]] to 0.
131
- internalSlots.minimumFractionDigits = 0;
132
- // 23.b. Set internalSlots.[[MaximumFractionDigits]] to 0.
133
- internalSlots.maximumFractionDigits = 0;
134
- // 23.c. Set internalSlots.[[MinimumSignificantDigits]] to 1.
135
- internalSlots.minimumSignificantDigits = 1;
136
- // 23.d. Set internalSlots.[[MaximumSignificantDigits]] to 2.
137
- internalSlots.maximumSignificantDigits = 2;
138
- // 23.e. Set internalSlots.[[RoundingType]] to "morePrecision".
139
- internalSlots.roundingType = 'morePrecision';
140
- // 23.f. Set internalSlots.[[RoundingPriority]] to "morePrecision".
141
- internalSlots.roundingPriority = 'morePrecision';
142
- }
143
- else if (roundingPriority === 'morePrecision') {
144
- // 24. Else if roundingPriority is "morePrecision", then
145
- // 24.a. Set internalSlots.[[RoundingType]] to "morePrecision".
146
- internalSlots.roundingType = 'morePrecision';
147
- // 24.b. Set internalSlots.[[RoundingPriority]] to "morePrecision".
148
- internalSlots.roundingPriority = 'morePrecision';
149
- }
150
- else if (roundingPriority === 'lessPrecision') {
151
- // 25. Else if roundingPriority is "lessPrecision", then
152
- // 25.a. Set internalSlots.[[RoundingType]] to "lessPrecision".
153
- internalSlots.roundingType = 'lessPrecision';
154
- // 25.b. Set internalSlots.[[RoundingPriority]] to "lessPrecision".
155
- internalSlots.roundingPriority = 'lessPrecision';
156
- }
157
- else if (hasSd) {
158
- // 26. Else if hasSd is true, then
159
- // 26.a. Set internalSlots.[[RoundingType]] to "significantDigits".
160
- internalSlots.roundingType = 'significantDigits';
161
- // 26.b. Set internalSlots.[[RoundingPriority]] to "auto".
162
- internalSlots.roundingPriority = 'auto';
163
- }
164
- else {
165
- // 27. Else,
166
- // 27.a. Set internalSlots.[[RoundingType]] to "fractionDigits".
167
- internalSlots.roundingType = 'fractionDigits';
168
- // 27.b. Set internalSlots.[[RoundingPriority]] to "auto".
169
- internalSlots.roundingPriority = 'auto';
170
- }
171
- // 28. If roundingIncrement is not 1, then
172
- if (roundingIncrement !== 1) {
173
- // 28.a. Assert: internalSlots.[[RoundingType]] is "fractionDigits".
174
- invariant(internalSlots.roundingType === 'fractionDigits', 'Invalid roundingType', TypeError);
175
- // 28.b. Assert: internalSlots.[[MaximumFractionDigits]] is equal to internalSlots.[[MinimumFractionDigits]].
176
- invariant(internalSlots.maximumFractionDigits ===
177
- internalSlots.minimumFractionDigits, 'With roundingIncrement > 1, maximumFractionDigits and minimumFractionDigits must be equal.', RangeError);
178
- }
179
- }
@@ -1,5 +0,0 @@
1
- import { NumberFormatInternal, NumberFormatOptions } from '../types/number';
2
- /**
3
- * https://tc39.es/ecma402/#sec-setnumberformatunitoptions
4
- */
5
- export declare function SetNumberFormatUnitOptions(internalSlots: NumberFormatInternal, options?: NumberFormatOptions | undefined): void;
@@ -1,50 +0,0 @@
1
- import { GetOption } from '../GetOption';
2
- import { IsWellFormedCurrencyCode } from '../IsWellFormedCurrencyCode';
3
- import { IsWellFormedUnitIdentifier } from '../IsWellFormedUnitIdentifier';
4
- import { invariant } from '../utils';
5
- /**
6
- * https://tc39.es/ecma402/#sec-setnumberformatunitoptions
7
- */
8
- export function SetNumberFormatUnitOptions(internalSlots, options) {
9
- if (options === void 0) { options = Object.create(null); }
10
- // 1. Let style be ? GetOption(options, "style", "string", « "decimal", "percent", "currency", "unit" », "decimal").
11
- var style = GetOption(options, 'style', 'string', ['decimal', 'percent', 'currency', 'unit'], 'decimal');
12
- // 2. Set internalSlots.[[Style]] to style.
13
- internalSlots.style = style;
14
- // 3. Let currency be ? GetOption(options, "currency", "string", undefined, undefined).
15
- var currency = GetOption(options, 'currency', 'string', undefined, undefined);
16
- // 4. If currency is not undefined, then
17
- // a. If the result of IsWellFormedCurrencyCode(currency) is false, throw a RangeError exception.
18
- invariant(currency === undefined || IsWellFormedCurrencyCode(currency), 'Malformed currency code', RangeError);
19
- // 5. If style is "currency" and currency is undefined, throw a TypeError exception.
20
- invariant(style !== 'currency' || currency !== undefined, 'currency cannot be undefined', TypeError);
21
- // 6. Let currencyDisplay be ? GetOption(options, "currencyDisplay", "string", « "code", "symbol", "narrowSymbol", "name" », "symbol").
22
- var currencyDisplay = GetOption(options, 'currencyDisplay', 'string', ['code', 'symbol', 'narrowSymbol', 'name'], 'symbol');
23
- // 7. Let currencySign be ? GetOption(options, "currencySign", "string", « "standard", "accounting" », "standard").
24
- var currencySign = GetOption(options, 'currencySign', 'string', ['standard', 'accounting'], 'standard');
25
- // 8. Let unit be ? GetOption(options, "unit", "string", undefined, undefined).
26
- var unit = GetOption(options, 'unit', 'string', undefined, undefined);
27
- // 9. If unit is not undefined, then
28
- // a. If the result of IsWellFormedUnitIdentifier(unit) is false, throw a RangeError exception.
29
- invariant(unit === undefined || IsWellFormedUnitIdentifier(unit), 'Invalid unit argument for Intl.NumberFormat()', RangeError);
30
- // 10. If style is "unit" and unit is undefined, throw a TypeError exception.
31
- invariant(style !== 'unit' || unit !== undefined, 'unit cannot be undefined', TypeError);
32
- // 11. Let unitDisplay be ? GetOption(options, "unitDisplay", "string", « "short", "narrow", "long" », "short").
33
- var unitDisplay = GetOption(options, 'unitDisplay', 'string', ['short', 'narrow', 'long'], 'short');
34
- // 12. If style is "currency", then
35
- if (style === 'currency') {
36
- // a. Set internalSlots.[[Currency]] to the result of converting currency to upper case as specified in 6.1.
37
- internalSlots.currency = currency.toUpperCase();
38
- // b. Set internalSlots.[[CurrencyDisplay]] to currencyDisplay.
39
- internalSlots.currencyDisplay = currencyDisplay;
40
- // c. Set internalSlots.[[CurrencySign]] to currencySign.
41
- internalSlots.currencySign = currencySign;
42
- }
43
- // 13. If style is "unit", then
44
- if (style === 'unit') {
45
- // a. Set internalSlots.[[Unit]] to unit.
46
- internalSlots.unit = unit;
47
- // b. Set internalSlots.[[UnitDisplay]] to unitDisplay.
48
- internalSlots.unitDisplay = unitDisplay;
49
- }
50
- }
@@ -1,9 +0,0 @@
1
- import Decimal from 'decimal.js';
2
- import { RawNumberFormatResult, UnsignedRoundingModeType } from '../types/number';
3
- /**
4
- * https://tc39.es/ecma402/#sec-torawfixed
5
- * @param x a finite non-negative Number or BigInt
6
- * @param minFraction an integer between 0 and 20
7
- * @param maxFraction an integer between 0 and 20
8
- */
9
- export declare function ToRawFixed(x: Decimal, minFraction: number, maxFraction: number, roundingIncrement: number, unsignedRoundingMode: UnsignedRoundingModeType): RawNumberFormatResult;
@@ -1,119 +0,0 @@
1
- import Decimal from 'decimal.js';
2
- import { repeat } from '../utils';
3
- import { ApplyUnsignedRoundingMode } from './ApplyUnsignedRoundingMode';
4
- //IMPL: Setting Decimal configuration
5
- Decimal.set({
6
- toExpPos: 100,
7
- });
8
- //IMPL: Helper function to calculate raw fixed value
9
- function ToRawFixedFn(n, f) {
10
- return n.times(Decimal.pow(10, -f));
11
- }
12
- //IMPL: Helper function to find n1 and r1
13
- function findN1R1(x, f, roundingIncrement) {
14
- var nx = x.times(Decimal.pow(10, f)).floor();
15
- var n1 = nx.div(roundingIncrement).floor().times(roundingIncrement);
16
- var r1 = ToRawFixedFn(n1, f);
17
- return {
18
- n1: n1,
19
- r1: r1,
20
- };
21
- }
22
- //IMPL: Helper function to find n2 and r2
23
- function findN2R2(x, f, roundingIncrement) {
24
- var nx = x.times(Decimal.pow(10, f)).ceil();
25
- var n2 = nx.div(roundingIncrement).ceil().times(roundingIncrement);
26
- var r2 = ToRawFixedFn(n2, f);
27
- return {
28
- n2: n2,
29
- r2: r2,
30
- };
31
- }
32
- /**
33
- * https://tc39.es/ecma402/#sec-torawfixed
34
- * @param x a finite non-negative Number or BigInt
35
- * @param minFraction an integer between 0 and 20
36
- * @param maxFraction an integer between 0 and 20
37
- */
38
- export function ToRawFixed(x, minFraction, maxFraction, roundingIncrement, unsignedRoundingMode) {
39
- // 1. Let f be maxFraction.
40
- var f = maxFraction;
41
- // 2. Let n1 and r1 be the results of performing the maximized rounding of x to f fraction digits.
42
- var _a = findN1R1(x, f, roundingIncrement), n1 = _a.n1, r1 = _a.r1;
43
- // 3. Let n2 and r2 be the results of performing the minimized rounding of x to f fraction digits.
44
- var _b = findN2R2(x, f, roundingIncrement), n2 = _b.n2, r2 = _b.r2;
45
- // 4. Let r be ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode).
46
- var r = ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode);
47
- var n, xFinal;
48
- var m;
49
- // 5. If r is equal to r1, then
50
- if (r.eq(r1)) {
51
- // a. Let n be n1.
52
- n = n1;
53
- // b. Let xFinal be r1.
54
- xFinal = r1;
55
- }
56
- else {
57
- // 6. Else,
58
- // a. Let n be n2.
59
- n = n2;
60
- // b. Let xFinal be r2.
61
- xFinal = r2;
62
- }
63
- // 7. If n is 0, let m be "0".
64
- if (n.isZero()) {
65
- m = '0';
66
- }
67
- else {
68
- // 8. Else, let m be the String representation of n.
69
- m = n.toString();
70
- }
71
- var int;
72
- // 9. If f is not 0, then
73
- if (f !== 0) {
74
- // a. Let k be the length of m.
75
- var k = m.length;
76
- // b. If k < f, then
77
- if (k <= f) {
78
- // i. Let z be the String value consisting of f + 1 - k occurrences of the character "0".
79
- var z = repeat('0', f - k + 1);
80
- // ii. Set m to the string-concatenation of z and m.
81
- m = z + m;
82
- // iii. Set k to f + 1.
83
- k = f + 1;
84
- }
85
- // c. Let a be the substring of m from 0 to k - f.
86
- var a = m.slice(0, k - f);
87
- // d. Let b be the substring of m from k - f to k.
88
- var b = m.slice(m.length - f);
89
- // e. Set m to the string-concatenation of a, ".", and b.
90
- m = a + '.' + b;
91
- // f. Let int be the length of a.
92
- int = a.length;
93
- }
94
- else {
95
- // 10. Else, let int be the length of m.
96
- int = m.length;
97
- }
98
- // 11. Let cut be maxFraction - minFraction.
99
- var cut = maxFraction - minFraction;
100
- // 12. Repeat, while cut > 0 and the last character of m is "0",
101
- while (cut > 0 && m[m.length - 1] === '0') {
102
- // a. Remove the last character from m.
103
- m = m.slice(0, m.length - 1);
104
- // b. Decrease cut by 1.
105
- cut--;
106
- }
107
- // 13. If the last character of m is ".", then
108
- if (m[m.length - 1] === '\u002e') {
109
- // a. Remove the last character from m.
110
- m = m.slice(0, m.length - 1);
111
- }
112
- // 14. Return the Record { [[FormattedString]]: m, [[RoundedNumber]]: xFinal, [[IntegerDigitsCount]]: int, [[RoundingMagnitude]]: -f }.
113
- return {
114
- formattedString: m,
115
- roundedNumber: xFinal,
116
- integerDigitsCount: int,
117
- roundingMagnitude: -f,
118
- };
119
- }
@@ -1,9 +0,0 @@
1
- import Decimal from 'decimal.js';
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
- */
9
- export declare function ToRawPrecision(x: Decimal, minPrecision: number, maxPrecision: number, unsignedRoundingMode: UnsignedRoundingModeType): RawNumberFormatResult;