LibJS: Add notation to Intl.PluralRules

This is a normative change in the ECMA-402 spec. See:
a7ff535
This commit is contained in:
Timothy Flynn 2025-05-27 08:05:19 -04:00 committed by Tim Flynn
commit 8e5cc74eb1
Notes: github-actions[bot] 2025-05-27 14:40:25 +00:00
9 changed files with 101 additions and 29 deletions

View file

@ -117,6 +117,12 @@ describe("errors", () => {
}).toThrowWithMessage(RangeError, "Value 22 is NaN or is not between 1 and 21");
});
test("notation option is invalid ", () => {
expect(() => {
new Intl.PluralRules("en", { notation: "hello!" });
}).toThrowWithMessage(RangeError, "hello! is not a valid value for option notation");
});
test("roundingPriority option is invalid", () => {
expect(() => {
new Intl.PluralRules("en", { roundingPriority: "hello!" });
@ -239,6 +245,14 @@ describe("normal behavior", () => {
}
});
test("all valid notation options", () => {
["standard", "scientific", "engineering", "compact"].forEach(notation => {
expect(() => {
new Intl.PluralRules("en", { notation: notation });
}).not.toThrow();
});
});
test("all valid roundingPriority options", () => {
["auto", "morePrecision", "lessPrecision"].forEach(roundingPriority => {
expect(() => {

View file

@ -78,6 +78,16 @@ describe("correct behavior", () => {
expect(en3.resolvedOptions().maximumSignificantDigits).toBe(10);
});
test("notation", () => {
const en1 = new Intl.PluralRules("en");
expect(en1.resolvedOptions().notation).toBe("standard");
["standard", "scientific", "engineering", "compact"].forEach(notation => {
const en2 = new Intl.PluralRules("en", { notation: notation });
expect(en2.resolvedOptions().notation).toBe(notation);
});
});
test("plural categories", () => {
const enCardinal = new Intl.PluralRules("en", { type: "cardinal" }).resolvedOptions();
expect(enCardinal.pluralCategories).toEqual(["one", "other"]);

View file

@ -139,4 +139,25 @@ describe("correct behavior", () => {
expect(mk.select(27)).toBe("many");
expect(mk.select(28)).toBe("many");
});
test("notation", () => {
const standard = new Intl.PluralRules("fr", { notation: "standard" });
const engineering = new Intl.PluralRules("fr", { notation: "engineering" });
const scientific = new Intl.PluralRules("fr", { notation: "scientific" });
const compact = new Intl.PluralRules("fr", { notation: "compact" });
// prettier-ignore
const data = [
{ value: 1e6, standard: "many", engineering: "many", scientific: "many", compact: "many" },
{ value: 1.5e6, standard: "other", engineering: "many", scientific: "many", compact: "many" },
{ value: 1e-6, standard: "one", engineering: "many", scientific: "many", compact: "one" },
];
data.forEach(d => {
expect(standard.select(d.value)).toBe(d.standard);
expect(engineering.select(d.value)).toBe(d.engineering);
expect(scientific.select(d.value)).toBe(d.scientific);
expect(compact.select(d.value)).toBe(d.compact);
});
});
});