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

LibJS: Implement compact formatting for Intl.NumberFormat

This commit is contained in:
Timothy Flynn 2021-11-15 11:07:41 -05:00 committed by Linus Groh
parent 80b86d20dc
commit fdae323401
3 changed files with 117 additions and 12 deletions

View file

@ -205,6 +205,54 @@ describe("style=decimal", () => {
expect(ar.format(12.34)).toBe("\u0661\u0662");
});
test("notation=compact, compactDisplay=long", () => {
const en = new Intl.NumberFormat("en", { notation: "compact", compactDisplay: "long" });
expect(en.format(1)).toBe("1");
expect(en.format(1200)).toBe("1.2 thousand");
expect(en.format(1290)).toBe("1.3 thousand");
expect(en.format(12000)).toBe("12 thousand");
expect(en.format(12900)).toBe("13 thousand");
expect(en.format(1200000)).toBe("1.2 million");
expect(en.format(1290000)).toBe("1.3 million");
expect(en.format(12000000)).toBe("12 million");
expect(en.format(12900000)).toBe("13 million");
const ar = new Intl.NumberFormat("ar", { notation: "compact", compactDisplay: "long" });
expect(ar.format(1)).toBe("\u0661");
expect(ar.format(1200)).toBe("\u0661\u066b\u0662 ألف");
expect(ar.format(1290)).toBe("\u0661\u066b\u0663 ألف");
expect(ar.format(12000)).toBe("\u0661\u0662 ألف");
expect(ar.format(12900)).toBe("\u0661\u0663 ألف");
expect(ar.format(1200000)).toBe("\u0661\u066b\u0662 مليون");
expect(ar.format(1290000)).toBe("\u0661\u066b\u0663 مليون");
expect(ar.format(12000000)).toBe("\u0661\u0662 مليون");
expect(ar.format(12900000)).toBe("\u0661\u0663 مليون");
});
test("notation=compact, compactDisplay=short", () => {
const en = new Intl.NumberFormat("en", { notation: "compact", compactDisplay: "short" });
expect(en.format(1)).toBe("1");
expect(en.format(1200)).toBe("1.2K");
expect(en.format(1290)).toBe("1.3K");
expect(en.format(12000)).toBe("12K");
expect(en.format(12900)).toBe("13K");
expect(en.format(1200000)).toBe("1.2M");
expect(en.format(1290000)).toBe("1.3M");
expect(en.format(12000000)).toBe("12M");
expect(en.format(12900000)).toBe("13M");
const ar = new Intl.NumberFormat("ar", { notation: "compact", compactDisplay: "short" });
expect(ar.format(1)).toBe("\u0661");
expect(ar.format(1200)).toBe("\u0661\u066b\u0662\u00a0ألف");
expect(ar.format(1290)).toBe("\u0661\u066b\u0663\u00a0ألف");
expect(ar.format(12000)).toBe("\u0661\u0662\u00a0ألف");
expect(ar.format(12900)).toBe("\u0661\u0663\u00a0ألف");
expect(ar.format(1200000)).toBe("\u0661\u066b\u0662\u00a0مليون");
expect(ar.format(1290000)).toBe("\u0661\u066b\u0663\u00a0مليون");
expect(ar.format(12000000)).toBe("\u0661\u0662\u00a0مليون");
expect(ar.format(12900000)).toBe("\u0661\u0663\u00a0مليون");
});
test("signDisplay=never", () => {
const en = new Intl.NumberFormat("en", { signDisplay: "never" });
expect(en.format(1)).toBe("1");

View file

@ -312,6 +312,64 @@ describe("style=decimal", () => {
{ type: "exponentInteger", value: "\u0663" },
]);
});
test("notation=compact, compactDisplay=long", () => {
const en = new Intl.NumberFormat("en", { notation: "compact", compactDisplay: "long" });
expect(en.formatToParts(1200)).toEqual([
{ type: "integer", value: "1" },
{ type: "decimal", value: "." },
{ type: "fraction", value: "2" },
{ type: "literal", value: " " },
{ type: "compact", value: "thousand" },
]);
expect(en.formatToParts(12900000)).toEqual([
{ type: "integer", value: "13" },
{ type: "literal", value: " " },
{ type: "compact", value: "million" },
]);
const ar = new Intl.NumberFormat("ar", { notation: "compact", compactDisplay: "long" });
expect(ar.formatToParts(1200)).toEqual([
{ type: "integer", value: "\u0661" },
{ type: "decimal", value: "\u066b" },
{ type: "fraction", value: "\u0662" },
{ type: "literal", value: " " },
{ type: "compact", value: "ألف" },
]);
expect(ar.formatToParts(12900000)).toEqual([
{ type: "integer", value: "\u0661\u0663" },
{ type: "literal", value: " " },
{ type: "compact", value: "مليون" },
]);
});
test("notation=compact, compactDisplay=short", () => {
const en = new Intl.NumberFormat("en", { notation: "compact", compactDisplay: "short" });
expect(en.formatToParts(1200)).toEqual([
{ type: "integer", value: "1" },
{ type: "decimal", value: "." },
{ type: "fraction", value: "2" },
{ type: "compact", value: "K" },
]);
expect(en.formatToParts(12900000)).toEqual([
{ type: "integer", value: "13" },
{ type: "compact", value: "M" },
]);
const ar = new Intl.NumberFormat("ar", { notation: "compact", compactDisplay: "short" });
expect(ar.formatToParts(1200)).toEqual([
{ type: "integer", value: "\u0661" },
{ type: "decimal", value: "\u066b" },
{ type: "fraction", value: "\u0662" },
{ type: "literal", value: "\u00a0" },
{ type: "compact", value: "ألف" },
]);
expect(ar.formatToParts(12900000)).toEqual([
{ type: "integer", value: "\u0661\u0663" },
{ type: "literal", value: "\u00a0" },
{ type: "compact", value: "مليون" },
]);
});
});
describe("style=percent", () => {