diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-02 08:19:29 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-02 17:56:42 +0100 |
commit | c3b6f436417c781acf22697176222690d7ea06b0 (patch) | |
tree | 2601bceb26be3b344df8d40ec732be60061b4336 /Userland/Libraries/LibJS | |
parent | bdf36575c805efbf995fb233f6ea68deff6ba66a (diff) | |
download | serenity-c3b6f436417c781acf22697176222690d7ea06b0.zip |
LibJS: Implement Intl.Locale.prototype.language
Diffstat (limited to 'Userland/Libraries/LibJS')
3 files changed, 42 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index fa82a5841d..0d9de229f0 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -53,6 +53,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.hourCycle, hour_cycle, {}, Attribute::Configurable); define_native_accessor(vm.names.numberingSystem, numbering_system, {}, Attribute::Configurable); define_native_accessor(vm.names.numeric, numeric, {}, Attribute::Configurable); + define_native_accessor(vm.names.language, language, {}, Attribute::Configurable); } // 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString @@ -123,4 +124,23 @@ JS_DEFINE_NATIVE_GETTER(LocalePrototype::numeric) return Value(locale_object->numeric()); } +// 14.3.13 get Intl.Locale.prototype.language, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.language +JS_DEFINE_NATIVE_GETTER(LocalePrototype::language) +{ + // 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. Return the substring of locale corresponding to the unicode_language_subtag production of the unicode_language_id. + return js_string(vm, *locale->language_id.language); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index bdd557c569..8827c8cda9 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -28,6 +28,7 @@ private: JS_DECLARE_NATIVE_GETTER(hour_cycle); JS_DECLARE_NATIVE_GETTER(numbering_system); JS_DECLARE_NATIVE_GETTER(numeric); + JS_DECLARE_NATIVE_GETTER(language); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.language.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.language.js new file mode 100644 index 0000000000..5b6820bb3a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.language.js @@ -0,0 +1,21 @@ +describe("errors", () => { + test("called on non-Locale object", () => { + expect(() => { + Intl.Locale.prototype.language; + }).toThrowWithMessage(TypeError, "Not a Intl.Locale object"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + expect(new Intl.Locale("en").language).toBe("en"); + expect(new Intl.Locale("en-Latn").language).toBe("en"); + expect(new Intl.Locale("en-GB").language).toBe("en"); + expect(new Intl.Locale("en", { script: "Latn" }).language).toBe("en"); + expect(new Intl.Locale("en", { region: "GB" }).language).toBe("en"); + + expect(new Intl.Locale("en-u-ca-abc").language).toBe("en"); + expect(new Intl.Locale("en", { calendar: "abc" }).language).toBe("en"); + expect(new Intl.Locale("en-x-abcd").language).toBe("en"); + }); +}); |