Pārlūkot izejas kodu

LibJS: Implement the Intl.PluralRules constructor

Timothy Flynn 3 gadi atpakaļ
vecāks
revīzija
348059bffd

+ 45 - 0
Userland/Libraries/LibJS/Runtime/Intl/PluralRules.cpp

@@ -4,6 +4,8 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <AK/Array.h>
+#include <LibJS/Runtime/GlobalObject.h>
 #include <LibJS/Runtime/Intl/PluralRules.h>
 
 namespace JS::Intl {
@@ -37,4 +39,47 @@ StringView PluralRules::type_string() const
     }
 }
 
+// 16.1.1 InitializePluralRules ( pluralRules, locales, options ), https://tc39.es/ecma402/#sec-initializepluralrules
+ThrowCompletionOr<PluralRules*> initialize_plural_rules(GlobalObject& global_object, PluralRules& plural_rules, Value locales_value, Value options_value)
+{
+    auto& vm = global_object.vm();
+
+    // 1. Let requestedLocales be ? CanonicalizeLocaleList(locales).
+    auto requested_locales = TRY(canonicalize_locale_list(global_object, locales_value));
+
+    // 2. Set options to ? CoerceOptionsToObject(options).
+    auto* options = TRY(coerce_options_to_object(global_object, options_value));
+
+    // 3. Let opt be a new Record.
+    LocaleOptions opt {};
+
+    // 4. Let matcher be ? GetOption(options, "localeMatcher", "string", « "lookup", "best fit" », "best fit").
+    auto matcher = TRY(get_option(global_object, *options, vm.names.localeMatcher, Value::Type::String, AK::Array { "lookup"sv, "best fit"sv }, "best fit"sv));
+
+    // 5. Set opt.[[localeMatcher]] to matcher.
+    opt.locale_matcher = matcher;
+
+    // 6. Let t be ? GetOption(options, "type", "string", « "cardinal", "ordinal" », "cardinal").
+    auto type = TRY(get_option(global_object, *options, vm.names.type, Value::Type::String, AK::Array { "cardinal"sv, "ordinal"sv }, "cardinal"sv));
+
+    // 7. Set pluralRules.[[Type]] to t.
+    plural_rules.set_type(type.as_string().string());
+
+    // 8. Perform ? SetNumberFormatDigitOptions(pluralRules, options, +0𝔽, 3𝔽, "standard").
+    TRY(set_number_format_digit_options(global_object, plural_rules, *options, 0, 3, NumberFormat::Notation::Standard));
+
+    // 9. Let localeData be %PluralRules%.[[LocaleData]].
+    // 10. Let r be ResolveLocale(%PluralRules%.[[AvailableLocales]], requestedLocales, opt, %PluralRules%.[[RelevantExtensionKeys]], localeData).
+    auto result = resolve_locale(requested_locales, opt, {});
+
+    // 11. Set pluralRules.[[Locale]] to r.[[locale]].
+    plural_rules.set_locale(move(result.locale));
+
+    // Non-standard, the data locale is used by our NumberFormat implementation.
+    plural_rules.set_data_locale(move(result.data_locale));
+
+    // 12. Return pluralRules.
+    return &plural_rules;
+}
+
 }

+ 3 - 0
Userland/Libraries/LibJS/Runtime/Intl/PluralRules.h

@@ -8,6 +8,7 @@
 
 #include <AK/String.h>
 #include <AK/StringView.h>
+#include <LibJS/Runtime/Completion.h>
 #include <LibJS/Runtime/Intl/NumberFormat.h>
 #include <LibJS/Runtime/Object.h>
 
@@ -33,4 +34,6 @@ private:
     Type m_type { Type::Cardinal }; // [[Type]]
 };
 
+ThrowCompletionOr<PluralRules*> initialize_plural_rules(GlobalObject& global_object, PluralRules& plural_rules, Value locales_value, Value options_value);
+
 }

+ 5 - 1
Userland/Libraries/LibJS/Runtime/Intl/PluralRulesConstructor.cpp

@@ -38,13 +38,17 @@ ThrowCompletionOr<Value> PluralRulesConstructor::call()
 // 16.2.1 Intl.PluralRules ( [ locales [ , options ] ] ), https://tc39.es/ecma402/#sec-intl.pluralrules
 ThrowCompletionOr<Object*> PluralRulesConstructor::construct(FunctionObject& new_target)
 {
+    auto& vm = this->vm();
     auto& global_object = this->global_object();
 
+    auto locales = vm.argument(0);
+    auto options = vm.argument(1);
+
     // 2. Let pluralRules be ? OrdinaryCreateFromConstructor(NewTarget, "%PluralRules.prototype%", « [[InitializedPluralRules]], [[Locale]], [[Type]], [[MinimumIntegerDigits]], [[MinimumFractionDigits]], [[MaximumFractionDigits]], [[MinimumSignificantDigits]], [[MaximumSignificantDigits]], [[RoundingType]] »).
     auto* plural_rules = TRY(ordinary_create_from_constructor<PluralRules>(global_object, new_target, &GlobalObject::intl_plural_rules_prototype));
 
     // 3. Return ? InitializePluralRules(pluralRules, locales, options).
-    return plural_rules;
+    return TRY(initialize_plural_rules(global_object, *plural_rules, locales, options));
 }
 
 }

+ 168 - 0
Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.js

