-
Notifications
You must be signed in to change notification settings - Fork 446
/
formatToParts-style-long-en.js
68 lines (62 loc) · 2.56 KB
/
formatToParts-style-long-en.js
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
// Copyright (C) 2023 Igalia S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.DurationFormat.prototype.formatToParts
description: Checks basic handling of formatToParts, using long, short,narrow and digital styles.
features: [Intl.DurationFormat]
---*/
// Utils functions
function* zip(a, b) {
for (let i = 0; i < a.length; ++i) {
yield [i, a[i], b[i]];
}
}
function compare(actual, expected, message) {
assert.sameValue(Array.isArray(expected), true, `${message}: expected is Array`);
assert.sameValue(Array.isArray(actual), true, `${message}: actual is Array`);
assert.sameValue(actual.length, expected.length, `${message}: length`);
for (const [i, actualEntry, expectedEntry] of zip(actual, expected)) {
// assertions
assert.sameValue(actualEntry.type, expectedEntry.type, `type for entry ${i}`);
assert.sameValue(actualEntry.value, expectedEntry.value, `value for entry ${i}`);
if (expectedEntry.unit) {
assert.sameValue(actualEntry.unit, expectedEntry.unit, `unit for entry ${i}`);
}
}
}
const duration = {
hours: 7,
minutes: 8,
seconds: 9,
milliseconds: 123,
microseconds: 456,
nanoseconds: 789,
};
const style = "long";
const expected = [
{ type: "integer", value: "7", unit: "hour" },
{ type: "literal", value: " ", unit: "hour" },
{ type: "unit", value: "hours", unit: "hour" },
{ type: "literal", value: ", " },
{ type: "integer", value: "8", unit: "minute" },
{ type: "literal", value: " ", unit: "minute" },
{ type: "unit", value: "minutes", unit: "minute" },
{ type: "literal", value: ", " },
{ type: "integer", value: "9", unit: "second" },
{ type: "literal", value: " ", unit: "second" },
{ type: "unit", value: "seconds", unit: "second" },
{ type: "literal", value: ", " },
{ type: "integer", value: "123", unit: "millisecond" },
{ type: "literal", value: " ", unit: "millisecond" },
{ type: "unit", value: "milliseconds", unit: "millisecond" },
{ type: "literal", value: ", " },
{ type: "integer", value: "456", unit: "microsecond" },
{ type: "literal", value: " ", unit: "microsecond" },
{ type: "unit", value: "microseconds", unit: "microsecond" },
{ type: "literal", value: ", " },
{ type: "integer", value: "789", unit: "nanosecond" },
{ type: "literal", value: " ", unit: "nanosecond" },
{ type: "unit", value: "nanoseconds", unit: "nanosecond" },
];
let df = new Intl.DurationFormat('en', { style });
compare(df.formatToParts(duration), expected, `Using style : ${style}`);