@formatjs/ecma402-abstract 2.2.4 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +20 -15
- 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 +19 -15
- 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
- package/262.js.map +0 -1
- package/CanonicalizeLocaleList.js.map +0 -1
- package/CanonicalizeTimeZoneName.js.map +0 -1
- package/CoerceOptionsToObject.js.map +0 -1
- package/DefaultNumberOption.js.map +0 -1
- package/GetNumberOption.js.map +0 -1
- package/GetOption.js.map +0 -1
- package/GetOptionsObject.js.map +0 -1
- package/GetStringOrBooleanOption.js.map +0 -1
- package/IsSanctionedSimpleUnitIdentifier.js.map +0 -1
- package/IsValidTimeZoneName.js.map +0 -1
- package/IsWellFormedCurrencyCode.js.map +0 -1
- package/IsWellFormedUnitIdentifier.js.map +0 -1
- package/NumberFormat/ApplyUnsignedRoundingMode.js.map +0 -1
- package/NumberFormat/CollapseNumberRange.js.map +0 -1
- package/NumberFormat/ComputeExponent.js.map +0 -1
- package/NumberFormat/ComputeExponentForMagnitude.js.map +0 -1
- package/NumberFormat/CurrencyDigits.js.map +0 -1
- package/NumberFormat/FormatApproximately.js.map +0 -1
- package/NumberFormat/FormatNumericRange.js.map +0 -1
- package/NumberFormat/FormatNumericRangeToParts.js.map +0 -1
- package/NumberFormat/FormatNumericToParts.js.map +0 -1
- package/NumberFormat/FormatNumericToString.js.map +0 -1
- package/NumberFormat/GetUnsignedRoundingMode.js.map +0 -1
- package/NumberFormat/InitializeNumberFormat.js.map +0 -1
- package/NumberFormat/PartitionNumberPattern.js.map +0 -1
- package/NumberFormat/PartitionNumberRangePattern.js.map +0 -1
- package/NumberFormat/SetNumberFormatDigitOptions.js.map +0 -1
- package/NumberFormat/SetNumberFormatUnitOptions.js.map +0 -1
- package/NumberFormat/ToRawFixed.js.map +0 -1
- package/NumberFormat/ToRawPrecision.js.map +0 -1
- package/NumberFormat/digit-mapping.generated.js.map +0 -1
- package/NumberFormat/format_to_parts.js.map +0 -1
- package/PartitionPattern.js.map +0 -1
- package/SupportedLocales.js.map +0 -1
- package/data.js.map +0 -1
- package/index.js.map +0 -1
- package/lib/262.js.map +0 -1
- package/lib/CanonicalizeLocaleList.js.map +0 -1
- package/lib/CanonicalizeTimeZoneName.js.map +0 -1
- package/lib/CoerceOptionsToObject.js.map +0 -1
- package/lib/DefaultNumberOption.js.map +0 -1
- package/lib/GetNumberOption.js.map +0 -1
- package/lib/GetOption.js.map +0 -1
- package/lib/GetOptionsObject.js.map +0 -1
- package/lib/GetStringOrBooleanOption.js.map +0 -1
- package/lib/IsSanctionedSimpleUnitIdentifier.js.map +0 -1
- package/lib/IsValidTimeZoneName.js.map +0 -1
- package/lib/IsWellFormedCurrencyCode.js.map +0 -1
- package/lib/IsWellFormedUnitIdentifier.js.map +0 -1
- package/lib/NumberFormat/ApplyUnsignedRoundingMode.js.map +0 -1
- package/lib/NumberFormat/CollapseNumberRange.js.map +0 -1
- package/lib/NumberFormat/ComputeExponent.js.map +0 -1
- package/lib/NumberFormat/ComputeExponentForMagnitude.js.map +0 -1
- package/lib/NumberFormat/CurrencyDigits.js.map +0 -1
- package/lib/NumberFormat/FormatApproximately.js.map +0 -1
- package/lib/NumberFormat/FormatNumericRange.js.map +0 -1
- package/lib/NumberFormat/FormatNumericRangeToParts.js.map +0 -1
- package/lib/NumberFormat/FormatNumericToParts.js.map +0 -1
- package/lib/NumberFormat/FormatNumericToString.js.map +0 -1
- package/lib/NumberFormat/GetUnsignedRoundingMode.js.map +0 -1
- package/lib/NumberFormat/InitializeNumberFormat.js.map +0 -1
- package/lib/NumberFormat/PartitionNumberPattern.js.map +0 -1
- package/lib/NumberFormat/PartitionNumberRangePattern.js.map +0 -1
- package/lib/NumberFormat/SetNumberFormatDigitOptions.js.map +0 -1
- package/lib/NumberFormat/SetNumberFormatUnitOptions.js.map +0 -1
- package/lib/NumberFormat/ToRawFixed.js.map +0 -1
- package/lib/NumberFormat/ToRawPrecision.js.map +0 -1
- package/lib/NumberFormat/digit-mapping.generated.js.map +0 -1
- package/lib/NumberFormat/format_to_parts.js.map +0 -1
- package/lib/PartitionPattern.js.map +0 -1
- package/lib/SupportedLocales.js.map +0 -1
- package/lib/data.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/regex.generated.js.map +0 -1
- package/lib/types/core.js.map +0 -1
- package/lib/types/date-time.js.map +0 -1
- package/lib/types/displaynames.js.map +0 -1
- package/lib/types/list.js.map +0 -1
- package/lib/types/number.js.map +0 -1
- package/lib/types/plural-rules.js.map +0 -1
- package/lib/types/relative-time.js.map +0 -1
- package/lib/utils.js.map +0 -1
- package/regex.generated.js.map +0 -1
- package/types/core.js.map +0 -1
- package/types/date-time.js.map +0 -1
- package/types/displaynames.js.map +0 -1
- package/types/list.js.map +0 -1
- package/types/number.js.map +0 -1
- package/types/plural-rules.js.map +0 -1
- package/types/relative-time.js.map +0 -1
- package/utils.js.map +0 -1
package/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/262.js
CHANGED
|
@@ -23,6 +23,8 @@ exports.MinFromTime = MinFromTime;
|
|
|
23
23
|
exports.SecFromTime = SecFromTime;
|
|
24
24
|
exports.OrdinaryHasInstance = OrdinaryHasInstance;
|
|
25
25
|
exports.msFromTime = msFromTime;
|
|
26
|
+
var decimal_js_1 = require("decimal.js");
|
|
27
|
+
var constants_1 = require("./constants");
|
|
26
28
|
/**
|
|
27
29
|
* https://tc39.es/ecma262/#sec-tostring
|
|
28
30
|
*/
|
|
@@ -39,21 +41,18 @@ function ToString(o) {
|
|
|
39
41
|
*/
|
|
40
42
|
function ToNumber(val) {
|
|
41
43
|
if (val === undefined) {
|
|
42
|
-
return NaN;
|
|
44
|
+
return new decimal_js_1.Decimal(NaN);
|
|
43
45
|
}
|
|
44
46
|
if (val === null) {
|
|
45
|
-
return
|
|
47
|
+
return constants_1.ZERO;
|
|
46
48
|
}
|
|
47
49
|
if (typeof val === 'boolean') {
|
|
48
|
-
return val ? 1 :
|
|
49
|
-
}
|
|
50
|
-
if (typeof val === 'number') {
|
|
51
|
-
return val;
|
|
50
|
+
return new decimal_js_1.Decimal(val ? 1 : 0);
|
|
52
51
|
}
|
|
53
52
|
if (typeof val === 'symbol' || typeof val === 'bigint') {
|
|
54
53
|
throw new TypeError('Cannot convert symbol/bigint to number');
|
|
55
54
|
}
|
|
56
|
-
return Number(val);
|
|
55
|
+
return new decimal_js_1.Decimal(Number(val));
|
|
57
56
|
}
|
|
58
57
|
/**
|
|
59
58
|
* https://tc39.es/ecma262/#sec-tointeger
|
|
@@ -61,18 +60,15 @@ function ToNumber(val) {
|
|
|
61
60
|
*/
|
|
62
61
|
function ToInteger(n) {
|
|
63
62
|
var number = ToNumber(n);
|
|
64
|
-
if (isNaN(
|
|
65
|
-
return
|
|
63
|
+
if (number.isNaN() || number.isZero()) {
|
|
64
|
+
return constants_1.ZERO;
|
|
66
65
|
}
|
|
67
|
-
if (isFinite(
|
|
66
|
+
if (number.isFinite()) {
|
|
68
67
|
return number;
|
|
69
68
|
}
|
|
70
|
-
var integer =
|
|
71
|
-
if (number
|
|
72
|
-
integer =
|
|
73
|
-
}
|
|
74
|
-
if (SameValue(integer, -0)) {
|
|
75
|
-
return 0;
|
|
69
|
+
var integer = number.abs().floor();
|
|
70
|
+
if (number.isNegative()) {
|
|
71
|
+
integer = integer.negated();
|
|
76
72
|
}
|
|
77
73
|
return integer;
|
|
78
74
|
}
|
|
@@ -87,7 +83,7 @@ function TimeClip(time) {
|
|
|
87
83
|
if (Math.abs(time) > 8.64 * 1e15) {
|
|
88
84
|
return NaN;
|
|
89
85
|
}
|
|
90
|
-
return ToInteger(time);
|
|
86
|
+
return ToInteger(time).toNumber();
|
|
91
87
|
}
|
|
92
88
|
/**
|
|
93
89
|
* 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,40 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ApplyUnsignedRoundingMode = ApplyUnsignedRoundingMode;
|
|
4
|
+
var utils_1 = require("../utils");
|
|
4
5
|
function ApplyUnsignedRoundingMode(x, r1, r2, unsignedRoundingMode) {
|
|
5
|
-
if (x
|
|
6
|
+
if (x.eq(r1))
|
|
6
7
|
return r1;
|
|
7
|
-
|
|
8
|
-
throw new Error('unsignedRoundingMode is mandatory');
|
|
9
|
-
}
|
|
8
|
+
(0, utils_1.invariant)(r1.lessThan(x) && x.lessThan(r2), "x should be between r1 and r2 but x=".concat(x, ", r1=").concat(r1, ", r2=").concat(r2));
|
|
10
9
|
if (unsignedRoundingMode === 'zero') {
|
|
11
10
|
return r1;
|
|
12
11
|
}
|
|
13
12
|
if (unsignedRoundingMode === 'infinity') {
|
|
14
13
|
return r2;
|
|
15
14
|
}
|
|
16
|
-
var d1 = x
|
|
17
|
-
var d2 = r2
|
|
18
|
-
if (d1
|
|
15
|
+
var d1 = x.minus(r1);
|
|
16
|
+
var d2 = r2.minus(x);
|
|
17
|
+
if (d1.lessThan(d2)) {
|
|
19
18
|
return r1;
|
|
20
19
|
}
|
|
21
|
-
if (d2
|
|
20
|
+
if (d2.lessThan(d1)) {
|
|
22
21
|
return r2;
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
throw new Error('Unexpected error');
|
|
26
|
-
}
|
|
23
|
+
(0, utils_1.invariant)(d1.eq(d2), 'd1 should be equal to d2');
|
|
27
24
|
if (unsignedRoundingMode === 'half-zero') {
|
|
28
25
|
return r1;
|
|
29
26
|
}
|
|
30
27
|
if (unsignedRoundingMode === 'half-infinity') {
|
|
31
28
|
return r2;
|
|
32
29
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
var cardinality = (r1 / (r2 - r1)) % 2;
|
|
37
|
-
if (cardinality === 0) {
|
|
30
|
+
(0, utils_1.invariant)(unsignedRoundingMode === 'half-even', 'unsignedRoundingMode should be half-even');
|
|
31
|
+
var cardinality = r1.div(r2.minus(r1)).mod(2);
|
|
32
|
+
if (cardinality.isZero()) {
|
|
38
33
|
return r1;
|
|
39
34
|
}
|
|
40
35
|
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,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ComputeExponent = ComputeExponent;
|
|
4
|
-
var
|
|
4
|
+
var constants_1 = require("../constants");
|
|
5
5
|
var ComputeExponentForMagnitude_1 = require("./ComputeExponentForMagnitude");
|
|
6
6
|
var FormatNumericToString_1 = require("./FormatNumericToString");
|
|
7
7
|
/**
|
|
@@ -13,30 +13,30 @@ var FormatNumericToString_1 = require("./FormatNumericToString");
|
|
|
13
13
|
*/
|
|
14
14
|
function ComputeExponent(numberFormat, x, _a) {
|
|
15
15
|
var getInternalSlots = _a.getInternalSlots;
|
|
16
|
-
if (x
|
|
16
|
+
if (x.isZero()) {
|
|
17
17
|
return [0, 0];
|
|
18
18
|
}
|
|
19
|
-
if (x
|
|
20
|
-
x =
|
|
19
|
+
if (x.isNegative()) {
|
|
20
|
+
x = x.negated();
|
|
21
21
|
}
|
|
22
|
-
var magnitude = (
|
|
22
|
+
var magnitude = x.log(10).floor();
|
|
23
23
|
var exponent = (0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(numberFormat, magnitude, {
|
|
24
24
|
getInternalSlots: getInternalSlots,
|
|
25
25
|
});
|
|
26
26
|
// Preserve more precision by doing multiplication when exponent is negative.
|
|
27
|
-
x =
|
|
27
|
+
x = x.times(constants_1.TEN.pow(-exponent));
|
|
28
28
|
var formatNumberResult = (0, FormatNumericToString_1.FormatNumericToString)(getInternalSlots(numberFormat), x);
|
|
29
|
-
if (formatNumberResult.roundedNumber
|
|
30
|
-
return [exponent, magnitude];
|
|
29
|
+
if (formatNumberResult.roundedNumber.isZero()) {
|
|
30
|
+
return [exponent, magnitude.toNumber()];
|
|
31
31
|
}
|
|
32
|
-
var newMagnitude =
|
|
33
|
-
if (newMagnitude
|
|
34
|
-
return [exponent, magnitude];
|
|
32
|
+
var newMagnitude = formatNumberResult.roundedNumber.log(10).floor();
|
|
33
|
+
if (newMagnitude.eq(magnitude.minus(exponent))) {
|
|
34
|
+
return [exponent, magnitude.toNumber()];
|
|
35
35
|
}
|
|
36
36
|
return [
|
|
37
|
-
(0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(numberFormat, magnitude
|
|
37
|
+
(0, ComputeExponentForMagnitude_1.ComputeExponentForMagnitude)(numberFormat, magnitude.plus(1), {
|
|
38
38
|
getInternalSlots: getInternalSlots,
|
|
39
39
|
}),
|
|
40
|
-
magnitude
|
|
40
|
+
magnitude.plus(1).toNumber(),
|
|
41
41
|
];
|
|
42
42
|
}
|
|
@@ -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,6 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ComputeExponentForMagnitude = ComputeExponentForMagnitude;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var decimal_js_1 = tslib_1.__importDefault(require("decimal.js"));
|
|
6
|
+
var constants_1 = require("../constants");
|
|
7
|
+
var utils_1 = require("../utils");
|
|
8
|
+
decimal_js_1.default.set({
|
|
9
|
+
toExpPos: 100,
|
|
10
|
+
});
|
|
4
11
|
/**
|
|
5
12
|
* The abstract operation ComputeExponentForMagnitude computes an exponent by which to scale a
|
|
6
13
|
* number of the given magnitude (power of ten of the most significant digit) according to the
|
|
@@ -14,10 +21,12 @@ function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
|
|
|
14
21
|
case 'standard':
|
|
15
22
|
return 0;
|
|
16
23
|
case 'scientific':
|
|
17
|
-
return magnitude;
|
|
24
|
+
return magnitude.toNumber();
|
|
18
25
|
case 'engineering':
|
|
19
|
-
|
|
26
|
+
var thousands = magnitude.div(3).floor();
|
|
27
|
+
return thousands.times(3).toNumber();
|
|
20
28
|
default: {
|
|
29
|
+
(0, utils_1.invariant)(notation === 'compact', 'Invalid notation');
|
|
21
30
|
// Let exponent be an implementation- and locale-dependent (ILD) integer by which to scale a
|
|
22
31
|
// number of the given magnitude in compact notation for the current locale.
|
|
23
32
|
var compactDisplay = internalSlots.compactDisplay, style = internalSlots.style, currencyDisplay = internalSlots.currencyDisplay;
|
|
@@ -35,7 +44,7 @@ function ComputeExponentForMagnitude(numberFormat, magnitude, _a) {
|
|
|
35
44
|
if (!thresholdMap) {
|
|
36
45
|
return 0;
|
|
37
46
|
}
|
|
38
|
-
var num =
|
|
47
|
+
var num = constants_1.TEN.pow(magnitude).toString();
|
|
39
48
|
var thresholds = Object.keys(thresholdMap); // TODO: this can be pre-processed
|
|
40
49
|
if (num < thresholds[0]) {
|
|
41
50
|
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,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FormatNumericToParts = FormatNumericToParts;
|
|
4
|
-
var PartitionNumberPattern_1 = require("./PartitionNumberPattern");
|
|
5
4
|
var _262_1 = require("../262");
|
|
5
|
+
var PartitionNumberPattern_1 = require("./PartitionNumberPattern");
|
|
6
6
|
function FormatNumericToParts(nf, x, implDetails) {
|
|
7
7
|
var parts = (0, PartitionNumberPattern_1.PartitionNumberPattern)(nf, x, implDetails);
|
|
8
8
|
var result = (0, _262_1.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
|
};
|
|
@@ -1,44 +1,86 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FormatNumericToString = FormatNumericToString;
|
|
4
|
-
var
|
|
5
|
-
var ToRawPrecision_1 = require("./ToRawPrecision");
|
|
4
|
+
var constants_1 = require("../constants");
|
|
6
5
|
var utils_1 = require("../utils");
|
|
6
|
+
var GetUnsignedRoundingMode_1 = require("./GetUnsignedRoundingMode");
|
|
7
7
|
var ToRawFixed_1 = require("./ToRawFixed");
|
|
8
|
+
var ToRawPrecision_1 = require("./ToRawPrecision");
|
|
8
9
|
/**
|
|
9
10
|
* https://tc39.es/ecma402/#sec-formatnumberstring
|
|
10
11
|
*/
|
|
11
12
|
function FormatNumericToString(intlObject, x) {
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
var sign;
|
|
14
|
+
// -0
|
|
15
|
+
if (x.isZero() && x.isNegative()) {
|
|
16
|
+
sign = 'negative';
|
|
17
|
+
x = constants_1.ZERO;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
(0, utils_1.invariant)(x.isFinite(), 'NumberFormatDigitInternalSlots value is not finite');
|
|
21
|
+
if (x.lessThan(0)) {
|
|
22
|
+
sign = 'negative';
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
sign = 'positive';
|
|
26
|
+
}
|
|
27
|
+
if (sign === 'negative') {
|
|
28
|
+
x = x.negated();
|
|
29
|
+
}
|
|
15
30
|
}
|
|
16
31
|
var result;
|
|
17
|
-
var
|
|
18
|
-
|
|
32
|
+
var roundingType = intlObject.roundingType;
|
|
33
|
+
var unsignedRoundingMode = (0, GetUnsignedRoundingMode_1.GetUnsignedRoundingMode)(intlObject.roundingMode, sign === 'negative');
|
|
34
|
+
switch (roundingType) {
|
|
19
35
|
case 'significantDigits':
|
|
20
|
-
result = (0, ToRawPrecision_1.ToRawPrecision)(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits);
|
|
36
|
+
result = (0, ToRawPrecision_1.ToRawPrecision)(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits, unsignedRoundingMode);
|
|
21
37
|
break;
|
|
22
38
|
case 'fractionDigits':
|
|
23
|
-
result = (0, ToRawFixed_1.ToRawFixed)(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits);
|
|
39
|
+
result = (0, ToRawFixed_1.ToRawFixed)(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits, intlObject.roundingIncrement, unsignedRoundingMode);
|
|
24
40
|
break;
|
|
25
41
|
default:
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
var sResult = (0, ToRawPrecision_1.ToRawPrecision)(x, intlObject.minimumSignificantDigits, intlObject.maximumSignificantDigits, unsignedRoundingMode);
|
|
43
|
+
var fResult = (0, ToRawFixed_1.ToRawFixed)(x, intlObject.minimumFractionDigits, intlObject.maximumFractionDigits, intlObject.roundingIncrement, unsignedRoundingMode);
|
|
44
|
+
if (intlObject.roundingType === 'morePrecision') {
|
|
45
|
+
if (sResult.roundingMagnitude <= fResult.roundingMagnitude) {
|
|
46
|
+
result = sResult;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
result = fResult;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
(0, utils_1.invariant)(intlObject.roundingType === 'lessPrecision', 'Invalid roundingType');
|
|
54
|
+
if (sResult.roundingMagnitude <= fResult.roundingMagnitude) {
|
|
55
|
+
result = fResult;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
result = sResult;
|
|
59
|
+
}
|
|
29
60
|
}
|
|
30
61
|
break;
|
|
31
62
|
}
|
|
32
63
|
x = result.roundedNumber;
|
|
33
64
|
var string = result.formattedString;
|
|
65
|
+
if (intlObject.trailingZeroDisplay === 'stripIfInteger' && x.isInteger()) {
|
|
66
|
+
var i = string.indexOf('.');
|
|
67
|
+
if (i > -1) {
|
|
68
|
+
string = string.slice(0, i);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
34
71
|
var int = result.integerDigitsCount;
|
|
35
72
|
var minInteger = intlObject.minimumIntegerDigits;
|
|
36
73
|
if (int < minInteger) {
|
|
37
74
|
var forwardZeros = (0, utils_1.repeat)('0', minInteger - int);
|
|
38
75
|
string = forwardZeros + string;
|
|
39
76
|
}
|
|
40
|
-
if (
|
|
41
|
-
x
|
|
77
|
+
if (sign === 'negative') {
|
|
78
|
+
if (x.isZero()) {
|
|
79
|
+
x = constants_1.NEGATIVE_ZERO;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
x = x.negated();
|
|
83
|
+
}
|
|
42
84
|
}
|
|
43
85
|
return { roundedNumber: x, formattedString: string };
|
|
44
86
|
}
|
|
@@ -4,22 +4,17 @@ exports.InitializeNumberFormat = InitializeNumberFormat;
|
|
|
4
4
|
var intl_localematcher_1 = require("@formatjs/intl-localematcher");
|
|
5
5
|
var CanonicalizeLocaleList_1 = require("../CanonicalizeLocaleList");
|
|
6
6
|
var CoerceOptionsToObject_1 = require("../CoerceOptionsToObject");
|
|
7
|
-
var GetNumberOption_1 = require("../GetNumberOption");
|
|
8
7
|
var GetOption_1 = require("../GetOption");
|
|
9
8
|
var GetStringOrBooleanOption_1 = require("../GetStringOrBooleanOption");
|
|
10
9
|
var utils_1 = require("../utils");
|
|
11
10
|
var CurrencyDigits_1 = require("./CurrencyDigits");
|
|
12
11
|
var SetNumberFormatDigitOptions_1 = require("./SetNumberFormatDigitOptions");
|
|
13
12
|
var SetNumberFormatUnitOptions_1 = require("./SetNumberFormatUnitOptions");
|
|
14
|
-
var VALID_ROUND_INCREMENT_VALUES = [
|
|
15
|
-
1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000,
|
|
16
|
-
];
|
|
17
13
|
/**
|
|
18
14
|
* https://tc39.es/ecma402/#sec-initializenumberformat
|
|
19
15
|
*/
|
|
20
16
|
function InitializeNumberFormat(nf, locales, opts, _a) {
|
|
21
17
|
var getInternalSlots = _a.getInternalSlots, localeData = _a.localeData, availableLocales = _a.availableLocales, numberingSystemNames = _a.numberingSystemNames, getDefaultLocale = _a.getDefaultLocale, currencyDigitsData = _a.currencyDigitsData;
|
|
22
|
-
// @ts-ignore
|
|
23
18
|
var requestedLocales = (0, CanonicalizeLocaleList_1.CanonicalizeLocaleList)(locales);
|
|
24
19
|
var options = (0, CoerceOptionsToObject_1.CoerceOptionsToObject)(opts);
|
|
25
20
|
var opt = Object.create(null);
|
|
@@ -45,9 +40,11 @@ function InitializeNumberFormat(nf, locales, opts, _a) {
|
|
|
45
40
|
internalSlots.dataLocaleData = dataLocaleData;
|
|
46
41
|
(0, SetNumberFormatUnitOptions_1.SetNumberFormatUnitOptions)(nf, options, { getInternalSlots: getInternalSlots });
|
|
47
42
|
var style = internalSlots.style;
|
|
43
|
+
var notation = (0, GetOption_1.GetOption)(options, 'notation', 'string', ['standard', 'scientific', 'engineering', 'compact'], 'standard');
|
|
44
|
+
internalSlots.notation = notation;
|
|
48
45
|
var mnfdDefault;
|
|
49
46
|
var mxfdDefault;
|
|
50
|
-
if (style === 'currency') {
|
|
47
|
+
if (style === 'currency' && notation === 'standard') {
|
|
51
48
|
var currency = internalSlots.currency;
|
|
52
49
|
var cDigits = (0, CurrencyDigits_1.CurrencyDigits)(currency, { currencyDigitsData: currencyDigitsData });
|
|
53
50
|
mnfdDefault = cDigits;
|
|
@@ -57,42 +54,16 @@ function InitializeNumberFormat(nf, locales, opts, _a) {
|
|
|
57
54
|
mnfdDefault = 0;
|
|
58
55
|
mxfdDefault = style === 'percent' ? 0 : 3;
|
|
59
56
|
}
|
|
60
|
-
var notation = (0, GetOption_1.GetOption)(options, 'notation', 'string', ['standard', 'scientific', 'engineering', 'compact'], 'standard');
|
|
61
|
-
internalSlots.notation = notation;
|
|
62
57
|
(0, SetNumberFormatDigitOptions_1.SetNumberFormatDigitOptions)(internalSlots, options, mnfdDefault, mxfdDefault, notation);
|
|
63
|
-
var roundingIncrement = (0, GetNumberOption_1.GetNumberOption)(options, 'roundingIncrement', 1, 5000, 1);
|
|
64
|
-
if (VALID_ROUND_INCREMENT_VALUES.indexOf(roundingIncrement) === -1) {
|
|
65
|
-
throw new RangeError("Invalid rounding increment value: ".concat(roundingIncrement, ".\nValid values are ").concat(VALID_ROUND_INCREMENT_VALUES, "."));
|
|
66
|
-
}
|
|
67
|
-
if (roundingIncrement !== 1 &&
|
|
68
|
-
internalSlots.roundingType !== 'fractionDigits') {
|
|
69
|
-
throw new TypeError("For roundingIncrement > 1 only fractionDigits is a valid roundingType");
|
|
70
|
-
}
|
|
71
|
-
if (roundingIncrement !== 1 &&
|
|
72
|
-
internalSlots.maximumFractionDigits !== internalSlots.minimumFractionDigits) {
|
|
73
|
-
throw new RangeError('With roundingIncrement > 1, maximumFractionDigits and minimumFractionDigits must be equal.');
|
|
74
|
-
}
|
|
75
|
-
internalSlots.roundingIncrement = roundingIncrement;
|
|
76
|
-
var trailingZeroDisplay = (0, GetOption_1.GetOption)(options, 'trailingZeroDisplay', 'string', ['auto', 'stripIfInteger'], 'auto');
|
|
77
|
-
internalSlots.trailingZeroDisplay = trailingZeroDisplay;
|
|
78
58
|
var compactDisplay = (0, GetOption_1.GetOption)(options, 'compactDisplay', 'string', ['short', 'long'], 'short');
|
|
79
59
|
var defaultUseGrouping = 'auto';
|
|
80
60
|
if (notation === 'compact') {
|
|
81
61
|
internalSlots.compactDisplay = compactDisplay;
|
|
82
62
|
defaultUseGrouping = 'min2';
|
|
83
63
|
}
|
|
84
|
-
|
|
85
|
-
internalSlots.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
'floor',
|
|
89
|
-
'expand',
|
|
90
|
-
'trunc',
|
|
91
|
-
'halfCeil',
|
|
92
|
-
'halfFloor',
|
|
93
|
-
'halfExpand',
|
|
94
|
-
'halfTrunc',
|
|
95
|
-
'halfEven',
|
|
96
|
-
], 'halfExpand');
|
|
64
|
+
var useGrouping = (0, GetStringOrBooleanOption_1.GetStringOrBooleanOption)(options, 'useGrouping', ['min2', 'auto', 'always'], 'always', false, defaultUseGrouping);
|
|
65
|
+
internalSlots.useGrouping = useGrouping;
|
|
66
|
+
var signDisplay = (0, GetOption_1.GetOption)(options, 'signDisplay', 'string', ['auto', 'never', 'always', 'exceptZero', 'negative'], 'auto');
|
|
67
|
+
internalSlots.signDisplay = signDisplay;
|
|
97
68
|
return nf;
|
|
98
69
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Decimal from 'decimal.js';
|
|
2
|
+
import { NumberFormatInternal, NumberFormatPart } from '../types/number';
|
|
2
3
|
/**
|
|
3
4
|
* https://tc39.es/ecma402/#sec-formatnumberstring
|
|
4
5
|
*/
|
|
5
|
-
export declare function PartitionNumberPattern(numberFormat: Intl.NumberFormat, x:
|
|
6
|
+
export declare function PartitionNumberPattern(numberFormat: Intl.NumberFormat, x: Decimal, { getInternalSlots, }: {
|
|
6
7
|
getInternalSlots(nf: Intl.NumberFormat): NumberFormatInternal;
|
|
7
|
-
}):
|
|
8
|
+
}): NumberFormatPart[];
|