From 21b3c5edba16096ac8b93fcd05e2a4b89d48b6c0 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 1 Sep 2021 22:39:40 -0400 Subject: [PATCH] LibJS: Implement Intl.Locale.prototype.baseName --- .../LibJS/Runtime/CommonPropertyNames.h | 1 + .../LibJS/Runtime/Intl/LocalePrototype.cpp | 20 ++++++++++++++++++ .../LibJS/Runtime/Intl/LocalePrototype.h | 2 ++ .../Intl/Locale/Locale.prototype.baseName.js | 21 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.baseName.js diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 9e13c7959f..636b704e88 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -71,6 +71,7 @@ namespace JS { P(atan) \ P(atan2) \ P(atanh) \ + P(baseName) \ P(big) \ P(bind) \ P(blank) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index 03344f21ec..e0808a8e4f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace JS::Intl { @@ -44,6 +45,8 @@ void LocalePrototype::initialize(GlobalObject& global_object) // 14.3.2 Intl.Locale.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.Locale.prototype-@@tostringtag define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(vm, "Intl.Locale"), Attribute::Configurable); + + define_native_accessor(vm.names.baseName, base_name, {}, Attribute::Configurable); } // 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString @@ -59,4 +62,21 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::to_string) return js_string(vm, locale_object->locale()); } +// 14.3.6 get Intl.Locale.prototype.baseName, https://tc39.es/ecma402/#sec-Intl.Locale.prototype.baseName +JS_DEFINE_NATIVE_GETTER(LocalePrototype::base_name) +{ + // 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()); + VERIFY(locale.has_value()); + + // 4. Return the substring of locale corresponding to the unicode_language_id production. + return js_string(vm, locale->language_id.to_string()); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index 21de9dabd7..b6b6b76f6e 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -20,6 +20,8 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(to_string); + + JS_DECLARE_NATIVE_GETTER(base_name); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.baseName.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.baseName.js new file mode 100644 index 0000000000..e265427517 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.baseName.js @@ -0,0 +1,21 @@ +describe("errors", () => { + test("called on non-Locale object", () => { + expect(() => { + Intl.Locale.prototype.baseName; + }).toThrowWithMessage(TypeError, "Not a Intl.Locale object"); + }); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + expect(new Intl.Locale("en").baseName).toBe("en"); + expect(new Intl.Locale("en-Latn").baseName).toBe("en-Latn"); + expect(new Intl.Locale("en-GB").baseName).toBe("en-GB"); + expect(new Intl.Locale("en", { script: "Latn" }).baseName).toBe("en-Latn"); + expect(new Intl.Locale("en", { region: "GB" }).baseName).toBe("en-GB"); + + expect(new Intl.Locale("en-u-ca-abc").baseName).toBe("en"); + expect(new Intl.Locale("en", { calendar: "abc" }).baseName).toBe("en"); + expect(new Intl.Locale("en-x-abcd").baseName).toBe("en"); + }); +});