summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-01-28 13:31:04 -0500
committerLinus Groh <mail@linusgroh.de>2022-01-28 19:38:47 +0000
commit8b3f49ff8476158cccb8b37cc93b0859fb956cd6 (patch)
tree158ad7e3cad4fc0b2a3da20b3ea9c34e7f091cf4 /Userland/Libraries/LibJS
parent624fad38215ba10cb3cea9b579bc99e1cbfd7b7a (diff)
downloadserenity-8b3f49ff8476158cccb8b37cc93b0859fb956cd6.zip
LibJS: Implement Intl.PluralRules.supportedLocalesOf
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.cpp20
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.h2
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.supportedLocalesOf.js43
3 files changed, 65 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.cpp
index 95ba0d416e..fa95275c09 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.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/PluralRules.h>
#include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
@@ -26,6 +28,9 @@ void PluralRulesConstructor::initialize(GlobalObject& global_object)
// 16.3.1 Intl.PluralRules.prototype, https://tc39.es/ecma402/#sec-intl.pluralrules.prototype
define_direct_property(vm.names.prototype, global_object.intl_plural_rules_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);
}
// 16.2.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
@@ -51,4 +56,19 @@ ThrowCompletionOr<Object*> PluralRulesConstructor::construct(FunctionObject& new
return TRY(initialize_plural_rules(global_object, *plural_rules, locales, options));
}
+// 16.3.2 Intl.PluralRules.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-intl.pluralrules.supportedlocalesof
+JS_DEFINE_NATIVE_FUNCTION(PluralRulesConstructor::supported_locales_of)
+{
+ auto locales = vm.argument(0);
+ auto options = vm.argument(1);
+
+ // 1. Let availableLocales be %PluralRules%.[[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/PluralRulesConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.h
index 4aef86b23b..10952d72e6 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.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/PluralRules/PluralRules.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.supportedLocalesOf.js
new file mode 100644
index 0000000000..190d36e8ba
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.supportedLocalesOf.js
@@ -0,0 +1,43 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Intl.PluralRules.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.PluralRules.supportedLocalesOf(input)).toEqual(expected);
+ // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment
+ expect(
+ Intl.PluralRules.supportedLocalesOf(input, { localeMatcher: "best fit" })
+ ).toEqual(Intl.PluralRules.supportedLocalesOf(input, { localeMatcher: "lookup" }));
+ }
+ });
+});
+
+describe("errors", () => {
+ test("invalid value for localeMatcher option", () => {
+ expect(() => {
+ Intl.PluralRules.supportedLocalesOf([], { localeMatcher: "foo" });
+ }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher");
+ });
+
+ test("invalid language tag", () => {
+ expect(() => {
+ Intl.PluralRules.supportedLocalesOf(["aaaaaaaaa"]);
+ }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag");
+ });
+});