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

LibJS: Implement Intl.NumberFormat V3's [[UseGrouping]] changes

In the main spec, [[UseGrouping]] can be true or false. In V3, it may be
one of:

    auto: Respect the per-locale preference for grouping.

    always: Ignore per-locale preference for grouping and always insert
    the grouping separator (note: true is now an alias for always).

    min2: Ignore per-locale preference for grouping and only insert the
    grouping separator if the primary group has at least 2 digits.

    false: Ignore per-locale preference for grouping and never insert
    the grouping separator.
This commit is contained in:
Timothy Flynn 2022-07-12 14:30:30 -04:00 committed by Linus Groh
parent cd4ee46b70
commit 733192089f
3 changed files with 142 additions and 45 deletions

View file

@ -347,29 +347,89 @@ describe("style=decimal", () => {
expect(ar.format(-1)).toBe("\u061c-\u0661");
});
test("useGrouping=true", () => {
const en = new Intl.NumberFormat("en", { useGrouping: true });
test("useGrouping=always", () => {
const en = new Intl.NumberFormat("en", { useGrouping: "always" });
expect(en.format(123)).toBe("123");
expect(en.format(1234)).toBe("1,234");
expect(en.format(12345)).toBe("12,345");
expect(en.format(123456)).toBe("123,456");
expect(en.format(1234567)).toBe("1,234,567");
const enIn = new Intl.NumberFormat("en-IN", { useGrouping: true });
const enIn = new Intl.NumberFormat("en-IN", { useGrouping: "always" });
expect(enIn.format(123)).toBe("123");
expect(enIn.format(1234)).toBe("1,234");
expect(enIn.format(12345)).toBe("12,345");
expect(enIn.format(123456)).toBe("1,23,456");
expect(enIn.format(1234567)).toBe("12,34,567");
const ar = new Intl.NumberFormat("ar", { useGrouping: true });
const ar = new Intl.NumberFormat("ar", { useGrouping: "always" });
expect(ar.format(123)).toBe("\u0661\u0662\u0663");
expect(ar.format(1234)).toBe("\u0661\u066c\u0662\u0663\u0664");
expect(ar.format(12345)).toBe("\u0661\u0662\u066c\u0663\u0664\u0665");
expect(ar.format(123456)).toBe("\u0661\u0662\u0663\u066c\u0664\u0665\u0666");
expect(ar.format(1234567)).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0666\u0667");
const plPl = new Intl.NumberFormat("pl-PL", { useGrouping: true });
const plPl = new Intl.NumberFormat("pl-PL", { useGrouping: "always" });
expect(plPl.format(123)).toBe("123");
expect(plPl.format(1234)).toBe("1\u00a0234");
expect(plPl.format(12345)).toBe("12\u00a0345");
expect(plPl.format(123456)).toBe("123\u00a0456");
expect(plPl.format(1234567)).toBe("1\u00a0234\u00a0567");
});
test("useGrouping=auto", () => {
const en = new Intl.NumberFormat("en", { useGrouping: "auto" });
expect(en.format(123)).toBe("123");
expect(en.format(1234)).toBe("1,234");
expect(en.format(12345)).toBe("12,345");
expect(en.format(123456)).toBe("123,456");
expect(en.format(1234567)).toBe("1,234,567");
const enIn = new Intl.NumberFormat("en-IN", { useGrouping: "auto" });
expect(enIn.format(123)).toBe("123");
expect(enIn.format(1234)).toBe("1,234");
expect(enIn.format(12345)).toBe("12,345");
expect(enIn.format(123456)).toBe("1,23,456");
expect(enIn.format(1234567)).toBe("12,34,567");
const ar = new Intl.NumberFormat("ar", { useGrouping: "auto" });
expect(ar.format(123)).toBe("\u0661\u0662\u0663");
expect(ar.format(1234)).toBe("\u0661\u066c\u0662\u0663\u0664");
expect(ar.format(12345)).toBe("\u0661\u0662\u066c\u0663\u0664\u0665");
expect(ar.format(123456)).toBe("\u0661\u0662\u0663\u066c\u0664\u0665\u0666");
expect(ar.format(1234567)).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0666\u0667");
const plPl = new Intl.NumberFormat("pl-PL", { useGrouping: "auto" });
expect(plPl.format(123)).toBe("123");
expect(plPl.format(1234)).toBe("1234");
expect(plPl.format(12345)).toBe("12\u00a0345");
expect(plPl.format(123456)).toBe("123\u00a0456");
expect(plPl.format(1234567)).toBe("1\u00a0234\u00a0567");
});
test("useGrouping=min2", () => {
const en = new Intl.NumberFormat("en", { useGrouping: "min2" });
expect(en.format(123)).toBe("123");
expect(en.format(1234)).toBe("1234");
expect(en.format(12345)).toBe("12,345");
expect(en.format(123456)).toBe("123,456");
expect(en.format(1234567)).toBe("1,234,567");
const enIn = new Intl.NumberFormat("en-IN", { useGrouping: "min2" });
expect(enIn.format(123)).toBe("123");
expect(enIn.format(1234)).toBe("1234");
expect(enIn.format(12345)).toBe("12,345");
expect(enIn.format(123456)).toBe("1,23,456");
expect(enIn.format(1234567)).toBe("12,34,567");
const ar = new Intl.NumberFormat("ar", { useGrouping: "min2" });
expect(ar.format(123)).toBe("\u0661\u0662\u0663");
expect(ar.format(1234)).toBe("\u0661\u0662\u0663\u0664");
expect(ar.format(12345)).toBe("\u0661\u0662\u066c\u0663\u0664\u0665");
expect(ar.format(123456)).toBe("\u0661\u0662\u0663\u066c\u0664\u0665\u0666");
expect(ar.format(1234567)).toBe("\u0661\u066c\u0662\u0663\u0664\u066c\u0665\u0666\u0667");
const plPl = new Intl.NumberFormat("pl-PL", { useGrouping: "min2" });
expect(plPl.format(123)).toBe("123");
expect(plPl.format(1234)).toBe("1234");
expect(plPl.format(12345)).toBe("12\u00a0345");

View file

@ -171,8 +171,34 @@ describe("style=decimal", () => {
]);
});
test("useGrouping=true", () => {
const en = new Intl.NumberFormat("en", { useGrouping: true });
test("useGrouping=always", () => {
const en = new Intl.NumberFormat("en", { useGrouping: "always" });
expect(en.formatToParts(1234)).toEqual([
{ type: "integer", value: "1" },
{ type: "group", value: "," },
{ type: "integer", value: "234" },
]);
expect(en.formatToParts(12345)).toEqual([
{ type: "integer", value: "12" },
{ type: "group", value: "," },
{ type: "integer", value: "345" },
]);
const plPl = new Intl.NumberFormat("pl-PL", { useGrouping: "always" });
expect(plPl.formatToParts(1234)).toEqual([
{ type: "integer", value: "1" },
{ type: "group", value: "\u00a0" },
{ type: "integer", value: "234" },
]);
expect(plPl.formatToParts(12345)).toEqual([
{ type: "integer", value: "12" },
{ type: "group", value: "\u00a0" },
{ type: "integer", value: "345" },
]);
});
test("useGrouping=auto", () => {
const en = new Intl.NumberFormat("en", { useGrouping: "auto" });
expect(en.formatToParts(123456)).toEqual([
{ type: "integer", value: "123" },
{ type: "group", value: "," },
@ -186,7 +212,7 @@ describe("style=decimal", () => {
{ type: "integer", value: "567" },
]);
const enIn = new Intl.NumberFormat("en-IN", { useGrouping: true });
const enIn = new Intl.NumberFormat("en-IN", { useGrouping: "auto" });
expect(enIn.formatToParts(123456)).toEqual([
{ type: "integer", value: "1" },
{ type: "group", value: "," },
@ -202,7 +228,7 @@ describe("style=decimal", () => {
{ type: "integer", value: "567" },
]);
const ar = new Intl.NumberFormat("ar", { useGrouping: true });
const ar = new Intl.NumberFormat("ar", { useGrouping: "auto" });
expect(ar.formatToParts(123456)).toEqual([
{ type: "integer", value: "\u0661\u0662\u0663" },
{ type: "group", value: "\u066c" },
@ -217,6 +243,24 @@ describe("style=decimal", () => {
]);
});
test("useGrouping=min2", () => {
const en = new Intl.NumberFormat("en", { useGrouping: "min2" });
expect(en.formatToParts(1234)).toEqual([{ type: "integer", value: "1234" }]);
expect(en.formatToParts(12345)).toEqual([
{ type: "integer", value: "12" },
{ type: "group", value: "," },
{ type: "integer", value: "345" },
]);
const plPl = new Intl.NumberFormat("pl-PL", { useGrouping: "min2" });
expect(plPl.formatToParts(1234)).toEqual([{ type: "integer", value: "1234" }]);
expect(plPl.formatToParts(12345)).toEqual([
{ type: "integer", value: "12" },
{ type: "group", value: "\u00a0" },
{ type: "integer", value: "345" },
]);
});
test("useGrouping=false", () => {
const en = new Intl.NumberFormat("en", { useGrouping: false });
expect(en.formatToParts(123456)).toEqual([{ type: "integer", value: "123456" }]);