From b20ef7d6403cf611acfac63f03609a5f05a0ef9d Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 30 Jun 2022 01:25:47 +0300 Subject: [PATCH] LibJS: Implement Intl.DurationFormat.supportedLocalesOf --- .../Intl/DurationFormatConstructor.cpp | 19 ++++++++ .../Runtime/Intl/DurationFormatConstructor.h | 2 + .../DurationFormat.supportedLocalesOf.js | 43 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.supportedLocalesOf.js diff --git a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.cpp index 5109d846ca..2028c2871c 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -27,6 +28,9 @@ void DurationFormatConstructor::initialize(GlobalObject& global_object) // 1.3.1 Intl.DurationFormat.prototype, https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.prototype define_direct_property(vm.names.prototype, global_object.intl_duration_format_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); } // 1.2.1 Intl.DurationFormat ( [ locales [ , options ] ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat @@ -137,4 +141,19 @@ ThrowCompletionOr DurationFormatConstructor::construct(FunctionObject& return duration_format; } +// 1.3.2 Intl.DurationFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/proposal-intl-duration-format/#sec-Intl.DurationFormat.supportedLocalesOf +JS_DEFINE_NATIVE_FUNCTION(DurationFormatConstructor::supported_locales_of) +{ + auto locales = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let availableLocales be %DurationFormat%.[[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/DurationFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.h index 8b42105136..400a32d5d8 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/DurationFormatConstructor.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/DurationFormat/DurationFormat.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.supportedLocalesOf.js new file mode 100644 index 0000000000..aa06862fd9 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/DurationFormat/DurationFormat.supportedLocalesOf.js @@ -0,0 +1,43 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Intl.DurationFormat.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.DurationFormat.supportedLocalesOf(input)).toEqual(expected); + // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment + expect( + Intl.DurationFormat.supportedLocalesOf(input, { localeMatcher: "best fit" }) + ).toEqual(Intl.DurationFormat.supportedLocalesOf(input, { localeMatcher: "lookup" })); + } + }); +}); + +describe("errors", () => { + test("invalid value for localeMatcher option", () => { + expect(() => { + Intl.DurationFormat.supportedLocalesOf([], { localeMatcher: "foo" }); + }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher"); + }); + + test("invalid language tag", () => { + expect(() => { + Intl.DurationFormat.supportedLocalesOf(["aaaaaaaaa"]); + }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag"); + }); +});