mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:07:36 +00:00
LibJS: Handle NumberFormat's [[UseGrouping]] option for "true" / "false"
This is a normative change to the Intl NumberFormat V3 spec. See:
4751da5
This commit is contained in:
parent
9743849ddb
commit
887dac0929
3 changed files with 24 additions and 7 deletions
|
@ -633,12 +633,17 @@ ThrowCompletionOr<StringOrBoolean> get_string_or_boolean_option(VM& vm, Object c
|
||||||
// 6. Let value be ? ToString(value).
|
// 6. Let value be ? ToString(value).
|
||||||
auto value_string = TRY(value.to_string(vm));
|
auto value_string = TRY(value.to_string(vm));
|
||||||
|
|
||||||
// 7. If values does not contain an element equal to value, return fallback.
|
// 7. NOTE: For historical reasons, the strings "true" and "false" are treated the same as the boolean value true.
|
||||||
auto it = find(values.begin(), values.end(), value_string);
|
// 8. If value is "true" or "false", return fallback.
|
||||||
if (it == values.end())
|
if (value_string.is_one_of("true"sv, "false"sv))
|
||||||
return fallback;
|
return fallback;
|
||||||
|
|
||||||
// 8. Return value.
|
// 9. If values does not contain an element equal to value, throw a RangeError exception.
|
||||||
|
auto it = find(values.begin(), values.end(), value_string);
|
||||||
|
if (it == values.end())
|
||||||
|
return vm.throw_completion<RangeError>(ErrorType::OptionIsNotValidValue, value_string, property.as_string());
|
||||||
|
|
||||||
|
// 10. Return value.
|
||||||
return StringOrBoolean { *it };
|
return StringOrBoolean { *it };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -209,6 +209,12 @@ describe("errors", () => {
|
||||||
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option signDisplay");
|
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option signDisplay");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("useGrouping option is invalid", () => {
|
||||||
|
expect(() => {
|
||||||
|
new Intl.NumberFormat("en", { useGrouping: "hello!" });
|
||||||
|
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option useGrouping");
|
||||||
|
});
|
||||||
|
|
||||||
test("roundingPriority option is invalid", () => {
|
test("roundingPriority option is invalid", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new Intl.NumberFormat("en", { roundingPriority: "hello!" });
|
new Intl.NumberFormat("en", { roundingPriority: "hello!" });
|
||||||
|
@ -416,7 +422,7 @@ describe("normal behavior", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("valid useGrouping options", () => {
|
test("valid useGrouping options", () => {
|
||||||
["min2", "auto", "always", false, true, ""].forEach(useGrouping => {
|
["min2", "auto", "always", false, true, "false", "true", ""].forEach(useGrouping => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
new Intl.NumberFormat("en", { useGrouping: useGrouping });
|
new Intl.NumberFormat("en", { useGrouping: useGrouping });
|
||||||
}).not.toThrow();
|
}).not.toThrow();
|
||||||
|
|
|
@ -287,9 +287,15 @@ describe("correct behavior", () => {
|
||||||
const en4 = new Intl.NumberFormat("en", { useGrouping: true });
|
const en4 = new Intl.NumberFormat("en", { useGrouping: true });
|
||||||
expect(en4.resolvedOptions().useGrouping).toBe("always");
|
expect(en4.resolvedOptions().useGrouping).toBe("always");
|
||||||
|
|
||||||
|
const en5 = new Intl.NumberFormat("en", { useGrouping: "false" });
|
||||||
|
expect(en5.resolvedOptions().useGrouping).toBe("auto");
|
||||||
|
|
||||||
|
const en6 = new Intl.NumberFormat("en", { useGrouping: "true" });
|
||||||
|
expect(en6.resolvedOptions().useGrouping).toBe("auto");
|
||||||
|
|
||||||
["auto", "always", "min2"].forEach(useGrouping => {
|
["auto", "always", "min2"].forEach(useGrouping => {
|
||||||
const en5 = new Intl.NumberFormat("en", { useGrouping: useGrouping });
|
const en7 = new Intl.NumberFormat("en", { useGrouping: useGrouping });
|
||||||
expect(en5.resolvedOptions().useGrouping).toBe(useGrouping);
|
expect(en7.resolvedOptions().useGrouping).toBe(useGrouping);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue