| 1 |
function GenericPartCreator(type) { |
| 2 |
return str => ({ type, value: str }); |
| 3 |
} |
| 4 |
|
| 5 |
const NumberFormatParts = { |
| 6 |
Nan: GenericPartCreator("nan"), |
| 7 |
Inf: GenericPartCreator("infinity"), |
| 8 |
Integer: GenericPartCreator("integer"), |
| 9 |
Group: GenericPartCreator("group"), |
| 10 |
Decimal: GenericPartCreator("decimal"), |
| 11 |
Fraction: GenericPartCreator("fraction"), |
| 12 |
MinusSign: GenericPartCreator("minusSign"), |
| 13 |
PlusSign: GenericPartCreator("plusSign"), |
| 14 |
PercentSign: GenericPartCreator("percentSign"), |
| 15 |
Currency: GenericPartCreator("currency"), |
| 16 |
Literal: GenericPartCreator("literal"), |
| 17 |
ExponentSeparator: GenericPartCreator("exponentSeparator"), |
| 18 |
ExponentMinusSign: GenericPartCreator("exponentMinusSign"), |
| 19 |
ExponentInteger: GenericPartCreator("exponentInteger"), |
| 20 |
Compact: GenericPartCreator("compact"), |
| 21 |
Unit: GenericPartCreator("unit"), |
| 22 |
}; |
| 23 |
|
| 24 |
function NumberRangeFormatParts(source) { |
| 25 |
let entries = Object.entries(NumberFormatParts) |
| 26 |
|
| 27 |
entries.push(["Approx", GenericPartCreator("approximatelySign")]); |
| 28 |
|
| 29 |
return Object.fromEntries(entries.map(([key, part]) => { |
| 30 |
let partWithSource = str => { |
| 31 |
return Object.defineProperty(part(str), "source", { |
| 32 |
value: source, writable: true, enumerable: true, configurable: true |
| 33 |
}); |
| 34 |
}; |
| 35 |
return [key, partWithSource]; |
| 36 |
})); |
| 37 |
} |
| 38 |
|
| 39 |
function assertParts(nf, x, expected) { |
| 40 |
var parts = nf.formatToParts(x); |
| 41 |
assertEq(parts.map(part => part.value).join(""), nf.format(x), |
| 42 |
"formatToParts and format must agree"); |
| 43 |
|
| 44 |
var len = parts.length; |
| 45 |
assertEq(len, expected.length, "parts count mismatch"); |
| 46 |
for (var i = 0; i < len; i++) { |
| 47 |
assertEq(parts[i].type, expected[i].type, "type mismatch at " + i); |
| 48 |
assertEq(parts[i].value, expected[i].value, "value mismatch at " + i); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function assertRangeParts(nf, start, end, expected) { |
| 53 |
var parts = nf.formatRangeToParts(start, end); |
| 54 |
assertEq(parts.map(part => part.value).join(""), nf.formatRange(start, end), |
| 55 |
"formatRangeToParts and formatRange must agree"); |
| 56 |
|
| 57 |
var len = parts.length; |
| 58 |
assertEq(len, expected.length, "parts count mismatch"); |
| 59 |
for (var i = 0; i < len; i++) { |
| 60 |
assertEq(parts[i].type, expected[i].type, "type mismatch at " + i); |
| 61 |
assertEq(parts[i].value, expected[i].value, "value mismatch at " + i); |
| 62 |
assertEq(parts[i].source, expected[i].source, "source mismatch at " + i); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
function runNumberFormattingTestcases(testcases) { |
| 67 |
for (let {locale, options, values} of testcases) { |
| 68 |
let nf = new Intl.NumberFormat(locale, options); |
| 69 |
|
| 70 |
for (let {value, string, parts} of values) { |
| 71 |
assertEq(nf.format(value), string, |
| 72 |
`locale=${locale}, options=${JSON.stringify(options)}, value=${value}`); |
| 73 |
|
| 74 |
assertParts(nf, value, parts); |
| 75 |
} |
| 76 |
} |
| 77 |
} |