summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/resolved-locale-sorted-unicode-extension-keys.js
blob: 400f7f0f630145583b0e171d25a2d899de204181 (plain)
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
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

function IsIntlService(c) {
    return typeof c === "function" &&
           c.hasOwnProperty("prototype") &&
           c.prototype.hasOwnProperty("resolvedOptions");
}

const IntlServices = Object.getOwnPropertyNames(Intl).map(name => Intl[name]).filter(IsIntlService);

// Examples for all possible Unicode extension keys (with the exception of deprecated "vt").
const unicodeExtensions = [
    // calendar
    "ca-gregory",
    "fw-mon",
    "hc-h23",

    // collation
    "co-phonebk",
    "ka-noignore",
    "kb-false",
    "kc-false",
    "kf-false",
    "kh-false",
    "kk-false",
    "kn-false",
    "kr-space",
    "ks-level1",
    "kv-space",

    // currency
    "cf-standard",
    "cu-eur",

    // measure
    "ms-metric",

    // number
    "nu-latn",

    // segmentation
    "lb-strict",
    "lw-normal",
    "ss-none",

    // timezone
    "tz-atvie",

    // variant
    "em-default",
    "rg-atzzzz",
    "sd-atat1",
    "va-posix",
];

function reverse(a, b) {
    if (a < b) {
        return 1;
    }
    if (a > b) {
        return -1;
    }
    return 0;
}

function findUnicodeExtensionKeys(locale) {
    // Find the Unicode extension key subtag.
    var extension = locale.match(/.*-u-(.*)/);
    if (extension === null) {
        return [];
    }

    // Replace all types in the Unicode extension keywords.
    return extension[1].replace(/-\w{3,}/g, "").split("-");
}

// Test all Intl service constructors and ensure the Unicode extension keys in
// the resolved locale are sorted alphabetically.

for (let IntlService of IntlServices) {
    let options = undefined;
    if (IntlService === Intl.DisplayNames) {
        // Intl.DisplayNames requires the "type" option to be set.
        options = {type: "language"};
    }

    // sort() modifies the input array, so create a copy.
    let ext = unicodeExtensions.slice(0);

    let locale, keys;

    // Input keys unsorted.
    locale = new IntlService(`de-u-${ext.join("-")}`, options).resolvedOptions().locale;
    keys = findUnicodeExtensionKeys(locale);
    assertEqArray(keys, keys.slice(0).sort());

    // Input keys sorted alphabetically.
    locale = new IntlService(`de-u-${ext.sort().join("-")}`, options).resolvedOptions().locale;
    keys = findUnicodeExtensionKeys(locale);
    assertEqArray(keys, keys.slice(0).sort());

    // Input keys sorted alphabetically in reverse order.
    locale = new IntlService(`de-u-${ext.sort(reverse).join("-")}`, options).resolvedOptions().locale;
    keys = findUnicodeExtensionKeys(locale);
    assertEqArray(keys, keys.slice(0).sort());
}

if (typeof reportCompare === "function")
    reportCompare(0, 0);