@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.
- package/262.d.ts +3 -2
- package/262.js +77 -15
- package/NumberFormat/ComputeExponent.d.ts +1 -3
- package/NumberFormat/ComputeExponent.js +7 -11
- package/NumberFormat/ComputeExponentForMagnitude.d.ts +1 -3
- package/NumberFormat/ComputeExponentForMagnitude.js +2 -5
- package/NumberFormat/FormatApproximately.d.ts +1 -3
- package/NumberFormat/FormatApproximately.js +1 -3
- package/NumberFormat/FormatNumeric.d.ts +3 -0
- package/NumberFormat/FormatNumeric.js +8 -0
- package/NumberFormat/FormatNumericToParts.js +1 -1
- package/NumberFormat/FormatNumericToString.d.ts +1 -1
- package/NumberFormat/FormatNumericToString.js +2 -1
- package/NumberFormat/InitializeNumberFormat.js +1 -1
- package/NumberFormat/PartitionNumberPattern.d.ts +2 -4
- package/NumberFormat/PartitionNumberPattern.js +53 -14
- package/NumberFormat/PartitionNumberRangePattern.js +27 -19
- package/NumberFormat/SetNumberFormatDigitOptions.js +73 -3
- package/NumberFormat/SetNumberFormatUnitOptions.d.ts +1 -3
- package/NumberFormat/SetNumberFormatUnitOptions.js +26 -15
- package/NumberFormat/ToRawFixed.d.ts +2 -3
- package/NumberFormat/ToRawFixed.js +39 -7
- package/NumberFormat/ToRawPrecision.d.ts +6 -0
- package/NumberFormat/ToRawPrecision.js +61 -19
- package/NumberFormat/format_to_parts.js +27 -12
- package/ToIntlMathematicalValue.d.ts +2 -0
- package/ToIntlMathematicalValue.js +31 -0
- package/index.d.ts +2 -0
- package/index.js +4 -1
- package/lib/262.d.ts +3 -2
- package/lib/262.js +76 -15
- package/lib/NumberFormat/ComputeExponent.d.ts +1 -3
- package/lib/NumberFormat/ComputeExponent.js +6 -11
- package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts +1 -3
- package/lib/NumberFormat/ComputeExponentForMagnitude.js +2 -5
- package/lib/NumberFormat/FormatApproximately.d.ts +1 -3
- package/lib/NumberFormat/FormatApproximately.js +1 -3
- package/lib/NumberFormat/FormatNumeric.d.ts +3 -0
- package/lib/NumberFormat/FormatNumeric.js +5 -0
- package/lib/NumberFormat/FormatNumericToParts.js +1 -1
- package/lib/NumberFormat/FormatNumericToString.d.ts +1 -1
- package/lib/NumberFormat/FormatNumericToString.js +2 -1
- package/lib/NumberFormat/InitializeNumberFormat.js +1 -1
- package/lib/NumberFormat/PartitionNumberPattern.d.ts +2 -4
- package/lib/NumberFormat/PartitionNumberPattern.js +53 -14
- package/lib/NumberFormat/PartitionNumberRangePattern.js +27 -19
- package/lib/NumberFormat/SetNumberFormatDigitOptions.js +73 -3
- package/lib/NumberFormat/SetNumberFormatUnitOptions.d.ts +1 -3
- package/lib/NumberFormat/SetNumberFormatUnitOptions.js +26 -15
- package/lib/NumberFormat/ToRawFixed.d.ts +2 -3
- package/lib/NumberFormat/ToRawFixed.js +39 -7
- package/lib/NumberFormat/ToRawPrecision.d.ts +6 -0
- package/lib/NumberFormat/ToRawPrecision.js +62 -20
- package/lib/NumberFormat/format_to_parts.js +27 -12
- package/lib/ToIntlMathematicalValue.d.ts +2 -0
- package/lib/ToIntlMathematicalValue.js +27 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/package.json +3 -3
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.default = formatToParts;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
|
|
6
|
-
var constants_1 = require("../constants");
|
|
7
6
|
var regex_generated_1 = require("../regex.generated");
|
|
8
7
|
var digit_mapping_generated_1 = require("./digit-mapping.generated");
|
|
9
8
|
var GetUnsignedRoundingMode_1 = require("./GetUnsignedRoundingMode");
|
|
@@ -16,6 +15,7 @@ var CARET_S_UNICODE_REGEX = new RegExp("^".concat(regex_generated_1.S_UNICODE_RE
|
|
|
16
15
|
var S_DOLLAR_UNICODE_REGEX = new RegExp("".concat(regex_generated_1.S_UNICODE_REGEX.source, "$"));
|
|
17
16
|
var CLDR_NUMBER_PATTERN = /[#0](?:[\.,][#0]+)*/g;
|
|
18
17
|
function formatToParts(numberResult, data, pl, options) {
|
|
18
|
+
var _a;
|
|
19
19
|
var sign = numberResult.sign, exponent = numberResult.exponent, magnitude = numberResult.magnitude;
|
|
20
20
|
var notation = options.notation, style = options.style, numberingSystem = options.numberingSystem;
|
|
21
21
|
var defaultNumberingSystem = data.numbers.nu[0];
|
|
@@ -124,7 +124,7 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
124
124
|
// We only need to handle scientific and engineering notation here.
|
|
125
125
|
numberParts.push.apply(numberParts, partitionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem,
|
|
126
126
|
// If compact number pattern exists, do not insert group separators.
|
|
127
|
-
!compactNumberPattern &&
|
|
127
|
+
!compactNumberPattern && ((_a = options.useGrouping) !== null && _a !== void 0 ? _a : true), decimalNumberPattern, style, options.roundingIncrement, (0, GetUnsignedRoundingMode_1.GetUnsignedRoundingMode)(options.roundingMode, sign === -1)));
|
|
128
128
|
break;
|
|
129
129
|
}
|
|
130
130
|
case '-':
|
|
@@ -167,7 +167,9 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
167
167
|
var unitName = void 0;
|
|
168
168
|
var currencyNameData = data.currencies[options.currency];
|
|
169
169
|
if (currencyNameData) {
|
|
170
|
-
unitName = selectPlural(pl, numberResult.roundedNumber
|
|
170
|
+
unitName = selectPlural(pl, numberResult.roundedNumber
|
|
171
|
+
.times(decimal_js_1.default.pow(10, exponent))
|
|
172
|
+
.toNumber(), currencyNameData.displayName);
|
|
171
173
|
}
|
|
172
174
|
else {
|
|
173
175
|
// Fallback for unknown currency
|
|
@@ -176,8 +178,8 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
176
178
|
// Do {0} and {1} substitution
|
|
177
179
|
var unitPatternParts = unitPattern.split(/(\{[01]\})/g);
|
|
178
180
|
var result = [];
|
|
179
|
-
for (var
|
|
180
|
-
var part = unitPatternParts_1[
|
|
181
|
+
for (var _b = 0, unitPatternParts_1 = unitPatternParts; _b < unitPatternParts_1.length; _b++) {
|
|
182
|
+
var part = unitPatternParts_1[_b];
|
|
181
183
|
switch (part) {
|
|
182
184
|
case '{0}':
|
|
183
185
|
result.push.apply(result, numberParts);
|
|
@@ -204,15 +206,19 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
204
206
|
var unitPattern = void 0;
|
|
205
207
|
if (unitData) {
|
|
206
208
|
// Simple unit pattern
|
|
207
|
-
unitPattern = selectPlural(pl, numberResult.roundedNumber
|
|
209
|
+
unitPattern = selectPlural(pl, numberResult.roundedNumber
|
|
210
|
+
.times(decimal_js_1.default.pow(10, exponent))
|
|
211
|
+
.toNumber(), data.units.simple[unit][unitDisplay]);
|
|
208
212
|
}
|
|
209
213
|
else {
|
|
210
214
|
// See: http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
|
|
211
215
|
// If cannot find unit in the simple pattern, it must be "per" compound pattern.
|
|
212
216
|
// Implementation note: we are not following TR-35 here because we need to format to parts!
|
|
213
|
-
var
|
|
217
|
+
var _c = unit.split('-per-'), numeratorUnit = _c[0], denominatorUnit = _c[1];
|
|
214
218
|
unitData = data.units.simple[numeratorUnit];
|
|
215
|
-
var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber
|
|
219
|
+
var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber
|
|
220
|
+
.times(decimal_js_1.default.pow(10, exponent))
|
|
221
|
+
.toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
|
|
216
222
|
var perUnitPattern = data.units.simple[denominatorUnit].perUnit[unitDisplay];
|
|
217
223
|
if (perUnitPattern) {
|
|
218
224
|
// perUnitPattern exists, combine it with numeratorUnitPattern
|
|
@@ -230,8 +236,8 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
230
236
|
}
|
|
231
237
|
var result = [];
|
|
232
238
|
// We need spacing around "{0}" because they are not treated as "unit" parts, but "literal".
|
|
233
|
-
for (var
|
|
234
|
-
var part = _d
|
|
239
|
+
for (var _d = 0, _e = unitPattern.split(/(\s*\{0\}\s*)/); _d < _e.length; _d++) {
|
|
240
|
+
var part = _e[_d];
|
|
235
241
|
var interpolateMatch = /^(\s*)\{0\}(\s*)$/.exec(part);
|
|
236
242
|
if (interpolateMatch) {
|
|
237
243
|
// Space before "{0}"
|
|
@@ -298,8 +304,17 @@ decimalNumberPattern, style, roundingIncrement, unsignedRoundingMode) {
|
|
|
298
304
|
// unless the rounded number is greater than 10000:
|
|
299
305
|
// NumberFormat('de', {notation: 'compact', compactDisplay: 'short'}).format(1234) //=> "1234"
|
|
300
306
|
// NumberFormat('de').format(1234) //=> "1.234"
|
|
301
|
-
|
|
302
|
-
|
|
307
|
+
var shouldUseGrouping = false;
|
|
308
|
+
if (useGrouping === 'always') {
|
|
309
|
+
shouldUseGrouping = true;
|
|
310
|
+
}
|
|
311
|
+
else if (useGrouping === 'min2') {
|
|
312
|
+
shouldUseGrouping = x.greaterThanOrEqualTo(10000);
|
|
313
|
+
}
|
|
314
|
+
else if (useGrouping === 'auto' || useGrouping) {
|
|
315
|
+
shouldUseGrouping = notation !== 'compact' || x.greaterThanOrEqualTo(10000);
|
|
316
|
+
}
|
|
317
|
+
if (shouldUseGrouping) {
|
|
303
318
|
// a. Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator.
|
|
304
319
|
// For currency we should use `currencyGroup` instead of generic `group`
|
|
305
320
|
var groupSepSymbol = style === 'currency' && symbols.currencyGroup != null
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ToIntlMathematicalValue = ToIntlMathematicalValue;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
|
|
6
|
+
var _262_1 = require("./262");
|
|
7
|
+
function ToIntlMathematicalValue(input) {
|
|
8
|
+
var primValue = (0, _262_1.ToPrimitive)(input, 'number');
|
|
9
|
+
if (typeof primValue === 'bigint') {
|
|
10
|
+
return new decimal_js_1.default(primValue);
|
|
11
|
+
}
|
|
12
|
+
// IMPL
|
|
13
|
+
if (primValue === undefined) {
|
|
14
|
+
return new decimal_js_1.default(NaN);
|
|
15
|
+
}
|
|
16
|
+
if (primValue === true) {
|
|
17
|
+
return new decimal_js_1.default(1);
|
|
18
|
+
}
|
|
19
|
+
if (primValue === false) {
|
|
20
|
+
return new decimal_js_1.default(0);
|
|
21
|
+
}
|
|
22
|
+
if (primValue === null) {
|
|
23
|
+
return new decimal_js_1.default(0);
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
return new decimal_js_1.default(primValue);
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
return new decimal_js_1.default(NaN);
|
|
30
|
+
}
|
|
31
|
+
}
|
package/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/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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;
|
|
3
|
+
exports.ToIntlMathematicalValue = 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);
|
|
@@ -21,6 +21,7 @@ tslib_1.__exportStar(require("./NumberFormat/CurrencyDigits"), exports);
|
|
|
21
21
|
var format_to_parts_1 = require("./NumberFormat/format_to_parts");
|
|
22
22
|
Object.defineProperty(exports, "_formatToParts", { enumerable: true, get: function () { return tslib_1.__importDefault(format_to_parts_1).default; } });
|
|
23
23
|
tslib_1.__exportStar(require("./NumberFormat/FormatApproximately"), exports);
|
|
24
|
+
tslib_1.__exportStar(require("./NumberFormat/FormatNumeric"), exports);
|
|
24
25
|
tslib_1.__exportStar(require("./NumberFormat/FormatNumericRange"), exports);
|
|
25
26
|
tslib_1.__exportStar(require("./NumberFormat/FormatNumericRangeToParts"), exports);
|
|
26
27
|
tslib_1.__exportStar(require("./NumberFormat/FormatNumericToParts"), exports);
|
|
@@ -61,3 +62,5 @@ Object.defineProperty(exports, "createMemoizedPluralRules", { enumerable: true,
|
|
|
61
62
|
Object.defineProperty(exports, "invariant", { enumerable: true, get: function () { return utils_2.invariant; } });
|
|
62
63
|
var constants_1 = require("./constants");
|
|
63
64
|
Object.defineProperty(exports, "ZERO", { enumerable: true, get: function () { return constants_1.ZERO; } });
|
|
65
|
+
var ToIntlMathematicalValue_1 = require("./ToIntlMathematicalValue");
|
|
66
|
+
Object.defineProperty(exports, "ToIntlMathematicalValue", { enumerable: true, get: function () { return ToIntlMathematicalValue_1.ToIntlMathematicalValue; } });
|
package/lib/262.d.ts
CHANGED
|
@@ -7,12 +7,12 @@ export declare function ToString(o: unknown): string;
|
|
|
7
7
|
* https://tc39.es/ecma262/#sec-tonumber
|
|
8
8
|
* @param val
|
|
9
9
|
*/
|
|
10
|
-
export declare function ToNumber(
|
|
10
|
+
export declare function ToNumber(arg: any): Decimal;
|
|
11
11
|
/**
|
|
12
12
|
* https://tc39.es/ecma262/#sec-timeclip
|
|
13
13
|
* @param time
|
|
14
14
|
*/
|
|
15
|
-
export declare function TimeClip(time:
|
|
15
|
+
export declare function TimeClip(time: Decimal): Decimal;
|
|
16
16
|
/**
|
|
17
17
|
* https://tc39.es/ecma262/#sec-toobject
|
|
18
18
|
* @param arg
|
|
@@ -90,3 +90,4 @@ export declare function OrdinaryHasInstance(C: Object, O: any, internalSlots?: {
|
|
|
90
90
|
boundTargetFunction: any;
|
|
91
91
|
}): boolean;
|
|
92
92
|
export declare function msFromTime(t: number): number;
|
|
93
|
+
export declare function ToPrimitive<T extends 'string' | 'number' = 'string' | 'number'>(input: any, preferredType: T): string | number | boolean | undefined | null;
|
package/lib/262.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Decimal } from 'decimal.js';
|
|
2
2
|
import { ZERO } from './constants';
|
|
3
|
+
import { invariant } from './utils';
|
|
3
4
|
/**
|
|
4
5
|
* https://tc39.es/ecma262/#sec-tostring
|
|
5
6
|
*/
|
|
@@ -14,20 +15,32 @@ export function ToString(o) {
|
|
|
14
15
|
* https://tc39.es/ecma262/#sec-tonumber
|
|
15
16
|
* @param val
|
|
16
17
|
*/
|
|
17
|
-
export function ToNumber(
|
|
18
|
-
if (
|
|
18
|
+
export function ToNumber(arg) {
|
|
19
|
+
if (typeof arg === 'number') {
|
|
20
|
+
return new Decimal(arg);
|
|
21
|
+
}
|
|
22
|
+
invariant(typeof arg !== 'bigint' && typeof arg !== 'symbol', 'BigInt and Symbol are not supported', TypeError);
|
|
23
|
+
if (arg === undefined) {
|
|
19
24
|
return new Decimal(NaN);
|
|
20
25
|
}
|
|
21
|
-
if (
|
|
26
|
+
if (arg === null || arg === 0) {
|
|
22
27
|
return ZERO;
|
|
23
28
|
}
|
|
24
|
-
if (
|
|
25
|
-
return new Decimal(
|
|
26
|
-
}
|
|
27
|
-
if (typeof
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
if (arg === true) {
|
|
30
|
+
return new Decimal(1);
|
|
31
|
+
}
|
|
32
|
+
if (typeof arg === 'string') {
|
|
33
|
+
try {
|
|
34
|
+
return new Decimal(arg);
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
return new Decimal(NaN);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
invariant(typeof arg === 'object', 'object expected', TypeError);
|
|
41
|
+
var primValue = ToPrimitive(arg, 'number');
|
|
42
|
+
invariant(typeof primValue !== 'object', 'object expected', TypeError);
|
|
43
|
+
return ToNumber(primValue);
|
|
31
44
|
}
|
|
32
45
|
/**
|
|
33
46
|
* https://tc39.es/ecma262/#sec-tointeger
|
|
@@ -52,13 +65,13 @@ function ToInteger(n) {
|
|
|
52
65
|
* @param time
|
|
53
66
|
*/
|
|
54
67
|
export function TimeClip(time) {
|
|
55
|
-
if (!isFinite(
|
|
56
|
-
return NaN;
|
|
68
|
+
if (!time.isFinite()) {
|
|
69
|
+
return new Decimal(NaN);
|
|
57
70
|
}
|
|
58
|
-
if (
|
|
59
|
-
return NaN;
|
|
71
|
+
if (time.abs().greaterThan(8.64 * 1e15)) {
|
|
72
|
+
return new Decimal(NaN);
|
|
60
73
|
}
|
|
61
|
-
return ToInteger(time)
|
|
74
|
+
return ToInteger(time);
|
|
62
75
|
}
|
|
63
76
|
/**
|
|
64
77
|
* https://tc39.es/ecma262/#sec-toobject
|
|
@@ -330,3 +343,51 @@ export function OrdinaryHasInstance(C, O, internalSlots) {
|
|
|
330
343
|
export function msFromTime(t) {
|
|
331
344
|
return mod(t, MS_PER_SECOND);
|
|
332
345
|
}
|
|
346
|
+
function OrdinaryToPrimitive(O, hint) {
|
|
347
|
+
var methodNames;
|
|
348
|
+
if (hint === 'string') {
|
|
349
|
+
methodNames = ['toString', 'valueOf'];
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
methodNames = ['valueOf', 'toString'];
|
|
353
|
+
}
|
|
354
|
+
for (var _i = 0, methodNames_1 = methodNames; _i < methodNames_1.length; _i++) {
|
|
355
|
+
var name_1 = methodNames_1[_i];
|
|
356
|
+
var method = O[name_1];
|
|
357
|
+
if (IsCallable(method)) {
|
|
358
|
+
var result = method.call(O);
|
|
359
|
+
if (typeof result !== 'object') {
|
|
360
|
+
return result;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
throw new TypeError('Cannot convert object to primitive value');
|
|
365
|
+
}
|
|
366
|
+
export function ToPrimitive(input, preferredType) {
|
|
367
|
+
if (typeof input === 'object' && input != null) {
|
|
368
|
+
var exoticToPrim = Symbol.toPrimitive in input ? input[Symbol.toPrimitive] : undefined;
|
|
369
|
+
var hint = void 0;
|
|
370
|
+
if (exoticToPrim !== undefined) {
|
|
371
|
+
if (preferredType === undefined) {
|
|
372
|
+
hint = 'default';
|
|
373
|
+
}
|
|
374
|
+
else if (preferredType === 'string') {
|
|
375
|
+
hint = 'string';
|
|
376
|
+
}
|
|
377
|
+
else {
|
|
378
|
+
invariant(preferredType === 'number', 'preferredType must be "string" or "number"');
|
|
379
|
+
hint = 'number';
|
|
380
|
+
}
|
|
381
|
+
var result = exoticToPrim.call(input, hint);
|
|
382
|
+
if (typeof result !== 'object') {
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
throw new TypeError('Cannot convert exotic object to primitive.');
|
|
386
|
+
}
|
|
387
|
+
if (preferredType === undefined) {
|
|
388
|
+
preferredType = 'number';
|
|
389
|
+
}
|
|
390
|
+
return OrdinaryToPrimitive(input, preferredType);
|
|
391
|
+
}
|
|
392
|
+
return input;
|
|
393
|
+
}
|
|
@@ -7,6 +7,4 @@ import { NumberFormatInternal } from '../types/number';
|
|
|
7
7
|
*
|
|
8
8
|
* NOT IN SPEC: it returns [exponent, magnitude].
|
|
9
9
|
*/
|
|
10
|
-
export declare function ComputeExponent(
|
|
11
|
-
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
|
|
12
|
-
}): [number, number];
|
|
10
|
+
export declare function ComputeExponent(internalSlots: NumberFormatInternal, x: Decimal): [number, number];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
2
|
import { ComputeExponentForMagnitude } from './ComputeExponentForMagnitude';
|
|
3
3
|
import { FormatNumericToString } from './FormatNumericToString';
|
|
4
4
|
/**
|
|
@@ -8,8 +8,7 @@ import { FormatNumericToString } from './FormatNumericToString';
|
|
|
8
8
|
*
|
|
9
9
|
* NOT IN SPEC: it returns [exponent, magnitude].
|
|
10
10
|
*/
|
|
11
|
-
export function ComputeExponent(
|
|
12
|
-
var getInternalSlots = _a.getInternalSlots;
|
|
11
|
+
export function ComputeExponent(internalSlots, x) {
|
|
13
12
|
if (x.isZero()) {
|
|
14
13
|
return [0, 0];
|
|
15
14
|
}
|
|
@@ -17,12 +16,10 @@ export function ComputeExponent(numberFormat, x, _a) {
|
|
|
17
16
|
x = x.negated();
|
|
18
17
|
}
|
|
19
18
|
var magnitude = x.log(10).floor();
|
|
20
|
-
var exponent = ComputeExponentForMagnitude(
|
|
21
|
-
getInternalSlots: getInternalSlots,
|
|
22
|
-
});
|
|
19
|
+
var exponent = ComputeExponentForMagnitude(internalSlots, magnitude);
|
|
23
20
|
// Preserve more precision by doing multiplication when exponent is negative.
|
|
24
|
-
x = x.times(
|
|
25
|
-
var formatNumberResult = FormatNumericToString(
|
|
21
|
+
x = x.times(Decimal.pow(10, -exponent));
|
|
22
|
+
var formatNumberResult = FormatNumericToString(internalSlots, x);
|
|
26
23
|
if (formatNumberResult.roundedNumber.isZero()) {
|
|
27
24
|
return [exponent, magnitude.toNumber()];
|
|
28
25
|
}
|
|
@@ -31,9 +28,7 @@ export function ComputeExponent(numberFormat, x, _a) {
|
|
|
31
28
|
return [exponent, magnitude.toNumber()];
|
|
32
29
|
}
|
|
33
30
|
return [
|
|
34
|
-
ComputeExponentForMagnitude(
|
|
35
|
-
getInternalSlots: getInternalSlots,
|
|
36
|
-
}),
|
|
31
|
+
ComputeExponentForMagnitude(internalSlots, magnitude.plus(1)),
|
|
37
32
|
magnitude.plus(1).toNumber(),
|
|
38
33
|
];
|
|
39
34
|
}
|
|
@@ -5,6 +5,4 @@ import { NumberFormatInternal } from '../types/number';
|
|
|
5
5
|
* number of the given magnitude (power of ten of the most significant digit) according to the
|
|
6
6
|
* locale and the desired notation (scientific, engineering, or compact).
|
|
7
7
|
*/
|
|
8
|
-
export declare function ComputeExponentForMagnitude(
|
|
9
|
-
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
|
|
10
|
-
}): number;
|
|
8
|
+
export declare function ComputeExponentForMagnitude(internalSlots: NumberFormatInternal, magnitude: Decimal): number;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
|
-
import { TEN } from '../constants';
|
|
3
2
|
import { invariant } from '../utils';
|
|
4
3
|
Decimal.set({
|
|
5
4
|
toExpPos: 100,
|
|
@@ -9,9 +8,7 @@ Decimal.set({
|
|
|
9
8
|
* number of the given magnitude (power of ten of the most significant digit) according to the
|
|
10
9
|
* locale and the desired notation (scientific, engineering, or compact).
|
|
11
10
|
*/
|
|
12
|
-
export function ComputeExponentForMagnitude(
|
|
13
|
-
var getInternalSlots = _a.getInternalSlots;
|
|
14
|
-
var internalSlots = getInternalSlots(numberFormat);
|
|
11
|
+
export function ComputeExponentForMagnitude(internalSlots, magnitude) {
|
|
15
12
|
var notation = internalSlots.notation, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
|
|
16
13
|
switch (notation) {
|
|
17
14
|
case 'standard':
|
|
@@ -40,7 +37,7 @@ export function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
|
|
|
40
37
|
if (!thresholdMap) {
|
|
41
38
|
return 0;
|
|
42
39
|
}
|
|
43
|
-
var num =
|
|
40
|
+
var num = Decimal.pow(10, magnitude).toString();
|
|
44
41
|
var thresholds = Object.keys(thresholdMap); // TODO: this can be pre-processed
|
|
45
42
|
if (num < thresholds[0]) {
|
|
46
43
|
return 0;
|
|
@@ -2,6 +2,4 @@ import { NumberFormatInternal, NumberFormatPart } from '../types/number';
|
|
|
2
2
|
/**
|
|
3
3
|
* https://tc39.es/ecma402/#sec-formatapproximately
|
|
4
4
|
*/
|
|
5
|
-
export declare function FormatApproximately(
|
|
6
|
-
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
|
|
7
|
-
}): NumberFormatPart[];
|
|
5
|
+
export declare function FormatApproximately(internalSlots: NumberFormatInternal, result: NumberFormatPart[]): NumberFormatPart[];
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* https://tc39.es/ecma402/#sec-formatapproximately
|
|
3
3
|
*/
|
|
4
|
-
export function FormatApproximately(
|
|
5
|
-
var getInternalSlots = _a.getInternalSlots;
|
|
6
|
-
var internalSlots = getInternalSlots(numberFormat);
|
|
4
|
+
export function FormatApproximately(internalSlots, result) {
|
|
7
5
|
var symbols = internalSlots.dataLocaleData.numbers.symbols[internalSlots.numberingSystem];
|
|
8
6
|
var approximatelySign = symbols.approximatelySign;
|
|
9
7
|
result.push({ type: 'approximatelySign', value: approximatelySign });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ArrayCreate } from '../262';
|
|
2
2
|
import { PartitionNumberPattern } from './PartitionNumberPattern';
|
|
3
3
|
export function FormatNumericToParts(nf, x, implDetails) {
|
|
4
|
-
var parts = PartitionNumberPattern(nf, x
|
|
4
|
+
var parts = PartitionNumberPattern(implDetails.getInternalSlots(nf), x);
|
|
5
5
|
var result = ArrayCreate(0);
|
|
6
6
|
for (var _i = 0, parts_1 = parts; _i < parts_1.length; _i++) {
|
|
7
7
|
var part = parts_1[_i];
|
|
@@ -3,7 +3,7 @@ import { NumberFormatDigitInternalSlots } from '../types/number';
|
|
|
3
3
|
/**
|
|
4
4
|
* https://tc39.es/ecma402/#sec-formatnumberstring
|
|
5
5
|
*/
|
|
6
|
-
export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits' | 'roundingIncrement' | 'roundingMode' | 'trailingZeroDisplay'>,
|
|
6
|
+
export declare function FormatNumericToString(intlObject: Pick<NumberFormatDigitInternalSlots, 'roundingType' | 'minimumSignificantDigits' | 'maximumSignificantDigits' | 'minimumIntegerDigits' | 'minimumFractionDigits' | 'maximumFractionDigits' | 'roundingIncrement' | 'roundingMode' | 'trailingZeroDisplay'>, _x: Decimal): {
|
|
7
7
|
roundedNumber: Decimal;
|
|
8
8
|
formattedString: string;
|
|
9
9
|
};
|
|
@@ -6,7 +6,8 @@ import { ToRawPrecision } from './ToRawPrecision';
|
|
|
6
6
|
/**
|
|
7
7
|
* https://tc39.es/ecma402/#sec-formatnumberstring
|
|
8
8
|
*/
|
|
9
|
-
export function FormatNumericToString(intlObject,
|
|
9
|
+
export function FormatNumericToString(intlObject, _x) {
|
|
10
|
+
var x = _x;
|
|
10
11
|
var sign;
|
|
11
12
|
// -0
|
|
12
13
|
if (x.isZero() && x.isNegative()) {
|
|
@@ -35,7 +35,7 @@ export function InitializeNumberFormat(nf, locales, opts, _a) {
|
|
|
35
35
|
internalSlots.dataLocale = r.dataLocale;
|
|
36
36
|
internalSlots.numberingSystem = r.nu;
|
|
37
37
|
internalSlots.dataLocaleData = dataLocaleData;
|
|
38
|
-
SetNumberFormatUnitOptions(
|
|
38
|
+
SetNumberFormatUnitOptions(internalSlots, options);
|
|
39
39
|
var style = internalSlots.style;
|
|
40
40
|
var notation = GetOption(options, 'notation', 'string', ['standard', 'scientific', 'engineering', 'compact'], 'standard');
|
|
41
41
|
internalSlots.notation = notation;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
2
|
import { NumberFormatInternal, NumberFormatPart } from '../types/number';
|
|
3
3
|
/**
|
|
4
|
-
* https://tc39.es/ecma402/#sec-
|
|
4
|
+
* https://tc39.es/ecma402/#sec-partitionnumberpattern
|
|
5
5
|
*/
|
|
6
|
-
export declare function PartitionNumberPattern(
|
|
7
|
-
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
|
|
8
|
-
}): NumberFormatPart[];
|
|
6
|
+
export declare function PartitionNumberPattern(internalSlots: NumberFormatInternal, _x: Decimal): NumberFormatPart[];
|
|
@@ -1,87 +1,126 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
2
|
import { invariant } from '../utils';
|
|
3
3
|
import { ComputeExponent } from './ComputeExponent';
|
|
4
4
|
import formatToParts from './format_to_parts';
|
|
5
5
|
import { FormatNumericToString } from './FormatNumericToString';
|
|
6
6
|
/**
|
|
7
|
-
* https://tc39.es/ecma402/#sec-
|
|
7
|
+
* https://tc39.es/ecma402/#sec-partitionnumberpattern
|
|
8
8
|
*/
|
|
9
|
-
export function PartitionNumberPattern(
|
|
10
|
-
var
|
|
11
|
-
var
|
|
12
|
-
|
|
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]].
|
|
13
15
|
var pl = internalSlots.pl, dataLocaleData = internalSlots.dataLocaleData, numberingSystem = internalSlots.numberingSystem;
|
|
16
|
+
// 3. Let symbols be dataLocaleData.[[numbers]].[[symbols]][internalSlots.[[numberingSystem]]].
|
|
14
17
|
var symbols = dataLocaleData.numbers.symbols[numberingSystem] ||
|
|
15
18
|
dataLocaleData.numbers.symbols[dataLocaleData.numbers.nu[0]];
|
|
16
|
-
|
|
19
|
+
// 4. Let exponent be 0.
|
|
17
20
|
var exponent = 0;
|
|
21
|
+
// 5. Let n be ! ToString(x).
|
|
18
22
|
var n;
|
|
23
|
+
// 6. If x is NaN, then
|
|
19
24
|
if (x.isNaN()) {
|
|
25
|
+
// 6.a. Let n be symbols.[[nan]].
|
|
20
26
|
n = symbols.nan;
|
|
21
27
|
}
|
|
22
28
|
else if (!x.isFinite()) {
|
|
29
|
+
// 7. Else if x is a non-finite Number, then
|
|
30
|
+
// 7.a. Let n be symbols.[[infinity]].
|
|
23
31
|
n = symbols.infinity;
|
|
24
32
|
}
|
|
25
33
|
else {
|
|
34
|
+
// 8. Else,
|
|
26
35
|
if (!x.isZero()) {
|
|
36
|
+
// 8.a. If x < 0, let x be -x.
|
|
27
37
|
invariant(x.isFinite(), 'Input must be a mathematical value');
|
|
38
|
+
// 8.b. If internalSlots.[[style]] is "percent", let x be 100 × x.
|
|
28
39
|
if (internalSlots.style == 'percent') {
|
|
29
40
|
x = x.times(100);
|
|
30
41
|
}
|
|
42
|
+
// 8.c. Let exponent be ComputeExponent(numberFormat, x).
|
|
31
43
|
;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
x
|
|
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));
|
|
36
49
|
}
|
|
50
|
+
// 8.e. Let formatNumberResult be FormatNumericToString(internalSlots, x).
|
|
37
51
|
var formatNumberResult = FormatNumericToString(internalSlots, x);
|
|
52
|
+
// 8.f. Let n be formatNumberResult.[[formattedString]].
|
|
38
53
|
n = formatNumberResult.formattedString;
|
|
54
|
+
// 8.g. Let x be formatNumberResult.[[roundedNumber]].
|
|
39
55
|
x = formatNumberResult.roundedNumber;
|
|
40
56
|
}
|
|
41
|
-
//
|
|
42
|
-
// We need to do this before `x` is rounded.
|
|
57
|
+
// 9. Let sign be 0.
|
|
43
58
|
var sign;
|
|
59
|
+
// 10. If x is negative, then
|
|
44
60
|
var signDisplay = internalSlots.signDisplay;
|
|
45
61
|
switch (signDisplay) {
|
|
46
62
|
case 'never':
|
|
63
|
+
// 10.a. If internalSlots.[[signDisplay]] is "never", then
|
|
64
|
+
// 10.a.i. Let sign be 0.
|
|
47
65
|
sign = 0;
|
|
48
66
|
break;
|
|
49
67
|
case 'auto':
|
|
68
|
+
// 10.b. Else if internalSlots.[[signDisplay]] is "auto", then
|
|
50
69
|
if (x.isPositive() || x.isNaN()) {
|
|
70
|
+
// 10.b.i. If x is positive or x is NaN, let sign be 0.
|
|
51
71
|
sign = 0;
|
|
52
72
|
}
|
|
53
73
|
else {
|
|
74
|
+
// 10.b.ii. Else, let sign be -1.
|
|
54
75
|
sign = -1;
|
|
55
76
|
}
|
|
56
77
|
break;
|
|
57
78
|
case 'always':
|
|
79
|
+
// 10.c. Else if internalSlots.[[signDisplay]] is "always", then
|
|
58
80
|
if (x.isPositive() || x.isNaN()) {
|
|
81
|
+
// 10.c.i. If x is positive or x is NaN, let sign be 1.
|
|
59
82
|
sign = 1;
|
|
60
83
|
}
|
|
61
84
|
else {
|
|
85
|
+
// 10.c.ii. Else, let sign be -1.
|
|
62
86
|
sign = -1;
|
|
63
87
|
}
|
|
64
88
|
break;
|
|
65
89
|
case 'exceptZero':
|
|
90
|
+
// 10.d. Else if internalSlots.[[signDisplay]] is "exceptZero", then
|
|
66
91
|
if (x.isZero()) {
|
|
92
|
+
// 10.d.i. If x is 0, let sign be 0.
|
|
67
93
|
sign = 0;
|
|
68
94
|
}
|
|
69
95
|
else if (x.isNegative()) {
|
|
96
|
+
// 10.d.ii. Else if x is negative, let sign be -1.
|
|
70
97
|
sign = -1;
|
|
71
98
|
}
|
|
72
99
|
else {
|
|
100
|
+
// 10.d.iii. Else, let sign be 1.
|
|
73
101
|
sign = 1;
|
|
74
102
|
}
|
|
75
103
|
break;
|
|
76
104
|
default:
|
|
105
|
+
// 10.e. Else,
|
|
77
106
|
invariant(signDisplay === 'negative', 'signDisplay must be "negative"');
|
|
78
107
|
if (x.isNegative() && !x.isZero()) {
|
|
108
|
+
// 10.e.i. If x is negative and x is not 0, let sign be -1.
|
|
79
109
|
sign = -1;
|
|
80
110
|
}
|
|
81
111
|
else {
|
|
112
|
+
// 10.e.ii. Else, let sign be 0.
|
|
82
113
|
sign = 0;
|
|
83
114
|
}
|
|
84
115
|
break;
|
|
85
116
|
}
|
|
86
|
-
|
|
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);
|
|
87
126
|
}
|