constructor-options-variants-valid.js (2358B)
1 // Copyright 2025 Richard Gibson. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-intl.locale 6 description: > 7 Checks error cases for the options argument to the Locale 8 constructor. 9 info: | 10 Intl.Locale( tag [, options] ) 11 12. Set _tag_ to ? UpdateLanguageId(_tag_, _options_). 12 13 UpdateLanguageId ( tag, options ) 14 8. Let _variants_ be ? GetOption(_options_, *"variants"*, ~string~, ~empty~, GetLocaleVariants(_baseName_)). 15 ... 16 13. If _variants_ is not *undefined*, set _newTag_ to the string-concatenation of _newTag_, *"-"*, and _variants_. 17 18 features: [Intl.Locale] 19 ---*/ 20 21 const validVariantsOptions = [ 22 ['en', undefined, undefined], 23 ['en', 'spanglis', 'en-spanglis'], 24 25 // unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3}) 26 ['xx', '1xyz', 'xx-1xyz'], 27 ['xx', '1234', 'xx-1234'], 28 ['xx', 'abcde', 'xx-abcde'], 29 ['xx', '12345678', 'xx-12345678'], 30 ['xx', '1xyz-1234-abcde-12345678', 'xx-1234-12345678-1xyz-abcde'], 31 32 // Canonicalization affects subtag ordering. 33 ['en', 'spanglis-oxendict', 'en-oxendict-spanglis'], 34 ]; 35 for (const [lang, variants, baseName] of validVariantsOptions) { 36 let options = { variants }; 37 let optionsRepr = `{variants: ${typeof variants === "string" ? `"${variants}"` : variants}}`; 38 let instance; 39 let expect; 40 41 instance = new Intl.Locale(lang, options); 42 expect = baseName || lang; 43 assert.sameValue(instance.toString(), expect, 44 `new Intl.Locale("${lang}", ${optionsRepr}).toString() returns "${expect}"`); 45 46 instance = new Intl.Locale(lang + '-fonipa', options); 47 expect = baseName || (lang + '-fonipa'); 48 assert.sameValue(instance.toString(), expect, 49 `new Intl.Locale("${lang}-fonipa", ${optionsRepr}).toString() returns "${expect}"`); 50 51 instance = new Intl.Locale(lang + '-u-ca-gregory', options); 52 expect = (baseName || lang) + '-u-ca-gregory'; 53 assert.sameValue(instance.toString(), expect, 54 `new Intl.Locale("${lang}-u-ca-gregory", ${optionsRepr}).toString() returns "${expect}"`); 55 56 instance = new Intl.Locale(lang + '-fonipa-u-ca-gregory', options); 57 expect = (baseName || (lang + '-fonipa')) + '-u-ca-gregory'; 58 assert.sameValue(instance.toString(), expect, 59 `new Intl.Locale("${lang}-fonipa-u-ca-gregory", ${optionsRepr}).toString() returns "${expect}"`); 60 } 61 62 reportCompare(0, 0);