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

LibJS: Allow out-of-order plural ranges to be formatted

This is a normative change to the Intl NumberFormat V3 spec:
0c3d849
This commit is contained in:
Timothy Flynn 2022-07-26 07:12:46 -04:00 committed by Tim Flynn
parent fd7d97fba5
commit 417a385db1
3 changed files with 17 additions and 14 deletions

View file

@ -48,7 +48,6 @@
M(IntlInvalidRoundingIncrementForRoundingType, "{} is not a valid rounding increment for rounding type {}") \ M(IntlInvalidRoundingIncrementForRoundingType, "{} is not a valid rounding increment for rounding type {}") \
M(IntlInvalidTime, "Time value must be between -8.64E15 and 8.64E15") \ M(IntlInvalidTime, "Time value must be between -8.64E15 and 8.64E15") \
M(IntlInvalidUnit, "Unit {} is not a valid time unit") \ M(IntlInvalidUnit, "Unit {} is not a valid time unit") \
M(IntlStartRangeAfterEndRange, "Range start {} is greater than range end {}") \
M(IntlMinimumExceedsMaximum, "Minimum value {} is larger than maximum value {}") \ M(IntlMinimumExceedsMaximum, "Minimum value {} is larger than maximum value {}") \
M(IntlNumberIsNaN, "{} must not be NaN") \ M(IntlNumberIsNaN, "{} must not be NaN") \
M(IntlNumberIsNaNOrInfinity, "Number must not be NaN or Infinity") \ M(IntlNumberIsNaNOrInfinity, "Number must not be NaN or Infinity") \

View file

@ -152,23 +152,19 @@ ThrowCompletionOr<Unicode::PluralCategory> resolve_plural_range(GlobalObject& gl
if (end.is_nan()) if (end.is_nan())
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNumberIsNaN, "end"sv); return vm.throw_completion<RangeError>(global_object, ErrorType::IntlNumberIsNaN, "end"sv);
// 6. If x > y, throw a RangeError exception. // 6. Let xp be ! ResolvePlural(pluralRules, x).
if (start.as_double() > end.as_double())
return vm.throw_completion<RangeError>(global_object, ErrorType::IntlStartRangeAfterEndRange, start, end);
// 7. Let xp be ! ResolvePlural(pluralRules, x).
auto start_plurality = resolve_plural(plural_rules, start); auto start_plurality = resolve_plural(plural_rules, start);
// 8. Let yp be ! ResolvePlural(pluralRules, y). // 7. Let yp be ! ResolvePlural(pluralRules, y).
auto end_plurality = resolve_plural(plural_rules, end); auto end_plurality = resolve_plural(plural_rules, end);
// 9. Let locale be pluralRules.[[Locale]]. // 8. Let locale be pluralRules.[[Locale]].
auto const& locale = plural_rules.locale(); auto const& locale = plural_rules.locale();
// 10. Let type be pluralRules.[[Type]]. // 9. Let type be pluralRules.[[Type]].
auto type = plural_rules.type(); auto type = plural_rules.type();
// 11. Return ! PluralRuleSelectRange(locale, type, xp, yp). // 10. Return ! PluralRuleSelectRange(locale, type, xp, yp).
return plural_rule_select_range(locale, type, start_plurality, end_plurality); return plural_rule_select_range(locale, type, start_plurality, end_plurality);
} }

View file

@ -33,10 +33,6 @@ describe("errors", () => {
expect(() => { expect(() => {
new Intl.PluralRules().selectRange(1, NaN); new Intl.PluralRules().selectRange(1, NaN);
}).toThrowWithMessage(RangeError, "end must not be NaN"); }).toThrowWithMessage(RangeError, "end must not be NaN");
expect(() => {
new Intl.PluralRules().selectRange(1, 0);
}).toThrowWithMessage(RangeError, "Range start 1 is greater than range end 0");
}); });
}); });
@ -70,4 +66,16 @@ describe("correct behavior", () => {
expect(so.selectRange(0, 1)).toBe("one"); expect(so.selectRange(0, 1)).toBe("one");
expect(so.selectRange(1, 2)).toBe("other"); expect(so.selectRange(1, 2)).toBe("other");
}); });
test("numbers in reverse order", () => {
const en = new Intl.PluralRules("en");
expect(en.selectRange(1, -Infinity)).toBe("other");
expect(en.selectRange(Infinity, -Infinity)).toBe("other");
expect(en.selectRange(-0, -Infinity)).toBe("other");
const ja = new Intl.PluralRules("ja");
expect(ja.selectRange(1, -Infinity)).toBe("other");
expect(ja.selectRange(Infinity, -Infinity)).toBe("other");
expect(ja.selectRange(-0, -Infinity)).toBe("other");
});
}); });