diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-02 21:45:42 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-09-04 13:51:40 +0100 |
commit | 90971673c7b5b8cd57d66e92bc7047a1da60abe1 (patch) | |
tree | 573d0681f0274596bdf31cc027ddb52a18c28ca2 /Userland | |
parent | a77f323dfb00212e0c705209efa0155320b0256a (diff) | |
download | serenity-90971673c7b5b8cd57d66e92bc7047a1da60abe1.zip |
LibJS: Implement Intl.Locale.prototype.minimize
Diffstat (limited to 'Userland')
4 files changed, 57 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index fe3a28a4b4..f0604cf635 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -289,6 +289,7 @@ namespace JS { P(millisecond) \ P(milliseconds) \ P(min) \ + P(minimize) \ P(minute) \ P(minutes) \ P(month) \ diff --git a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp index 7078a41c74..a2ffb0afb6 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.cpp @@ -42,6 +42,7 @@ void LocalePrototype::initialize(GlobalObject& global_object) u8 attr = Attribute::Writable | Attribute::Configurable; define_native_function(vm.names.maximize, maximize, 0, attr); + define_native_function(vm.names.minimize, minimize, 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 @@ -79,6 +80,26 @@ JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::maximize) return Locale::create(global_object, *locale); } +// 14.3.4 Intl.Locale.prototype.minimize ( ), https://tc39.es/ecma402/#sec-Intl.Locale.prototype.minimize +JS_DEFINE_NATIVE_FUNCTION(LocalePrototype::minimize) +{ + // 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 minimal be the result of the Remove Likely Subtags algorithm applied to loc.[[Locale]]. If an error is signaled, set minimal to loc.[[Locale]]. + if (auto minimal = Unicode::remove_likely_subtags(locale->language_id); minimal.has_value()) + locale->language_id = minimal.release_value(); + + // 4. Return ! Construct(%Locale%, minimal). + 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 1e4b02cf2e..497269b610 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/LocalePrototype.h @@ -20,6 +20,7 @@ public: private: JS_DECLARE_NATIVE_FUNCTION(maximize); + JS_DECLARE_NATIVE_FUNCTION(minimize); JS_DECLARE_NATIVE_FUNCTION(to_string); JS_DECLARE_NATIVE_GETTER(base_name); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.minimize.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.minimize.js new file mode 100644 index 0000000000..724191f585 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Locale/Locale.prototype.minimize.js @@ -0,0 +1,34 @@ +test("length is 0", () => { + expect(Intl.Locale.prototype.minimize).toHaveLength(0); +}); + +test("normal behavior", () => { + expect(new Intl.Locale("en").minimize().toString()).toBe("en"); + + expect(new Intl.Locale("en-Latn").minimize().toString()).toBe("en"); + expect(new Intl.Locale("ar-Arab").minimize().toString()).toBe("ar"); + expect(new Intl.Locale("en-US").minimize().toString()).toBe("en"); + expect(new Intl.Locale("en-GB").minimize().toString()).toBe("en-GB"); + + expect(new Intl.Locale("en-Latn-US").minimize().toString()).toBe("en"); + expect(new Intl.Locale("en-Shaw-GB").minimize().toString()).toBe("en-Shaw"); + expect(new Intl.Locale("en-Arab-US").minimize().toString()).toBe("en-Arab"); + expect(new Intl.Locale("en-Latn-GB").minimize().toString()).toBe("en-GB"); + expect(new Intl.Locale("en-Latn-FR").minimize().toString()).toBe("en-FR"); + + expect(new Intl.Locale("it-Kana-CA").minimize().toString()).toBe("it-Kana-CA"); + + expect(new Intl.Locale("th-Thai-TH").minimize().toString()).toBe("th"); + expect(new Intl.Locale("es-Latn-419").minimize().toString()).toBe("es-419"); + expect(new Intl.Locale("ru-Cyrl-RU").minimize().toString()).toBe("ru"); + expect(new Intl.Locale("de-Latn-AT").minimize().toString()).toBe("de-AT"); + expect(new Intl.Locale("bg-Cyrl-RO").minimize().toString()).toBe("bg-RO"); + expect(new Intl.Locale("und-Latn-AQ").minimize().toString()).toBe("und-AQ"); +}); + +test("keywords are preserved", () => { + expect(new Intl.Locale("en-Latn-US-u-ca-abc").minimize().toString()).toBe("en-u-ca-abc"); + expect(new Intl.Locale("en-Latn-US", { calendar: "abc" }).minimize().toString()).toBe( + "en-u-ca-abc" + ); +}); |