diff options
4 files changed, 55 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 636b704e88..fe3a28a4b4 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -281,6 +281,7 @@ namespace JS { P(log10) \ P(map) \ P(max) \ + P(maximize) \ P(mergeFields) \ P(message) \ P(microsecond) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index 0603a228ac..7078a41c74 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -41,6 +41,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) auto& vm = this->vm(); u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.maximize, maximize, 0, attr); define_native_function(vm.names.toString, to_string, 0, attr); // 14.3.2 Intl.Locale.prototype[ @@toStringTag ], https://tc39.es/ecma402/#sec-Intl.Locale.prototype-@@tostringtag @@ -58,6 +59,26 @@ void LocalePrototype::initialize(GlobalObject& global_object) define_native_accessor(vm.names.region, region, {}, Attribute::Configurable); } +// 14.3.3 Intl.Locale.prototype.maximize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.maximize +JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::maximize) +{ + // 1. Let loc be the this value. + // 2. Perform ? RequireInternalSlot(loc, [[InitializedLocale]]). + auto* locale_object = typed_this(global_object); + if (!locale_object) + return {}; + + auto locale = Unicode::parse_unicode_locale_id(locale_object->locale()); + VERIFY(locale.has_value()); + + // 3. Let maximal be the result of the Add Likely Subtags algorithm applied to loc.[[Locale]]. If an error is signaled, set maximal to loc.[[Locale]]. + if (auto maximal = Unicode::add_likely_subtags(locale->language_id); maximal.has_value()) + locale->language_id = maximal.release_value(); + + // 4. Return ! Construct(%Locale%, maximal). + return Locale::create(global_object, *locale); +} + // 14.3.5 Intl.Locale.prototype.toString ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.toString JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::to_string) { diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h index 2d720d2b9f..1e4b02cf2e 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -19,6 +19,7 @@ public: virtual ~LocalePrototype() override = default; private: + JS_DECLARE_NATIVE_FUNCTION(maximize); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_GETTER(base_name); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.maximize.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.maximize.js new file mode 100644 index 0000000000..8f6bd34191 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.maximize.js @@ -0,0 +1,32 @@ +test("length is 0", () => { + expect(Intl.Locale.prototype.maximize).toHaveLength(0); +}); + +test("normal behavior", () => { + expect(new Intl.Locale("en").maximize().toString()).toBe("en-Latn-US"); + + expect(new Intl.Locale("en-Latn").maximize().toString()).toBe("en-Latn-US"); + expect(new Intl.Locale("en-Shaw").maximize().toString()).toBe("en-Shaw-GB"); + expect(new Intl.Locale("en-Arab").maximize().toString()).toBe("en-Arab-US"); + + expect(new Intl.Locale("en-US").maximize().toString()).toBe("en-Latn-US"); + expect(new Intl.Locale("en-GB").maximize().toString()).toBe("en-Latn-GB"); + expect(new Intl.Locale("en-FR").maximize().toString()).toBe("en-Latn-FR"); + + expect(new Intl.Locale("it-Kana-CA").maximize().toString()).toBe("it-Kana-CA"); + + expect(new Intl.Locale("und").maximize().toString()).toBe("en-Latn-US"); + expect(new Intl.Locale("und-Thai").maximize().toString()).toBe("th-Thai-TH"); + expect(new Intl.Locale("und-419").maximize().toString()).toBe("es-Latn-419"); + expect(new Intl.Locale("und-150").maximize().toString()).toBe("ru-Cyrl-RU"); + expect(new Intl.Locale("und-AT").maximize().toString()).toBe("de-Latn-AT"); + expect(new Intl.Locale("und-Cyrl-RO").maximize().toString()).toBe("bg-Cyrl-RO"); + expect(new Intl.Locale("und-AQ").maximize().toString()).toBe("und-Latn-AQ"); +}); + +test("keywords are preserved", () => { + expect(new Intl.Locale("en-u-ca-abc").maximize().toString()).toBe("en-Latn-US-u-ca-abc"); + expect(new Intl.Locale("en", { calendar: "abc" }).maximize().toString()).toBe( + "en-Latn-US-u-ca-abc" + ); +}); |