diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-11-28 21:16:27 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-29 22:48:46 +0000 |
commit | d0e1997e07f82db5ff4b9c70c7008c1edd076e53 (patch) | |
tree | 2feea9d7524c3c6156d94d92e6d1ec7ace4b8bcf | |
parent | 6ad00088c0d1c492bacd0582e1c486b631e8292b (diff) | |
download | serenity-d0e1997e07f82db5ff4b9c70c7008c1edd076e53.zip |
LibJS: Implement Intl.DateTimeFormat.supportedLocalesOf
3 files changed, 66 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp index 6c3ba848d4..a816293f04 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp @@ -5,7 +5,9 @@ */ #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/DateTimeFormat.h> #include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h> @@ -25,6 +27,10 @@ void DateTimeFormatConstructor::initialize(GlobalObject& global_object) // 11.3.1 Intl.DateTimeFormat.prototype, https://tc39.es/ecma402/#sec-intl.datetimeformat.prototype define_direct_property(vm.names.prototype, global_object.intl_date_time_format_prototype(), 0); + + u8 attr = Attribute::Writable | Attribute::Configurable; + define_native_function(vm.names.supportedLocalesOf, supported_locales_of, 1, attr); + define_direct_property(vm.names.length, Value(0), Attribute::Configurable); } @@ -58,4 +64,19 @@ ThrowCompletionOr<Object*> DateTimeFormatConstructor::construct(FunctionObject& return date_time_format; } +// 11.3.2 Intl.DateTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.datetimeformat.supportedlocalesof +JS_DEFINE_NATIVE_FUNCTION(DateTimeFormatConstructor::supported_locales_of) +{ + auto locales = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let availableLocales be %DateTimeFormat%.[[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/DateTimeFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h index 1cf7bfbdf5..301d37615f 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.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/DateTimeFormat/DateTimeFormat.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.supportedLocalesOf.js new file mode 100644 index 0000000000..27cb059323 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.supportedLocalesOf.js @@ -0,0 +1,43 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Intl.DateTimeFormat.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.DateTimeFormat.supportedLocalesOf(input)).toEqual(expected); + // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment + expect( + Intl.DateTimeFormat.supportedLocalesOf(input, { localeMatcher: "best fit" }) + ).toEqual(Intl.DateTimeFormat.supportedLocalesOf(input, { localeMatcher: "lookup" })); + } + }); +}); + +describe("errors", () => { + test("invalid value for localeMatcher option", () => { + expect(() => { + Intl.DateTimeFormat.supportedLocalesOf([], { localeMatcher: "foo" }); + }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher"); + }); + + test("invalid language tag", () => { + expect(() => { + Intl.DateTimeFormat.supportedLocalesOf(["aaaaaaaaa"]); + }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag"); + }); +}); |