resolved-locale-sorted-unicode-extension-keys.js (2694B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 3 function IsIntlService(c) { 4 return typeof c === "function" && 5 c.hasOwnProperty("prototype") && 6 c.prototype.hasOwnProperty("resolvedOptions"); 7 } 8 9 const IntlServices = Object.getOwnPropertyNames(Intl).map(name => Intl[name]).filter(IsIntlService); 10 11 // Examples for all possible Unicode extension keys (with the exception of deprecated "vt"). 12 const unicodeExtensions = [ 13 // calendar 14 "ca-gregory", 15 "fw-mon", 16 "hc-h23", 17 18 // collation 19 "co-phonebk", 20 "ka-noignore", 21 "kb-false", 22 "kc-false", 23 "kf-false", 24 "kh-false", 25 "kk-false", 26 "kn-false", 27 "kr-space", 28 "ks-level1", 29 "kv-space", 30 31 // currency 32 "cf-standard", 33 "cu-eur", 34 35 // measure 36 "ms-metric", 37 38 // number 39 "nu-latn", 40 41 // segmentation 42 "lb-strict", 43 "lw-normal", 44 "ss-none", 45 46 // timezone 47 "tz-atvie", 48 49 // variant 50 "em-default", 51 "rg-atzzzz", 52 "sd-atat1", 53 "va-posix", 54 ]; 55 56 function reverse(a, b) { 57 if (a < b) { 58 return 1; 59 } 60 if (a > b) { 61 return -1; 62 } 63 return 0; 64 } 65 66 function findUnicodeExtensionKeys(locale) { 67 // Find the Unicode extension key subtag. 68 var extension = locale.match(/.*-u-(.*)/); 69 if (extension === null) { 70 return []; 71 } 72 73 // Replace all types in the Unicode extension keywords. 74 return extension[1].replace(/-\w{3,}/g, "").split("-"); 75 } 76 77 // Test all Intl service constructors and ensure the Unicode extension keys in 78 // the resolved locale are sorted alphabetically. 79 80 for (let IntlService of IntlServices) { 81 let options = undefined; 82 if (IntlService === Intl.DisplayNames) { 83 // Intl.DisplayNames requires the "type" option to be set. 84 options = {type: "language"}; 85 } 86 87 // sort() modifies the input array, so create a copy. 88 let ext = unicodeExtensions.slice(0); 89 90 let locale, keys; 91 92 // Input keys unsorted. 93 locale = new IntlService(`de-u-${ext.join("-")}`, options).resolvedOptions().locale; 94 keys = findUnicodeExtensionKeys(locale); 95 assertEqArray(keys, keys.slice(0).sort()); 96 97 // Input keys sorted alphabetically. 98 locale = new IntlService(`de-u-${ext.sort().join("-")}`, options).resolvedOptions().locale; 99 keys = findUnicodeExtensionKeys(locale); 100 assertEqArray(keys, keys.slice(0).sort()); 101 102 // Input keys sorted alphabetically in reverse order. 103 locale = new IntlService(`de-u-${ext.sort(reverse).join("-")}`, options).resolvedOptions().locale; 104 keys = findUnicodeExtensionKeys(locale); 105 assertEqArray(keys, keys.slice(0).sort()); 106 } 107 108 if (typeof reportCompare === "function") 109 reportCompare(0, 0);