collations-accepted-by-Collator.js (2472B)
1 // Copyright (C) 2021 André Bargull. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-intl.supportedvaluesof 6 description: > 7 The returned "collation" values can be used with Collator. 8 info: | 9 Intl.supportedValuesOf ( key ) 10 11 1. Let key be ? ToString(key). 12 ... 13 3. Else if key is "collation", then 14 a. Let list be ! AvailableCollations( ). 15 ... 16 9. Return ! CreateArrayFromList( list ). 17 18 AvailableCollations ( ) 19 The AvailableCollations abstract operation returns a List, ordered as if an 20 Array of the same values had been sorted using %Array.prototype.sort% using 21 undefined as comparefn, that contains unique collation types identifying the 22 collations for which the implementation provides the functionality of 23 Intl.Collator objects. 24 includes: [testIntl.js] 25 locale: [en, ar, de, es, ko, ln, si, sv, zh] 26 features: [Intl-enumeration, Array.prototype.includes] 27 ---*/ 28 29 const collations = Intl.supportedValuesOf("collation"); 30 31 // Not all locales support all possible collations, so test the minimal set to 32 // cover all supported collations. 33 // 34 // The list of all collations can be derived from 35 // <https://github.com/unicode-org/cldr/blob/master/common/bcp47/collation.xml>. 36 // 37 // Note: "standard" and "search" are explicitly disallowed by Intl.Collator. 38 const locales = [ 39 "en", // ducet, emoji, eor 40 "ar", // compat 41 "de", // phonebk 42 "es", // trad 43 "hi", // direct 44 "ko", // searchjl 45 "ln", // phonetic 46 "si", // dict 47 "sv", // reformed 48 "zh", // big5han, gb2312, pinyin, stroke, unihan, zhuyin 49 ]; 50 51 for (let collation of collations) { 52 let supported = false; 53 for (let locale of locales) { 54 let obj = new Intl.Collator(locale, {collation}); 55 if (obj.resolvedOptions().collation === collation) { 56 supported = true; 57 break; 58 } 59 } 60 61 assert(supported, `${collation} is supported by Collator`); 62 } 63 64 for (let collation of allCollations()) { 65 let supported = false; 66 for (let locale of locales) { 67 let obj = new Intl.Collator(locale, {collation}); 68 if (obj.resolvedOptions().collation === collation) { 69 supported = true; 70 break; 71 } 72 } 73 74 if (supported) { 75 assert(collations.includes(collation), 76 `${collation} supported but not returned by supportedValuesOf`); 77 } else { 78 assert(!collations.includes(collation), 79 `${collation} not supported but returned by supportedValuesOf`); 80 } 81 } 82 83 reportCompare(0, 0);