@formatjs/ecma402-abstract 2.2.5 → 2.3.1
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 +5 -4
- package/262.js +13 -17
- package/CanonicalizeLocaleList.d.ts +1 -1
- package/IsSanctionedSimpleUnitIdentifier.d.ts +1 -1
- package/NumberFormat/ApplyUnsignedRoundingMode.d.ts +2 -1
- package/NumberFormat/ApplyUnsignedRoundingMode.js +11 -16
- package/NumberFormat/ComputeExponent.d.ts +2 -1
- package/NumberFormat/ComputeExponent.js +13 -13
- package/NumberFormat/ComputeExponentForMagnitude.d.ts +2 -1
- package/NumberFormat/ComputeExponentForMagnitude.js +12 -3
- package/NumberFormat/FormatNumericRange.d.ts +2 -1
- package/NumberFormat/FormatNumericRangeToParts.d.ts +2 -1
- package/NumberFormat/FormatNumericToParts.d.ts +2 -1
- package/NumberFormat/FormatNumericToParts.js +1 -1
- package/NumberFormat/FormatNumericToString.d.ts +3 -2
- package/NumberFormat/FormatNumericToString.js +56 -14
- package/NumberFormat/InitializeNumberFormat.js +7 -36
- package/NumberFormat/PartitionNumberPattern.d.ts +4 -3
- package/NumberFormat/PartitionNumberPattern.js +25 -18
- package/NumberFormat/PartitionNumberRangePattern.d.ts +2 -1
- package/NumberFormat/PartitionNumberRangePattern.js +2 -3
- package/NumberFormat/SetNumberFormatDigitOptions.js +53 -24
- package/NumberFormat/ToRawFixed.d.ts +3 -2
- package/NumberFormat/ToRawFixed.js +55 -18
- package/NumberFormat/ToRawPrecision.d.ts +3 -2
- package/NumberFormat/ToRawPrecision.js +75 -42
- package/NumberFormat/format_to_parts.d.ts +5 -2
- package/NumberFormat/format_to_parts.js +35 -20
- package/constants.d.ts +4 -0
- package/constants.js +8 -0
- package/index.d.ts +2 -1
- package/index.js +3 -2
- package/lib/262.d.ts +5 -4
- package/lib/262.js +13 -17
- package/lib/CanonicalizeLocaleList.d.ts +1 -1
- package/lib/IsSanctionedSimpleUnitIdentifier.d.ts +1 -1
- package/lib/NumberFormat/ApplyUnsignedRoundingMode.d.ts +2 -1
- package/lib/NumberFormat/ApplyUnsignedRoundingMode.js +11 -16
- package/lib/NumberFormat/ComputeExponent.d.ts +2 -1
- package/lib/NumberFormat/ComputeExponent.js +13 -13
- package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts +2 -1
- package/lib/NumberFormat/ComputeExponentForMagnitude.js +11 -3
- package/lib/NumberFormat/FormatNumericRange.d.ts +2 -1
- package/lib/NumberFormat/FormatNumericRangeToParts.d.ts +2 -1
- package/lib/NumberFormat/FormatNumericToParts.d.ts +2 -1
- package/lib/NumberFormat/FormatNumericToParts.js +1 -1
- package/lib/NumberFormat/FormatNumericToString.d.ts +3 -2
- package/lib/NumberFormat/FormatNumericToString.js +57 -15
- package/lib/NumberFormat/InitializeNumberFormat.js +7 -36
- package/lib/NumberFormat/PartitionNumberPattern.d.ts +4 -3
- package/lib/NumberFormat/PartitionNumberPattern.js +25 -18
- package/lib/NumberFormat/PartitionNumberRangePattern.d.ts +2 -1
- package/lib/NumberFormat/PartitionNumberRangePattern.js +2 -3
- package/lib/NumberFormat/SetNumberFormatDigitOptions.js +53 -24
- package/lib/NumberFormat/ToRawFixed.d.ts +3 -2
- package/lib/NumberFormat/ToRawFixed.js +54 -18
- package/lib/NumberFormat/ToRawPrecision.d.ts +3 -2
- package/lib/NumberFormat/ToRawPrecision.js +75 -43
- package/lib/NumberFormat/format_to_parts.d.ts +5 -2
- package/lib/NumberFormat/format_to_parts.js +34 -20
- package/lib/constants.d.ts +4 -0
- package/lib/constants.js +4 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/types/number.d.ts +16 -9
- package/lib/utils.d.ts +0 -5
- package/lib/utils.js +0 -9
- package/package.json +4 -3
- package/types/number.d.ts +16 -9
- package/utils.d.ts +0 -5
- package/utils.js +0 -10
|
@@ -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
|
|
@@ -12,6 +16,7 @@ var CARET_S_UNICODE_REGEX = new RegExp("^".concat(regex_generated_1.S_UNICODE_RE
|
|
|
12
16
|
var S_DOLLAR_UNICODE_REGEX = new RegExp("".concat(regex_generated_1.S_UNICODE_REGEX.source, "$"));
|
|
13
17
|
var CLDR_NUMBER_PATTERN = /[#0](?:[\.,][#0]+)*/g;
|
|
14
18
|
function formatToParts(numberResult, data, pl, options) {
|
|
19
|
+
var _a;
|
|
15
20
|
var sign = numberResult.sign, exponent = numberResult.exponent, magnitude = numberResult.magnitude;
|
|
16
21
|
var notation = options.notation, style = options.style, numberingSystem = options.numberingSystem;
|
|
17
22
|
var defaultNumberingSystem = data.numbers.nu[0];
|
|
@@ -118,9 +123,9 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
118
123
|
switch (part) {
|
|
119
124
|
case '{0}': {
|
|
120
125
|
// We only need to handle scientific and engineering notation here.
|
|
121
|
-
numberParts.push.apply(numberParts,
|
|
126
|
+
numberParts.push.apply(numberParts, partitionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem,
|
|
122
127
|
// If compact number pattern exists, do not insert group separators.
|
|
123
|
-
!compactNumberPattern &&
|
|
128
|
+
!compactNumberPattern && ((_a = options.useGrouping) !== null && _a !== void 0 ? _a : true), decimalNumberPattern, style, options.roundingIncrement, (0, GetUnsignedRoundingMode_1.GetUnsignedRoundingMode)(options.roundingMode, sign === -1)));
|
|
124
129
|
break;
|
|
125
130
|
}
|
|
126
131
|
case '-':
|
|
@@ -163,7 +168,7 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
163
168
|
var unitName = void 0;
|
|
164
169
|
var currencyNameData = data.currencies[options.currency];
|
|
165
170
|
if (currencyNameData) {
|
|
166
|
-
unitName = selectPlural(pl, numberResult.roundedNumber
|
|
171
|
+
unitName = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), currencyNameData.displayName);
|
|
167
172
|
}
|
|
168
173
|
else {
|
|
169
174
|
// Fallback for unknown currency
|
|
@@ -172,8 +177,8 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
172
177
|
// Do {0} and {1} substitution
|
|
173
178
|
var unitPatternParts = unitPattern.split(/(\{[01]\})/g);
|
|
174
179
|
var result = [];
|
|
175
|
-
for (var
|
|
176
|
-
var part = unitPatternParts_1[
|
|
180
|
+
for (var _b = 0, unitPatternParts_1 = unitPatternParts; _b < unitPatternParts_1.length; _b++) {
|
|
181
|
+
var part = unitPatternParts_1[_b];
|
|
177
182
|
switch (part) {
|
|
178
183
|
case '{0}':
|
|
179
184
|
result.push.apply(result, numberParts);
|
|
@@ -200,15 +205,15 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
200
205
|
var unitPattern = void 0;
|
|
201
206
|
if (unitData) {
|
|
202
207
|
// Simple unit pattern
|
|
203
|
-
unitPattern = selectPlural(pl, numberResult.roundedNumber
|
|
208
|
+
unitPattern = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), data.units.simple[unit][unitDisplay]);
|
|
204
209
|
}
|
|
205
210
|
else {
|
|
206
211
|
// See: http://unicode.org/reports/tr35/tr35-general.html#perUnitPatterns
|
|
207
212
|
// If cannot find unit in the simple pattern, it must be "per" compound pattern.
|
|
208
213
|
// Implementation note: we are not following TR-35 here because we need to format to parts!
|
|
209
|
-
var
|
|
214
|
+
var _c = unit.split('-per-'), numeratorUnit = _c[0], denominatorUnit = _c[1];
|
|
210
215
|
unitData = data.units.simple[numeratorUnit];
|
|
211
|
-
var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber
|
|
216
|
+
var numeratorUnitPattern = selectPlural(pl, numberResult.roundedNumber.times(constants_1.TEN.pow(exponent)).toNumber(), data.units.simple[numeratorUnit][unitDisplay]);
|
|
212
217
|
var perUnitPattern = data.units.simple[denominatorUnit].perUnit[unitDisplay];
|
|
213
218
|
if (perUnitPattern) {
|
|
214
219
|
// perUnitPattern exists, combine it with numeratorUnitPattern
|
|
@@ -226,8 +231,8 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
226
231
|
}
|
|
227
232
|
var result = [];
|
|
228
233
|
// We need spacing around "{0}" because they are not treated as "unit" parts, but "literal".
|
|
229
|
-
for (var
|
|
230
|
-
var part = _d
|
|
234
|
+
for (var _d = 0, _e = unitPattern.split(/(\s*\{0\}\s*)/); _d < _e.length; _d++) {
|
|
235
|
+
var part = _e[_d];
|
|
231
236
|
var interpolateMatch = /^(\s*)\{0\}(\s*)$/.exec(part);
|
|
232
237
|
if (interpolateMatch) {
|
|
233
238
|
// Space before "{0}"
|
|
@@ -254,7 +259,7 @@ function formatToParts(numberResult, data, pl, options) {
|
|
|
254
259
|
}
|
|
255
260
|
// A subset of https://tc39.es/ecma402/#sec-partitionnotationsubpattern
|
|
256
261
|
// Plus the exponent parts handling.
|
|
257
|
-
function
|
|
262
|
+
function partitionNumberIntoParts(symbols, numberResult, notation, exponent, numberingSystem, useGrouping,
|
|
258
263
|
/**
|
|
259
264
|
* This is the decimal number pattern without signs or symbols.
|
|
260
265
|
* It is used to infer the group size when `useGrouping` is true.
|
|
@@ -262,14 +267,14 @@ function paritionNumberIntoParts(symbols, numberResult, notation, exponent, numb
|
|
|
262
267
|
* A typical value looks like "#,##0.00" (primary group size is 3).
|
|
263
268
|
* Some locales like Hindi has secondary group size of 2 (e.g. "#,##,##0.00").
|
|
264
269
|
*/
|
|
265
|
-
decimalNumberPattern, style) {
|
|
270
|
+
decimalNumberPattern, style, roundingIncrement, unsignedRoundingMode) {
|
|
266
271
|
var result = [];
|
|
267
272
|
// eslint-disable-next-line prefer-const
|
|
268
273
|
var n = numberResult.formattedString, x = numberResult.roundedNumber;
|
|
269
|
-
if (isNaN(
|
|
274
|
+
if (x.isNaN()) {
|
|
270
275
|
return [{ type: 'nan', value: n }];
|
|
271
276
|
}
|
|
272
|
-
else if (!isFinite(
|
|
277
|
+
else if (!x.isFinite()) {
|
|
273
278
|
return [{ type: 'infinity', value: n }];
|
|
274
279
|
}
|
|
275
280
|
var digitReplacementTable = digit_mapping_generated_1.digitMapping[numberingSystem];
|
|
@@ -294,7 +299,17 @@ decimalNumberPattern, style) {
|
|
|
294
299
|
// unless the rounded number is greater than 10000:
|
|
295
300
|
// NumberFormat('de', {notation: 'compact', compactDisplay: 'short'}).format(1234) //=> "1234"
|
|
296
301
|
// NumberFormat('de').format(1234) //=> "1.234"
|
|
297
|
-
|
|
302
|
+
var shouldUseGrouping = false;
|
|
303
|
+
if (useGrouping === 'always') {
|
|
304
|
+
shouldUseGrouping = true;
|
|
305
|
+
}
|
|
306
|
+
else if (useGrouping === 'min2') {
|
|
307
|
+
shouldUseGrouping = x.greaterThanOrEqualTo(10000);
|
|
308
|
+
}
|
|
309
|
+
else if (useGrouping === 'auto' || useGrouping) {
|
|
310
|
+
shouldUseGrouping = notation !== 'compact' || x.greaterThanOrEqualTo(10000);
|
|
311
|
+
}
|
|
312
|
+
if (shouldUseGrouping) {
|
|
298
313
|
// a. Let groupSepSymbol be the implementation-, locale-, and numbering system-dependent (ILND) String representing the grouping separator.
|
|
299
314
|
// For currency we should use `currencyGroup` instead of generic `group`
|
|
300
315
|
var groupSepSymbol = style === 'currency' && symbols.currencyGroup != null
|
|
@@ -349,13 +364,13 @@ decimalNumberPattern, style) {
|
|
|
349
364
|
result.push({ type: 'decimal', value: decimalSepSymbol }, { type: 'fraction', value: fraction });
|
|
350
365
|
}
|
|
351
366
|
if ((notation === 'scientific' || notation === 'engineering') &&
|
|
352
|
-
isFinite(
|
|
367
|
+
x.isFinite()) {
|
|
353
368
|
result.push({ type: 'exponentSeparator', value: symbols.exponential });
|
|
354
369
|
if (exponent < 0) {
|
|
355
370
|
result.push({ type: 'exponentMinusSign', value: symbols.minusSign });
|
|
356
371
|
exponent = -exponent;
|
|
357
372
|
}
|
|
358
|
-
var exponentResult = (0, ToRawFixed_1.ToRawFixed)(exponent, 0, 0);
|
|
373
|
+
var exponentResult = (0, ToRawFixed_1.ToRawFixed)(new decimal_js_1.default(exponent), 0, 0, roundingIncrement, unsignedRoundingMode);
|
|
359
374
|
result.push({
|
|
360
375
|
type: 'exponentInteger',
|
|
361
376
|
value: exponentResult.formattedString,
|
|
@@ -401,7 +416,7 @@ function getCompactDisplayPattern(numberResult, pl, data, style, compactDisplay,
|
|
|
401
416
|
if (!compactPluralRules) {
|
|
402
417
|
return null;
|
|
403
418
|
}
|
|
404
|
-
pattern = selectPlural(pl, roundedNumber, compactPluralRules);
|
|
419
|
+
pattern = selectPlural(pl, roundedNumber.toNumber(), compactPluralRules);
|
|
405
420
|
}
|
|
406
421
|
else {
|
|
407
422
|
var byNumberingSystem = data.numbers.decimal;
|
|
@@ -411,7 +426,7 @@ function getCompactDisplayPattern(numberResult, pl, data, style, compactDisplay,
|
|
|
411
426
|
if (!compactPlaralRule) {
|
|
412
427
|
return null;
|
|
413
428
|
}
|
|
414
|
-
pattern = selectPlural(pl, roundedNumber, compactPlaralRule);
|
|
429
|
+
pattern = selectPlural(pl, roundedNumber.toNumber(), compactPlaralRule);
|
|
415
430
|
}
|
|
416
431
|
// See https://unicode.org/reports/tr35/tr35-numbers.html#Compact_Number_Formats
|
|
417
432
|
// > If the value is precisely “0”, either explicit or defaulted, then the normal number format
|
package/constants.d.ts
ADDED
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,
|
|
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.
|
|
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):
|
|
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):
|
|
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):
|
|
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 |
|
|
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
|
|
22
|
+
return ZERO;
|
|
21
23
|
}
|
|
22
24
|
if (typeof val === 'boolean') {
|
|
23
|
-
return val ? 1 :
|
|
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(
|
|
40
|
-
return
|
|
38
|
+
if (number.isNaN() || number.isZero()) {
|
|
39
|
+
return ZERO;
|
|
41
40
|
}
|
|
42
|
-
if (isFinite(
|
|
41
|
+
if (number.isFinite()) {
|
|
43
42
|
return number;
|
|
44
43
|
}
|
|
45
|
-
var integer =
|
|
46
|
-
if (number
|
|
47
|
-
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
|
|
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:
|
|
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:
|
|
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
|
|
3
|
+
if (x.eq(r1))
|
|
3
4
|
return r1;
|
|
4
|
-
|
|
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
|
|
14
|
-
var d2 = r2
|
|
15
|
-
if (d1
|
|
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
|
|
17
|
+
if (d2.lessThan(d1)) {
|
|
19
18
|
return r2;
|
|
20
19
|
}
|
|
21
|
-
|
|
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
|
-
|
|
31
|
-
|
|
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:
|
|
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 {
|
|
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
|
|
13
|
+
if (x.isZero()) {
|
|
14
14
|
return [0, 0];
|
|
15
15
|
}
|
|
16
|
-
if (x
|
|
17
|
-
x =
|
|
16
|
+
if (x.isNegative()) {
|
|
17
|
+
x = x.negated();
|
|
18
18
|
}
|
|
19
|
-
var magnitude =
|
|
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 =
|
|
24
|
+
x = x.times(TEN.pow(-exponent));
|
|
25
25
|
var formatNumberResult = FormatNumericToString(getInternalSlots(numberFormat), x);
|
|
26
|
-
if (formatNumberResult.roundedNumber
|
|
27
|
-
return [exponent, magnitude];
|
|
26
|
+
if (formatNumberResult.roundedNumber.isZero()) {
|
|
27
|
+
return [exponent, magnitude.toNumber()];
|
|
28
28
|
}
|
|
29
|
-
var newMagnitude =
|
|
30
|
-
if (newMagnitude
|
|
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
|
|
34
|
+
ComputeExponentForMagnitude(numberFormat, magnitude.plus(1), {
|
|
35
35
|
getInternalSlots: getInternalSlots,
|
|
36
36
|
}),
|
|
37
|
-
magnitude
|
|
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:
|
|
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
|
-
|
|
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 =
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
6
|
-
roundedNumber:
|
|
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
|
};
|