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
|
// |reftest| skip-if(!this.hasOwnProperty("Intl"))
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Tests the compare function with a diverse set of locales and options.
var input = [
"Argentina",
"Oerlikon",
"Offenbach",
"Sverige",
"Vaticano",
"Zimbabwe",
"la France",
"¡viva España!",
"Österreich",
"中国",
"日本",
"한국",
];
var collator, expected;
function assertEqualArray(actual, expected, collator) {
var description = JSON.stringify(collator.resolvedOptions());
assertEq(actual.length, expected.length, "array length, " + description);
for (var i = 0; i < actual.length; i++) {
assertEq(actual[i], expected[i], "element " + i + ", " + description);
}
}
// Locale en-US; default options.
collator = new Intl.Collator("en-US");
expected = [
"¡viva España!",
"Argentina",
"la France",
"Oerlikon",
"Offenbach",
"Österreich",
"Sverige",
"Vaticano",
"Zimbabwe",
"한국",
"中国",
"日本",
];
assertEqualArray(input.sort(collator.compare), expected, collator);
// Locale sv-SE; default options.
// Swedish treats "Ö" as a separate character, which sorts after "Z".
collator = new Intl.Collator("sv-SE");
expected = [
"¡viva España!",
"Argentina",
"la France",
"Oerlikon",
"Offenbach",
"Sverige",
"Vaticano",
"Zimbabwe",
"Österreich",
"한국",
"中国",
"日本",
];
assertEqualArray(input.sort(collator.compare), expected, collator);
// Locale sv-SE; ignore punctuation.
collator = new Intl.Collator("sv-SE", {ignorePunctuation: true});
expected = [
"Argentina",
"la France",
"Oerlikon",
"Offenbach",
"Sverige",
"Vaticano",
"¡viva España!",
"Zimbabwe",
"Österreich",
"한국",
"中国",
"日本",
];
assertEqualArray(input.sort(collator.compare), expected, collator);
// Locale de-DE; default options.
// In German standard sorting, umlauted characters are treated as variants
// of their base characters: ä ≅ a, ö ≅ o, ü ≅ u.
collator = new Intl.Collator("de-DE");
expected = [
"¡viva España!",
"Argentina",
"la France",
"Oerlikon",
"Offenbach",
"Österreich",
"Sverige",
"Vaticano",
"Zimbabwe",
"한국",
"中国",
"日本",
];
assertEqualArray(input.sort(collator.compare), expected, collator);
// Locale de-DE; phonebook sort order.
// In German phonebook sorting, umlauted characters are expanded to two-vowel
// sequences: ä → ae, ö → oe, ü → ue.
collator = new Intl.Collator("de-DE-u-co-phonebk");
expected = [
"¡viva España!",
"Argentina",
"la France",
"Oerlikon",
"Österreich",
"Offenbach",
"Sverige",
"Vaticano",
"Zimbabwe",
"한국",
"中国",
"日本",
];
assertEqualArray(input.sort(collator.compare), expected, collator);
// Test the .name property of the "compare" getter.
var desc = Object.getOwnPropertyDescriptor(Intl.Collator.prototype, "compare");
assertEq(desc !== undefined, true);
assertEq(typeof desc.get, "function");
assertEq(desc.get.name, "get compare");
reportCompare(0, 0, 'ok');
|