diff --git a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp index bfb3bfc8c9..c7c6b9316c 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.cpp @@ -5,7 +5,9 @@ */ #include +#include #include +#include #include #include @@ -26,6 +28,9 @@ void RelativeTimeFormatConstructor::initialize(GlobalObject& global_object) // 17.3.1 Intl.RelativeTimeFormat.prototype, https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.prototype define_direct_property(vm.names.prototype, global_object.intl_relative_time_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); } // 17.2.1 Intl.RelativeTimeFormat ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat @@ -51,4 +56,19 @@ ThrowCompletionOr RelativeTimeFormatConstructor::construct(FunctionObje return TRY(initialize_relative_time_format(global_object, *relative_time_format, locales, options)); } +// 17.3.2 Intl.RelativeTimeFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.RelativeTimeFormat.supportedLocalesOf +JS_DEFINE_NATIVE_FUNCTION(RelativeTimeFormatConstructor::supported_locales_of) +{ + auto locales = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let availableLocales be %RelativeTimeFormat%.[[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/RelativeTimeFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h index 2fc1b0fdc0..3fd8dad1b9 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/RelativeTimeFormatConstructor.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/RelativeTimeFormat/RelativeTimeFormat.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/RelativeTimeFormat/RelativeTimeFormat.supportedLocalesOf.js new file mode 100644 index 0000000000..d06b93850a --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/RelativeTimeFormat/RelativeTimeFormat.supportedLocalesOf.js @@ -0,0 +1,45 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Intl.RelativeTimeFormat.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.RelativeTimeFormat.supportedLocalesOf(input)).toEqual(expected); + // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment + expect( + Intl.RelativeTimeFormat.supportedLocalesOf(input, { localeMatcher: "best fit" }) + ).toEqual( + Intl.RelativeTimeFormat.supportedLocalesOf(input, { localeMatcher: "lookup" }) + ); + } + }); +}); + +describe("errors", () => { + test("invalid value for localeMatcher option", () => { + expect(() => { + Intl.RelativeTimeFormat.supportedLocalesOf([], { localeMatcher: "foo" }); + }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher"); + }); + + test("invalid language tag", () => { + expect(() => { + Intl.RelativeTimeFormat.supportedLocalesOf(["aaaaaaaaa"]); + }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag"); + }); +});