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