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
|
function GenericPartCreator(type) {
return str => ({ type, value: str });
}
const DateTimeFormatParts = {
Weekday: GenericPartCreator("weekday"),
Era: GenericPartCreator("era"),
Year: GenericPartCreator("year"),
YearName: GenericPartCreator("yearName"),
RelatedYear: GenericPartCreator("relatedYear"),
Month: GenericPartCreator("month"),
Day: GenericPartCreator("day"),
DayPeriod: GenericPartCreator("dayPeriod"),
Hour: GenericPartCreator("hour"),
Minute: GenericPartCreator("minute"),
Second: GenericPartCreator("second"),
FractionalSecond: GenericPartCreator("fractionalSecond"),
TimeZoneName: GenericPartCreator("timeZoneName"),
Unknown: GenericPartCreator("unknown"),
Literal: GenericPartCreator("literal"),
};
function assertParts(df, x, expected) {
var parts = df.formatToParts(x);
assertEq(parts.map(part => part.value).join(""), df.format(x),
"formatToParts and format must agree");
var len = parts.length;
assertEq(len, expected.length, "parts count mismatch");
for (var i = 0; i < len; i++) {
assertEq(parts[i].type, expected[i].type, "type mismatch at " + i);
assertEq(parts[i].value, expected[i].value, "value mismatch at " + i);
}
// Formatted parts must be consistent with the resolved options.
var resolvedOptions = df.resolvedOptions();
assertEq("dateStyle" in resolvedOptions, false, "dateStyle isn't yet supported here");
assertEq("timeStyle" in resolvedOptions, false, "timeStyle isn't yet supported here");
// Every formatted part must be in the resolved options.
for (var {type} of expected) {
switch (type) {
case "weekday":
case "era":
case "month":
case "day":
case "hour":
case "minute":
case "second":
case "timeZoneName":
assertEq(type in resolvedOptions, true, JSON.stringify(resolvedOptions));
break;
case "year":
case "yearName":
case "relatedYear":
assertEq("year" in resolvedOptions, true);
break;
case "dayPeriod":
assertEq("dayPeriod" in resolvedOptions || resolvedOptions.hour12 === true, true);
break;
case "fractionalSecond":
assertEq("fractionalSecondDigits" in resolvedOptions, true);
break;
case "unknown":
case "literal":
break;
default:
assertEq(true, false, `invalid part: ${type}`);
}
}
function includesType(...types) {
return parts.some(({type}) => types.includes(type));
}
// Every resolved option must be in the formatted parts.
for (var key of Object.keys(resolvedOptions)) {
switch (key) {
case "locale":
case "calendar":
case "numberingSystem":
case "timeZone":
case "hourCycle":
// Skip over non-pattern keys.
break;
case "hour12":
if (resolvedOptions.hour12) {
assertEq(includesType("dayPeriod"), true);
}
break;
case "weekday":
case "era":
case "month":
case "day":
case "dayPeriod":
case "hour":
case "minute":
case "second":
case "timeZoneName":
assertEq(includesType(key), true);
break;
case "year":
assertEq(includesType("year", "yearName", "relatedYear"), true);
break;
case "fractionalSecondDigits":
assertEq(includesType("fractionalSecond"), true);
break;
default:
assertEq(true, false, `invalid key: ${key}`);
}
}
}
function assertRangeParts(df, start, end, expected) {
var parts = df.formatRangeToParts(start, end);
assertEq(parts.map(part => part.value).join(""), df.formatRange(start, end),
"formatRangeToParts and formatRange must agree");
var len = parts.length;
assertEq(len, expected.length, "parts count mismatch");
for (var i = 0; i < len; i++) {
assertEq(parts[i].type, expected[i].type, "type mismatch at " + i);
assertEq(parts[i].value, expected[i].value, "value mismatch at " + i);
}
}
|