diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-09-08 13:32:19 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-29 20:27:24 +0000 |
commit | 17306078b5abea88f91a1e991b50999f9d1eb72d (patch) | |
tree | c8fe3e750f0422c8429a73bfa73d4da8300682dd | |
parent | 5a1eefcc1d83ed1335d43cc228e0df7a1ff62bc3 (diff) | |
download | serenity-17306078b5abea88f91a1e991b50999f9d1eb72d.zip |
LibJS: Implement Intl.Collator.supportedLocalesOf
3 files changed, 64 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp index b3ae20cd1e..4a56a30a41 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.cpp @@ -5,6 +5,7 @@ */ #include <LibJS/Runtime/AbstractOperations.h> +#include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/GlobalObject.h> #include <LibJS/Runtime/Intl/AbstractOperations.h> #include <LibJS/Runtime/Intl/Collator.h> @@ -145,6 +146,9 @@ void CollatorConstructor::initialize(GlobalObject& global_object) // 10.2.1 Intl.Collator.prototype, https://tc39.es/ecma402/#sec-intl.collator.prototype define_direct_property(vm.names.prototype, global_object.intl_collator_prototype(), 0); define_direct_property(vm.names.length, Value(0), Attribute::Configurable); + + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr); } // 10.1.2 Intl.Collator ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.collator @@ -176,4 +180,19 @@ ThrowCompletionOr<Object*> CollatorConstructor::construct(FunctionObject& new_ta return TRY(initialize_collator(global_object, *collator, locales, options)); } +// 10.2.2 Intl.Collator.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.collator.supportedlocalesof +JS_DEFINE_NATIVE_FUNCTION(CollatorConstructor::supported_locales_of) +{ + auto locales = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let availableLocales be %Collator%.[[AvailableLocales]]. + + // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales). + auto requested_locales = TRY(canonicalize_locale_list(global_object, locales)); + + // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options). + return TRY(supported_locales(global_object, requested_locales, options)); +} + } diff --git a/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.h index 327ec3d098..3e0e019b87 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/CollatorConstructor.h @@ -23,6 +23,8 @@ public: private: virtual bool has_constructor() const override { return true; } + + JS_DECLARE_NATIVE_FUNCTION(supported_locales_of); }; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/Collator/Collator.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Collator/Collator.supportedLocalesOf.js new file mode 100644 index 0000000000..0567cfc0ea --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Collator/Collator.supportedLocalesOf.js @@ -0,0 +1,43 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Intl.Collator.supportedLocalesOf).toHaveLength(1); + }); + + test("basic functionality", () => { + // prettier-ignore + const values = [ + [[], []], + [undefined, []], + ["en", ["en"]], + [new Intl.Locale("en"), ["en"]], + [["en"], ["en"]], + [["en", "en-gb", "en-us"], ["en", "en-GB", "en-US"]], + [["en", "de", "fr"], ["en", "de", "fr"]], + [["en-foobar"], ["en-foobar"]], + [["en-foobar-u-abc"], ["en-foobar-u-abc"]], + [["aa", "zz"], []], + [["en", "aa", "zz"], ["en"]], + ]; + for (const [input, expected] of values) { + expect(Intl.Collator.supportedLocalesOf(input)).toEqual(expected); + // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment + expect(Intl.Collator.supportedLocalesOf(input, { localeMatcher: "best fit" })).toEqual( + Intl.Collator.supportedLocalesOf(input, { localeMatcher: "lookup" }) + ); + } + }); +}); + +describe("errors", () => { + test("invalid value for localeMatcher option", () => { + expect(() => { + Intl.Collator.supportedLocalesOf([], { localeMatcher: "foo" }); + }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher"); + }); + + test("invalid language tag", () => { + expect(() => { + Intl.Collator.supportedLocalesOf(["aaaaaaaaa"]); + }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag"); + }); +}); |