forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberFormat.js
More file actions
288 lines (264 loc) · 14.5 KB
/
NumberFormat.js
File metadata and controls
288 lines (264 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
WScript.LoadScriptFile("..\\UnitTestFramework\\UnitTestFramework.js");
let suppressFormatEqualityCheck = false;
function format() {
let locale = "en-US", options, n;
assert.isTrue(arguments.length > 0);
if (typeof arguments[0] === "number") {
[n] = arguments;
} else if (typeof arguments[0] === "object" && !(arguments[0] instanceof Array)) {
[options, n] = arguments;
} else {
[locale, options, n] = arguments;
}
const nf = new Intl.NumberFormat(locale, options);
const format = nf.format(n);
const localeString = n.toLocaleString(locale, options);
assert.isTrue(format === localeString, `[locale = ${JSON.stringify(locale)}, options = ${JSON.stringify(options)}] format does not match toLocaleString`);
if (WScript.Platform.INTL_LIBRARY === "icu" && !suppressFormatEqualityCheck) {
assert.isTrue(format === nf.formatToParts(n).map((part) => part.value).join(""), `[locale = ${JSON.stringify(locale)}, options = ${JSON.stringify(options)}] format does not match formatToParts`);
}
return format;
}
const tests = [
{
name: "Decimal style default options",
body: function () {
assert.areEqual("5", format(5));
assert.areEqual("5,000", format(5000));
assert.areEqual("50.474", format(50.474));
}
},
{
name: "Min/max fractional digits",
body: function () {
// min
assert.areEqual("5.00", format({ minimumFractionDigits: 2 }, 5));
assert.areEqual("5.0", format({ minimumFractionDigits: 1 }, 5));
// min and max
assert.areEqual("5.00", format({ minimumFractionDigits: 2, maximumFractionDigits: 2 }, 5));
assert.areEqual("5.0", format({ minimumFractionDigits: 1, maximumFractionDigits: 2 }, 5));
// max
assert.areEqual("5.44", format({ maximumFractionDigits: 2 }, 5.444));
assert.areEqual("5.444", format({ maximumFractionDigits: 4 }, 5.444));
assert.areEqual("5.45", format({ maximumFractionDigits: 2 }, 5.445));
assert.areEqual("5.445", format({ maximumFractionDigits: 4 }, 5.445));
assert.areEqual("5.55", format({ maximumFractionDigits: 2 }, 5.554));
assert.areEqual("5.554", format({ maximumFractionDigits: 4 }, 5.554));
assert.areEqual("5", format({ maximumFractionDigits: 0 }, 5.45));
assert.areEqual("6", format({ maximumFractionDigits: 0 }, 5.5));
}
},
{
name: "Min integer digits",
body: function () {
assert.areEqual("5", format({ minimumIntegerDigits: 1 }, 5));
assert.areEqual("05", format({ minimumIntegerDigits: 2 }, 5));
assert.areEqual("0,000,000,005", format({ minimumIntegerDigits: 10 }, 5));
assert.areEqual("500", format({ minimumIntegerDigits: 1 }, 500));
assert.areEqual("0,000,000,500", format({ minimumIntegerDigits: 10 }, 500));
}
},
{
name: "Min/max significant digits",
body: function () {
// min
assert.areEqual("5.0", format({ minimumSignificantDigits: 2 }, 5));
assert.areEqual("500", format({ minimumSignificantDigits: 2 }, 500));
assert.areEqual("500.0", format({ minimumSignificantDigits: 4 }, 500));
// min and max
assert.areEqual("5.0", format({ minimumSignificantDigits: 2, maximumSignificantDigits: 2 }, 5));
assert.areEqual("5", format({ minimumSignificantDigits: 1, maximumSignificantDigits: 2 }, 5));
// max
assert.areEqual("5.44", format({ maximumSignificantDigits: 3 }, 5.444));
assert.areEqual("5.444", format({ maximumSignificantDigits: 4 }, 5.4444));
assert.areEqual("5.45", format({ maximumSignificantDigits: 3 }, 5.445));
assert.areEqual("5.445", format({ maximumSignificantDigits: 4 }, 5.4445));
assert.areEqual("5.55", format({ maximumSignificantDigits: 3 }, 5.554));
}
},
{
name: "Grouping separator",
body: function () {
assert.areEqual("50,000", format({ useGrouping: true }, 50000));
assert.areEqual("50000", format({ useGrouping: false }, 50000));
assert.areEqual("0000000005", format({ minimumIntegerDigits: 10, useGrouping: false }, 5));
assert.areEqual("0000005000", format({ minimumIntegerDigits: 10, useGrouping: false }, 5000));
}
},
{
name: "Default style option combinations",
body: function () {
assert.areEqual("123", format({ minimumSignificantDigits: 3, maximumSignificantDigits: 3, minimumIntegerDigits: 5, minimumFractionDigits: 5, maximumFractionDigits: 5 }, 123.1));
assert.areEqual("00,123.10000", format({ minimumIntegerDigits: 5, minimumFractionDigits: 5, maximumFractionDigits: 5 }, 123.1))
}
},
{
name: "Currency style",
body: function () {
function formatCurrency() {
let locale = "en-US", currency = "USD", options, n;
assert.isTrue(arguments.length > 0);
if (typeof arguments[0] === "number") {
[n] = arguments;
} else if (typeof arguments[0] === "object") {
[options, n] = arguments;
} else if (arguments.length === 3) {
[currency, options, n] = arguments;
} else {
[locale, currency, options, n] = arguments;
}
options = options || {};
options.style = "currency",
options.currency = currency;
return format(locale, options, n)
}
assert.areEqual("$1.00", formatCurrency(1));
assert.areEqual("$1.50", formatCurrency(1.50));
assert.areEqual("$1.50", formatCurrency(1.504));
assert.areEqual("$1.51", formatCurrency(1.505));
assert.matches(/USD[\x20\u00a0]?1.00/, formatCurrency({ currencyDisplay: "code" }, 1), "Currency display: code");
assert.matches(/USD[\x20\u00a0]?1.50/, formatCurrency({ currencyDisplay: "code" }, 1.504), "Currency display: code");
assert.matches(/USD[\x20\u00a0]?1.51/, formatCurrency({ currencyDisplay: "code" }, 1.505), "Currency display: code");
assert.areEqual("$1.00", formatCurrency({ currencyDisplay: "symbol" }, 1), "Currency display: symbol");
assert.areEqual("$1.50", formatCurrency({ currencyDisplay: "symbol" }, 1.504), "Currency display: symbol");
assert.areEqual("$1.51", formatCurrency({ currencyDisplay: "symbol" }, 1.505), "Currency display: symbol");
// ICU has a proper "name" currency display, while WinGlob falls back to "code"
if (WScript.Platform.ICU_VERSION >= 62) {
// In ICU 62, there is a mismatch between "1.00 US dollar" and "1.00 US dollars"
suppressFormatEqualityCheck = true;
}
assert.matches(/(?:USD[\x20\u00a0]?1.00|1.00 US dollars)/, formatCurrency({ currencyDisplay: "name" }, 1), "Currency display: name");
suppressFormatEqualityCheck = false;
assert.matches(/(?:USD[\x20\u00a0]?1.50|1.50 US dollars)/, formatCurrency({ currencyDisplay: "name" }, 1.504), "Currency display: name");
assert.matches(/(?:USD[\x20\u00a0]?1.51|1.51 US dollars)/, formatCurrency({ currencyDisplay: "name" }, 1.505), "Currency display: name");
}
},
{
name: "Percent style",
body: function () {
assert.matches(/100[\x20\u00a0]?%/, format({ style: "percent" }, 1));
assert.matches(/1[\x20\u00a0]?%/, format({ style: "percent" }, 0.01));
assert.matches(/10,000[\x20\u00a0]?%/,format({ style: "percent" }, 100));
}
},
{
name: "Negative 0 (https://github.com/tc39/ecma402/issues/219)",
body() {
assert.areEqual(
0,
new Intl.NumberFormat(undefined, { minimumFractionDigits: -0 }).resolvedOptions().minimumFractionDigits,
"Passing -0 for minimumFractionDigits should get normalized to 0 by DefaultNumberOption"
);
assert.areEqual("-0", (-0).toLocaleString(), "-0 should be treated as a negative number (toLocaleString)");
assert.areEqual("-0", new Intl.NumberFormat().format(-0), "-0 should be treated as a negative number (NumberFormat.prototype.format)");
if (WScript.Platform.INTL_LIBRARY === "icu") {
assert.areEqual("-0", new Intl.NumberFormat().formatToParts(-0).map(v => v.value).join(""), "-0 should be treated as a negative number (NumberFormat.prototype.formatToParts)");
}
}
},
{
name: "formatToParts",
body() {
if (WScript.Platform.INTL_LIBRARY === "winglob") {
return;
}
function assertParts(locale, options, n, expectedParts) {
const nf = new Intl.NumberFormat(locale, options);
const resolved = nf.resolvedOptions();
assert.areEqual(locale, resolved.locale, `This test requires ${locale} support`);
if (options) {
for (const opt of Object.getOwnPropertyNames(options)) {
assert.areEqual(options[opt], resolved[opt], `Bad option resolution for option ${opt}`)
}
}
const actualParts = nf.formatToParts(n);
assert.isTrue(Array.isArray(actualParts), `formatToParts(${n}) did not return an array`);
if (WScript.Platform.ICU_VERSION < 61) {
// real formatToParts support was only added with ICU 61
assert.areEqual(1, actualParts.length, `formatToParts(${n}) stub implementation should return only one part`);
const literal = actualParts[0];
assert.areEqual("unknown", literal.type, `formatToParts(${n}) stub implementation should return an unknown part`);
assert.areEqual(nf.format(n), literal.value, `formatToParts(${n}) stub implementation should return one part whose value matches the fully formatted number`);
return;
}
assert.areEqual(expectedParts.length, actualParts.length, `formatToParts(${n}) returned wrong number of parts (actual: ${JSON.stringify(actualParts, null, 2)})`);
expectedParts.forEach((part, i) => {
assert.areEqual(expectedParts[i].type, actualParts[i].type, `formatToParts(${n}) returned bad type for part ${i}`);
assert.areEqual(expectedParts[i].value, actualParts[i].value, `formatToParts(${n}) returned bad value for part ${i} (code points: ${actualParts[i].value.split("").map(char => char.charCodeAt(0)).toString()})`);
})
}
assertParts("en-US", undefined, 1000, [
{ type: "integer" , value: "1" },
{ type: "group", value: "," },
{ type: "integer", value: "000" }
]);
assertParts("en-US", undefined, -1000, [
{ type: "minusSign", value: "-" },
{ type: "integer" , value: "1" },
{ type: "group", value: "," },
{ type: "integer", value: "000" }
]);
if (WScript.Platform.ICU_VERSION !== 62) {
assertParts("en-US", undefined, NaN, [{ type: "nan", value: "NaN" }]);
}
assertParts("en-US", undefined, Infinity, [{ type: "infinity", value: "∞" }]);
assertParts("en-US", undefined, 1000.3423, [
{ type: "integer", value: "1" },
{ type: "group", value: "," },
{ type: "integer", value: "000" },
{ type: "decimal", value: "." },
{ type: "fraction", value: "342" }
]);
assertParts("en-US", { minimumFractionDigits: 5 }, 1000.3423, [
{ type: "integer", value: "1" },
{ type: "group", value: "," },
{ type: "integer", value: "000" },
{ type: "decimal", value: "." },
{ type: "fraction", value: "34230" }
]);
assertParts("en-US", { style: "currency", currency: "CAD", currencyDisplay: "name" }, 1000.3423, [
{ type: "integer", value: "1" },
{ type: "group", value: "," },
{ type: "integer", value: "000" },
{ type: "decimal", value: "." },
{ type: "fraction", value: "34" },
{ type: "literal", value: " " },
{ type: "currency", value: "Canadian dollars" }
]);
assertParts("en-US", { style: "percent", minimumSignificantDigits: 4 }, 0.3423, [
{ type: "integer", value: "34" },
{ type: "decimal", value: "." },
{ type: "fraction", value: "23" },
{ type: "percent", value: "%" }
]);
assertParts("de-DE", { minimumFractionDigits: 5 }, 1000.3423, [
{ type: "integer", value: "1" },
{ type: "group", value: "." },
{ type: "integer", value: "000" },
{ type: "decimal", value: "," },
{ type: "fraction", value: "34230" }
]);
assertParts("de-DE", { style: "currency", currency: "CAD", currencyDisplay: "name" }, 1000.3423, [
{ type: "integer", value: "1" },
{ type: "group", value: "." },
{ type: "integer", value: "000" },
{ type: "decimal", value: "," },
{ type: "fraction", value: "34" },
{ type: "literal", value: " " },
{ type: "currency", value: "Kanadische Dollar" }
]);
assertParts("de-DE", { style: "percent", minimumSignificantDigits: 4 }, 0.3423, [
{ type: "integer", value: "34" },
{ type: "decimal", value: "," },
{ type: "fraction", value: "23" },
{ type: "literal", value: "\u00A0" }, // non-breaking space
{ type: "percent", value: "%" }
]);
}
}
];
testRunner.runTests(tests, { verbose: false });