summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-09-06 09:00:06 -0400
committerLinus Groh <mail@linusgroh.de>2021-09-06 23:49:56 +0100
commit3b410742ab5bdf8a8a5cee313075f8c5eff1a4a7 (patch)
tree134382d8554c1286dda34547b5c4281ba15162c0 /Userland
parenteacc8bef47eeffe56f293486e2912992ee3fbd7c (diff)
downloadserenity-3b410742ab5bdf8a8a5cee313075f8c5eff1a4a7.zip
LibJS: Implement Intl.ListFormat.supportedLocalesOf
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp22
-rw-r--r--Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.h2
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.supportedLocalesOf.js43
3 files changed, 67 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp
index dbbd0d2f64..6f27550aba 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.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/ListFormat.h>
@@ -27,6 +28,10 @@ void ListFormatConstructor::initialize(GlobalObject& global_object)
// 13.3.1 Intl.ListFormat.prototype, https://tc39.es/ecma402/#sec-Intl.ListFormat.prototype
define_direct_property(vm.names.prototype, global_object.intl_list_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);
}
@@ -103,4 +108,21 @@ Value ListFormatConstructor::construct(FunctionObject& new_target)
return list_format;
}
+// 13.3.2 Intl.ListFormat.supportedLocalesOf ( locales [ , options ] ), https://tc39.es/ecma402/#sec-Intl.ListFormat.supportedLocalesOf
+JS_DEFINE_NATIVE_FUNCTION(ListFormatConstructor::supported_locales_of)
+{
+ auto locales = vm.argument(0);
+ auto options = vm.argument(1);
+
+ // 1. Let availableLocales be %DisplayNames%.[[AvailableLocales]].
+
+ // 2. Let requestedLocales be ? CanonicalizeLocaleList(locales).
+ auto requested_locales = canonicalize_locale_list(global_object, locales);
+ if (vm.exception())
+ return {};
+
+ // 3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
+ return supported_locales(global_object, requested_locales, options);
+}
+
}
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.h b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.h
index 30703ed0e3..72287dd876 100644
--- a/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.h
+++ b/Userland/Libraries/LibJS/Runtime/Intl/ListFormatConstructor.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/ListFormat/ListFormat.supportedLocalesOf.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.supportedLocalesOf.js
new file mode 100644
index 0000000000..3bd5550944
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/ListFormat/ListFormat.supportedLocalesOf.js
@@ -0,0 +1,43 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Intl.ListFormat.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.ListFormat.supportedLocalesOf(input)).toEqual(expected);
+ // "best fit" (implementation defined) just uses the same implementation as "lookup" at the moment
+ expect(
+ Intl.ListFormat.supportedLocalesOf(input, { localeMatcher: "best fit" })
+ ).toEqual(Intl.ListFormat.supportedLocalesOf(input, { localeMatcher: "lookup" }));
+ }
+ });
+});
+
+describe("errors", () => {
+ test("invalid value for localeMatcher option", () => {
+ expect(() => {
+ Intl.ListFormat.supportedLocalesOf([], { localeMatcher: "foo" });
+ }).toThrowWithMessage(RangeError, "foo is not a valid value for option localeMatcher");
+ });
+
+ test("invalid language tag", () => {
+ expect(() => {
+ Intl.ListFormat.supportedLocalesOf(["aaaaaaaaa"]);
+ }).toThrowWithMessage(RangeError, "aaaaaaaaa is not a structurally valid language tag");
+ });
+});