| 1 |
// |reftest| skip-if(!this.hasOwnProperty("Intl")) |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
// Tests the compare function with a diverse set of locales and options. |
| 7 |
|
| 8 |
var input = [ |
| 9 |
"Argentina", |
| 10 |
"Oerlikon", |
| 11 |
"Offenbach", |
| 12 |
"Sverige", |
| 13 |
"Vaticano", |
| 14 |
"Zimbabwe", |
| 15 |
"la France", |
| 16 |
"¡viva España!", |
| 17 |
"Österreich", |
| 18 |
"中国", |
| 19 |
"日本", |
| 20 |
"한국", |
| 21 |
]; |
| 22 |
|
| 23 |
var collator, expected; |
| 24 |
|
| 25 |
function assertEqualArray(actual, expected, collator) { |
| 26 |
var description = JSON.stringify(collator.resolvedOptions()); |
| 27 |
assertEq(actual.length, expected.length, "array length, " + description); |
| 28 |
for (var i = 0; i < actual.length; i++) { |
| 29 |
assertEq(actual[i], expected[i], "element " + i + ", " + description); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
// Locale en-US; default options. |
| 35 |
collator = new Intl.Collator("en-US"); |
| 36 |
expected = [ |
| 37 |
"¡viva España!", |
| 38 |
"Argentina", |
| 39 |
"la France", |
| 40 |
"Oerlikon", |
| 41 |
"Offenbach", |
| 42 |
"Österreich", |
| 43 |
"Sverige", |
| 44 |
"Vaticano", |
| 45 |
"Zimbabwe", |
| 46 |
"한국", |
| 47 |
"中国", |
| 48 |
"日本", |
| 49 |
]; |
| 50 |
assertEqualArray(input.sort(collator.compare), expected, collator); |
| 51 |
|
| 52 |
// Locale sv-SE; default options. |
| 53 |
// Swedish treats "Ö" as a separate character, which sorts after "Z". |
| 54 |
collator = new Intl.Collator("sv-SE"); |
| 55 |
expected = [ |
| 56 |
"¡viva España!", |
| 57 |
"Argentina", |
| 58 |
"la France", |
| 59 |
"Oerlikon", |
| 60 |
"Offenbach", |
| 61 |
"Sverige", |
| 62 |
"Vaticano", |
| 63 |
"Zimbabwe", |
| 64 |
"Österreich", |
| 65 |
"한국", |
| 66 |
"中国", |
| 67 |
"日本", |
| 68 |
]; |
| 69 |
assertEqualArray(input.sort(collator.compare), expected, collator); |
| 70 |
|
| 71 |
// Locale sv-SE; ignore punctuation. |
| 72 |
collator = new Intl.Collator("sv-SE", {ignorePunctuation: true}); |
| 73 |
expected = [ |
| 74 |
"Argentina", |
| 75 |
"la France", |
| 76 |
"Oerlikon", |
| 77 |
"Offenbach", |
| 78 |
"Sverige", |
| 79 |
"Vaticano", |
| 80 |
"¡viva España!", |
| 81 |
"Zimbabwe", |
| 82 |
"Österreich", |
| 83 |
"한국", |
| 84 |
"中国", |
| 85 |
"日本", |
| 86 |
]; |
| 87 |
assertEqualArray(input.sort(collator.compare), expected, collator); |
| 88 |
|
| 89 |
// Locale de-DE; default options. |
| 90 |
// In German standard sorting, umlauted characters are treated as variants |
| 91 |
// of their base characters: ä ≅ a, ö ≅ o, ü ≅ u. |
| 92 |
collator = new Intl.Collator("de-DE"); |
| 93 |
expected = [ |
| 94 |
"¡viva España!", |
| 95 |
"Argentina", |
| 96 |
"la France", |
| 97 |
"Oerlikon", |
| 98 |
"Offenbach", |
| 99 |
"Österreich", |
| 100 |
"Sverige", |
| 101 |
"Vaticano", |
| 102 |
"Zimbabwe", |
| 103 |
"한국", |
| 104 |
"中国", |
| 105 |
"日本", |
| 106 |
]; |
| 107 |
assertEqualArray(input.sort(collator.compare), expected, collator); |
| 108 |
|
| 109 |
// Locale de-DE; phonebook sort order. |
| 110 |
// In German phonebook sorting, umlauted characters are expanded to two-vowel |
| 111 |
// sequences: ä → ae, ö → oe, ü → ue. |
| 112 |
collator = new Intl.Collator("de-DE-u-co-phonebk"); |
| 113 |
expected = [ |
| 114 |
"¡viva España!", |
| 115 |
"Argentina", |
| 116 |
"la France", |
| 117 |
"Oerlikon", |
| 118 |
"Österreich", |
| 119 |
"Offenbach", |
| 120 |
"Sverige", |
| 121 |
"Vaticano", |
| 122 |
"Zimbabwe", |
| 123 |
"한국", |
| 124 |
"中国", |
| 125 |
"日本", |
| 126 |
]; |
| 127 |
assertEqualArray(input.sort(collator.compare), expected, collator); |
| 128 |
|
| 129 |
|
| 130 |
// Test the .name property of the "compare" getter. |
| 131 |
var desc = Object.getOwnPropertyDescriptor(Intl.Collator.prototype, "compare"); |
| 132 |
assertEq(desc !== undefined, true); |
| 133 |
assertEq(typeof desc.get, "function"); |
| 134 |
assertEq(desc.get.name, "get compare"); |
| 135 |
|
| 136 |
|
| 137 |
reportCompare(0, 0, 'ok'); |