@@ -4,10 +4,178 @@ describe("errors", () => {
             Intl.PluralRules();
         }).toThrowWithMessage(TypeError, "Intl.PluralRules constructor must be called with 'new'");
     });
+
+    test("options is an invalid type", () => {
+        expect(() => {
+            new Intl.PluralRules("en", null);
+        }).toThrowWithMessage(TypeError, "ToObject on null or undefined");
+    });
+
+    test("localeMatcher option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { localeMatcher: "hello!" });
+        }).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
+    });
+
+    test("type option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { type: "hello!" });
+        }).toThrowWithMessage(RangeError, "hello! is not a valid value for option type");
+    });
+
+    test("minimumIntegerDigits option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { minimumIntegerDigits: 1n });
+        }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumIntegerDigits: "hello!" });
+        }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 1 and 21");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumIntegerDigits: 0 });
+        }).toThrowWithMessage(RangeError, "Value 0 is NaN or is not between 1 and 21");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumIntegerDigits: 22 });
+        }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
+    });
+
+    test("minimumFractionDigits option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { minimumFractionDigits: 1n });
+        }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumFractionDigits: "hello!" });
+        }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 0 and 20");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumFractionDigits: -1 });
+        }).toThrowWithMessage(RangeError, "Value -1 is NaN or is not between 0 and 20");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumFractionDigits: 21 });
+        }).toThrowWithMessage(RangeError, "Value 21 is NaN or is not between 0 and 20");
+    });
+
+    test("maximumFractionDigits option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { maximumFractionDigits: 1n });
+        }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
+
+        expect(() => {
+            new Intl.PluralRules("en", { maximumFractionDigits: "hello!" });
+        }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 0 and 20");
+
+        expect(() => {
+            new Intl.PluralRules("en", { maximumFractionDigits: -1 });
+        }).toThrowWithMessage(RangeError, "Value -1 is NaN or is not between 0 and 20");
+
+        expect(() => {
+            new Intl.PluralRules("en", { maximumFractionDigits: 21 });
+        }).toThrowWithMessage(RangeError, "Value 21 is NaN or is not between 0 and 20");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumFractionDigits: 10, maximumFractionDigits: 5 });
+        }).toThrowWithMessage(RangeError, "Minimum value 10 is larger than maximum value 5");
+    });
+
+    test("minimumSignificantDigits option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { minimumSignificantDigits: 1n });
+        }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumSignificantDigits: "hello!" });
+        }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 1 and 21");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumSignificantDigits: 0 });
+        }).toThrowWithMessage(RangeError, "Value 0 is NaN or is not between 1 and 21");
+
+        expect(() => {
+            new Intl.PluralRules("en", { minimumSignificantDigits: 22 });
+        }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
+    });
+
+    test("maximumSignificantDigits option is invalid ", () => {
+        expect(() => {
+            new Intl.PluralRules("en", { maximumSignificantDigits: 1n });
+        }).toThrowWithMessage(TypeError, "Cannot convert BigInt to number");
+
+        expect(() => {
+            new Intl.PluralRules("en", { maximumSignificantDigits: "hello!" });
+        }).toThrowWithMessage(RangeError, "Value NaN is NaN or is not between 1 and 21");
+
+        expect(() => {
+            new Intl.PluralRules("en", { maximumSignificantDigits: 0 });
+        }).toThrowWithMessage(RangeError, "Value 0 is NaN or is not between 1 and 21");
+
+        expect(() => {
+            new Intl.PluralRules("en", { maximumSignificantDigits: 22 });
+        }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
+    });
 });
 
 describe("normal behavior", () => {
     test("length is 0", () => {
         expect(Intl.PluralRules).toHaveLength(0);
     });
+
+    test("all valid localeMatcher options", () => {
+        ["lookup", "best fit"].forEach(localeMatcher => {
+            expect(() => {
+                new Intl.PluralRules("en", { localeMatcher: localeMatcher });
+            }).not.toThrow();
+        });
+    });
+
+    test("all valid type options", () => {
+        ["cardinal", "ordinal"].forEach(type => {
+            expect(() => {
+                new Intl.PluralRules("en", { type: type });
+            }).not.toThrow();
+        });
+    });
+
+    test("all valid minimumIntegerDigits options", () => {
+        for (let i = 1; i <= 21; ++i) {
+            expect(() => {
+                new Intl.PluralRules("en", { minimumIntegerDigits: i });
+            }).not.toThrow();
+        }
+    });
+
+    test("all valid minimumFractionDigits options", () => {
+        for (let i = 0; i <= 20; ++i) {
+            expect(() => {
+                new Intl.PluralRules("en", { minimumFractionDigits: i });
+            }).not.toThrow();
+        }
+    });
+
+    test("all valid maximumFractionDigits options", () => {
+        for (let i = 0; i <= 20; ++i) {
+            expect(() => {
+                new Intl.PluralRules("en", { maximumFractionDigits: i });
+            }).not.toThrow();
+        }
+    });
+
+    test("all valid minimumSignificantDigits options", () => {
+        for (let i = 1; i <= 21; ++i) {
+            expect(() => {
+                new Intl.PluralRules("en", { minimumSignificantDigits: i });
+            }).not.toThrow();
+        }
+    });
+
+    test("all valid maximumSignificantDigits options", () => {
+        for (let i = 1; i <= 21; ++i) {
+            expect(() => {
+                new Intl.PluralRules("en", { maximumSignificantDigits: i });
+            }).not.toThrow();
+        }
+    });
 });