diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2022-01-29 23:53:24 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-30 19:47:01 +0000 |
commit | 6b8dfefc204c5c12696bf5e8f68051339a3c5534 (patch) | |
tree | 91e1fb76089a337896aeb6c2ba3bec5b2361056b /Userland/Libraries | |
parent | 7cf3f11815b08d4d336d41cca16f930fd68e9c88 (diff) | |
download | serenity-6b8dfefc204c5c12696bf5e8f68051339a3c5534.zip |
LibJS: Implement Intl.Segmenter.supportedLocalesOf
Diffstat (limited to 'Userland/Libraries')
3 files changed, 64 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.cpp index 10e188eee6..4b16af0711 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.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/Segmenter.h> @@ -28,6 +29,9 @@ void SegmenterConstructor::initialize(GlobalObject& global_object) // 18.2.1 Intl.Segmenter.prototype, https://tc39.es/ecma402/#sec-intl.segmenter.prototype define_direct_property(vm.names.prototype, global_object.intl_segmenter_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); } // 18.1.1 Intl.Segmenter ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.segmenter @@ -83,4 +87,19 @@ ThrowCompletionOr<Object*> SegmenterConstructor::construct(FunctionObject& new_t return segmenter; } +// 18.2.2 Intl.Segmenter.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.segmenter.supportedlocalesof +JS_DEFINE_NATIVE_FUNCTION(SegmenterConstructor::supported_locales_of) +{ + auto locales = vm.argument(0); + auto options = vm.argument(1); + + // 1. Let availableLocales be %Segmenter%.[[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/SegmenterConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.h index ee789b7de8..4c27e3908a 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.h +++ b/Userland/Libraries/LibJS/Runtime/Intl/SegmenterConstructor.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/Segmenter/Segmenter.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.supportedLocalesOf.js new file mode 100644 index 0000000000..bb0407147e --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/Segmenter/Segmenter.supportedLocalesOf.js @@ -0,0 +1,43 @@ +describe("correct behavior", () => { + test("length is 1", () => { + expect(Intl.Segmenter.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.Segmenter.supportedLocalesOf(input)).toEqual(expected); + // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment + expect(Intl.Segmenter.supportedLocalesOf(input, { localeMatcher: "best fit" })).toEqual( + Intl.Segmenter.supportedLocalesOf(input, { localeMatcher: "lookup" }) + ); + } + }); +}); + +describe("errors", () => { + test("invalid value for localeMatcher option", () => { + expect(() => { + Intl.Segmenter.supportedLocalesOf([], { localeMatcher: "foo" }); + }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher"); + }); + + test("invalid language tag", () => { + expect(() => { + Intl.Segmenter.supportedLocalesOf(["aaaaaaaaa"]); + }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag"); + }); +}); |