diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-02 08:25:20 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-02 17:56:42 +0100 |
commit | 32825107de88df9f86f3eb9b78fcd4b9c0cc9ea0 (patch) | |
tree | 8aa1c7b14205c56d2e1f7054bd5df99bc94eaee1 /Userland | |
parent | 349fd06b862a66cc53086e9d3298f4101fd8ece2 (diff) | |
download | serenity-32825107de88df9f86f3eb9b78fcd4b9c0cc9ea0.zip |
LibJS: Implement Intl.Locale.prototype.region
Diffstat (limited to 'Userland')
3 files changed, 46 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index 3c4037d8f6..0603a228ac 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -55,6 +55,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.numeric, numeric, {}, Attribute::Configurable); define_native_accessor(vm.names.language, language, {}, Attribute::Configurable); define_native_accessor(vm.names.script, script, {}, Attribute::Configurable); + define_native_accessor(vm.names.region, region, {}, Attribute::Configurable); } // 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString @@ -167,4 +168,27 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::script) return js_string(vm, *locale->language_id.script); } +// 14.3.15 get Intl.Locale.prototype.region, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.region +JS_DEFINE_NATIVE_GETTER(LocalePrototype::region) +{ + // 1. Let loc be the this value. + // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). + auto* locale_object = typed_this(global_object); + if (!locale_object) + return {}; + + // 3. Let locale be loc.[[Locale]]. + auto locale = Unicode::parse_unicode_locale_id(locale_object->locale()); + + // 4. Assert: locale matches the unicode_locale_id production. + VERIFY(locale.has_value()); + + // 5. If the unicode_language_id production of locale does not contain the ["-" unicode_region_subtag] sequence, return undefined. + if (!locale->language_id.region.has_value()) + return js_undefined(); + + // 6. Return the substring of locale corresponding to the unicode_region_subtag production of the unicode_language_id. + return js_string(vm, *locale->language_id.region); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index c622f751b6..2d720d2b9f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -30,6 +30,7 @@ private: JS_DECLARE_NATIVE_GETTER(numeric); JS_DECLARE_NATIVE_GETTER(language); JS_DECLARE_NATIVE_GETTER(script); + JS_DECLARE_NATIVE_GETTER(region); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.region.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.region.js new file mode 100644 index 0000000000..f42fd53655 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.region.js @@ -0,0 +1,21 @@ +describe("errors", () => { + test("called on non-Locale object", () => { + expect(() => { + Intl.Locale.prototype.region; + }).toThrowWithMessage(TypeError, "Not a Intl.Locale object"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + expect(new Intl.Locale("en").region).toBeUndefined(); + expect(new Intl.Locale("en-Latn").region).toBeUndefined(); + expect(new Intl.Locale("en-GB").region).toBe("GB"); + expect(new Intl.Locale("en", { script: "Latn" }).region).toBeUndefined(); + expect(new Intl.Locale("en", { region: "GB" }).region).toBe("GB"); + + expect(new Intl.Locale("en-u-ca-abc").region).toBeUndefined(); + expect(new Intl.Locale("en", { calendar: "abc" }).region).toBeUndefined(); + expect(new Intl.Locale("en-x-abcd").region).toBeUndefined(); + }); +}); |