diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-07-12 13:23:21 -0400 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-13 19:22:26 +0100 |
commit | cd4ee46b706e23ffd7efe57034f95e2d6090987a (patch) | |
tree | 8411e670d91805881be655c59c5a1e90cb88c21b | |
parent | 33698b961542618e64c6aede5213c3b0624399d0 (diff) | |
download | serenity-cd4ee46b706e23ffd7efe57034f95e2d6090987a.zip |
LibJS: Populate roundingPriority in Intl.PluralRules.resolvedOptions
This is inherited from Intl.NumberFormat.
3 files changed, 47 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesPrototype.cpp index ccc272b64c..32ef606755 100644 --- a/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Intl/PluralRulesPrototype.cpp @@ -76,6 +76,7 @@ JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::select_range) } // 16.3.4 Intl.PluralRules.prototype.resolvedOptions ( ), https://tc39.es/ecma402/#sec-intl.pluralrules.prototype.resolvedoptions +// 1.4.5 Intl.PluralRules.prototype.resolvedOptions ( ), https://tc39.es/proposal-intl-numberformat-v3/out/pluralrules/proposed.html#sec-intl.pluralrules.prototype.resolvedoptions JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::resolved_options) { // 1. Let pr be the this value. @@ -112,7 +113,25 @@ JS_DEFINE_NATIVE_FUNCTION(PluralRulesPrototype::resolved_options) // 6. Perform ! CreateDataProperty(options, "pluralCategories", CreateArrayFromList(pluralCategories)). MUST(options->create_data_property_or_throw(vm.names.pluralCategories, plural_categories)); - // 7. Return options. + switch (plural_rules->rounding_type()) { + // 7. If pr.[[RoundingType]] is morePrecision, then + case NumberFormatBase::RoundingType::MorePrecision: + // a. Perform ! CreateDataPropertyOrThrow(options, "roundingPriority", "morePrecision"). + MUST(options->create_data_property_or_throw(vm.names.roundingPriority, js_string(vm, "morePrecision"sv))); + break; + // 8. Else if pr.[[RoundingType]] is lessPrecision, then + case NumberFormatBase::RoundingType::LessPrecision: + // a. Perform ! CreateDataPropertyOrThrow(options, "roundingPriority", "lessPrecision"). + MUST(options->create_data_property_or_throw(vm.names.roundingPriority, js_string(vm, "lessPrecision"sv))); + break; + // 9. Else, + default: + // a. Perform ! CreateDataPropertyOrThrow(options, "roundingPriority", "auto"). + MUST(options->create_data_property_or_throw(vm.names.roundingPriority, js_string(vm, "auto"sv))); + break; + } + + // 10. Return options. return options; } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.js index 8467d6f432..8c983d514a 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.js @@ -116,6 +116,15 @@ describe("errors", () => { new Intl.PluralRules("en", { maximumSignificantDigits: 22 }); }).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21"); }); + + test("roundingPriority option is invalid", () => { + expect(() => { + new Intl.PluralRules("en", { roundingPriority: "hello!" }); + }).toThrowWithMessage( + RangeError, + "hello! is not a valid value for option roundingPriority" + ); + }); }); describe("normal behavior", () => { @@ -178,4 +187,12 @@ describe("normal behavior", () => { }).not.toThrow(); } }); + + test("all valid roundingPriority options", () => { + ["auto", "morePrecision", "lessPrecision"].forEach(roundingPriority => { + expect(() => { + new Intl.PluralRules("en", { roundingPriority: roundingPriority }); + }).not.toThrow(); + }); + }); }); diff --git a/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.prototype.resolvedOptions.js b/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.prototype.resolvedOptions.js index 693bc248b4..27e65fb39f 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.prototype.resolvedOptions.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Intl/PluralRules/PluralRules.prototype.resolvedOptions.js @@ -104,4 +104,14 @@ describe("correct behavior", () => { expect(gaOrdinal.pluralCategories).toBeDefined(); expect(contains(gaOrdinal.pluralCategories, ["other", "one"])).toBeTrue(); }); + + test("rounding priority", () => { + const en1 = new Intl.PluralRules("en"); + expect(en1.resolvedOptions().roundingPriority).toBe("auto"); + + ["auto", "morePrecision", "lessPrecision"].forEach(roundingPriority => { + const en2 = new Intl.PluralRules("en", { roundingPriority: roundingPriority }); + expect(en2.resolvedOptions().roundingPriority).toBe(roundingPriority); + }); + }); }); |