| 1 |
// |reftest| skip-if(!this.hasOwnProperty('Intl')) |
| 2 |
|
| 3 |
const defaultLocale = "en"; |
| 4 |
const defaultCalendar = new Intl.DateTimeFormat(defaultLocale).resolvedOptions().calendar; |
| 5 |
|
| 6 |
function createWithLocale(locale, calendar) { |
| 7 |
return new Intl.DateTimeFormat(locale, {calendar}); |
| 8 |
} |
| 9 |
|
| 10 |
function create(calendar) { |
| 11 |
return createWithLocale(defaultLocale, calendar); |
| 12 |
} |
| 13 |
|
| 14 |
// Empty string should throw. |
| 15 |
assertThrowsInstanceOf(() => create(""), RangeError); |
| 16 |
|
| 17 |
// Trailing \0 should throw. |
| 18 |
assertThrowsInstanceOf(() => create("gregory\0"), RangeError); |
| 19 |
|
| 20 |
// Too short or too long strings should throw. |
| 21 |
assertThrowsInstanceOf(() => create("a"), RangeError); |
| 22 |
assertThrowsInstanceOf(() => create("toolongstring"), RangeError); |
| 23 |
|
| 24 |
// Throw even when prefix is valid. |
| 25 |
assertThrowsInstanceOf(() => create("gregory-toolongstring"), RangeError); |
| 26 |
|
| 27 |
// |calendar| can be set to |undefined|. |
| 28 |
let dtf = create(undefined); |
| 29 |
assertEq(dtf.resolvedOptions().calendar, defaultCalendar); |
| 30 |
|
| 31 |
// Unsupported calendars are ignored. |
| 32 |
dtf = create("xxxxxxxx"); |
| 33 |
assertEq(dtf.resolvedOptions().calendar, defaultCalendar); |
| 34 |
|
| 35 |
// Calendars in options overwrite Unicode extension keyword. |
| 36 |
dtf = createWithLocale(`${defaultLocale}-u-ca-iso8601`, "japanese"); |
| 37 |
assertEq(dtf.resolvedOptions().locale, defaultLocale); |
| 38 |
assertEq(dtf.resolvedOptions().calendar, "japanese"); |
| 39 |
|
| 40 |
// |calendar| option ignores case. |
| 41 |
dtf = create("CHINESE"); |
| 42 |
assertEq(dtf.resolvedOptions().locale, defaultLocale); |
| 43 |
assertEq(dtf.resolvedOptions().calendar, "chinese"); |
| 44 |
|
| 45 |
const calendars = [ |
| 46 |
"buddhist", "chinese", "coptic", "dangi", "ethioaa", "ethiopic-amete-alem", |
| 47 |
"ethiopic", "gregory", "hebrew", "indian", "islamic", "islamic-umalqura", |
| 48 |
"islamic-tbla", "islamic-civil", "islamic-rgsa", "iso8601", "japanese", |
| 49 |
"persian", "roc", "islamicc", |
| 50 |
]; |
| 51 |
|
| 52 |
// https://github.com/tc39/proposal-intl-locale/issues/96 |
| 53 |
const canonical = { |
| 54 |
"islamicc": "islamic-civil", |
| 55 |
"ethiopic-amete-alem": "ethioaa", |
| 56 |
}; |
| 57 |
|
| 58 |
for (let calendar of calendars) { |
| 59 |
let dtf1 = new Intl.DateTimeFormat(`${defaultLocale}-u-ca-${calendar}`); |
| 60 |
let dtf2 = new Intl.DateTimeFormat(defaultLocale, {calendar}); |
| 61 |
|
| 62 |
assertEq(dtf1.resolvedOptions().calendar, canonical[calendar] ?? calendar); |
| 63 |
assertEq(dtf2.resolvedOptions().calendar, canonical[calendar] ?? calendar); |
| 64 |
|
| 65 |
assertEq(dtf2.format(0), dtf1.format(0)); |
| 66 |
} |
| 67 |
|
| 68 |
if (typeof reportCompare === "function") |
| 69 |
reportCompare(true, true); |