1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:37:43 +00:00

LibJS: Populate roundingPriority in Intl.PluralRules.resolvedOptions

This is inherited from Intl.NumberFormat.
This commit is contained in:
Timothy Flynn 2022-07-12 13:23:21 -04:00 committed by Linus Groh
parent 33698b9615
commit cd4ee46b70
3 changed files with 47 additions and 1 deletions

View file

@ -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;
}

View file

@ -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();
});
});
});

View file

@ -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);
});
});
});