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

LibJS: Allow out-of-order number 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 06:55:25 -04:00 committed by Tim Flynn
parent 415742ab98
commit fd7d97fba5
4 changed files with 77 additions and 121 deletions

View file

@ -33,41 +33,6 @@ describe("errors", () => {
expect(() => {
new Intl.NumberFormat().formatRange(1, NaN);
}).toThrowWithMessage(RangeError, "end must not be NaN");
expect(() => {
new Intl.NumberFormat().formatRange(1, 0);
}).toThrowWithMessage(
RangeError,
"start is a mathematical value, end is a mathematical value and end < start"
);
expect(() => {
new Intl.NumberFormat().formatRange(1, -Infinity);
}).toThrowWithMessage(RangeError, "start is a mathematical value, end is -∞");
expect(() => {
new Intl.NumberFormat().formatRange(1, -0);
}).toThrowWithMessage(RangeError, "start is a mathematical value, end is -0 and start ≥ 0");
expect(() => {
new Intl.NumberFormat().formatRange(Infinity, 0);
}).toThrowWithMessage(RangeError, "start is +∞, end is a mathematical value");
expect(() => {
new Intl.NumberFormat().formatRange(Infinity, -Infinity);
}).toThrowWithMessage(RangeError, "start is +∞, end is -∞");
expect(() => {
new Intl.NumberFormat().formatRange(Infinity, -0);
}).toThrowWithMessage(RangeError, "start is +∞, end is -0");
expect(() => {
new Intl.NumberFormat().formatRange(-0, -1);
}).toThrowWithMessage(RangeError, "start is -0, end is a mathematical value and end < 0");
expect(() => {
new Intl.NumberFormat().formatRange(-0, -Infinity);
}).toThrowWithMessage(RangeError, "start is -0, end is -∞");
});
});
@ -137,4 +102,26 @@ describe("correct behavior", () => {
});
expect(ja2.formatRange(3, 5)).toBe("¥3 ¥5");
});
test("numbers in reverse order", () => {
const en = new Intl.NumberFormat("en");
expect(en.formatRange(1, 0)).toBe("10");
expect(en.formatRange(1, -Infinity)).toBe("1 -∞");
expect(en.formatRange(1, -0)).toBe("1 -0");
expect(en.formatRange(Infinity, 0)).toBe("∞ 0");
expect(en.formatRange(Infinity, -Infinity)).toBe("∞ -∞");
expect(en.formatRange(Infinity, -0)).toBe("∞ -0");
expect(en.formatRange(-0, -1)).toBe("-0 -1");
expect(en.formatRange(-0, -Infinity)).toBe("-0 -∞");
const ja = new Intl.NumberFormat("ja");
expect(ja.formatRange(1, 0)).toBe("10");
expect(ja.formatRange(1, -Infinity)).toBe("1 -∞");
expect(ja.formatRange(1, -0)).toBe("1 -0");
expect(ja.formatRange(Infinity, 0)).toBe("∞ 0");
expect(ja.formatRange(Infinity, -Infinity)).toBe("∞ -∞");
expect(ja.formatRange(Infinity, -0)).toBe("∞ -0");
expect(ja.formatRange(-0, -1)).toBe("-0 -1");
expect(ja.formatRange(-0, -Infinity)).toBe("-0 -∞");
});
});

View file

@ -33,41 +33,6 @@ describe("errors", () => {
expect(() => {
new Intl.NumberFormat().formatRangeToParts(1, NaN);
}).toThrowWithMessage(RangeError, "end must not be NaN");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(1, 0);
}).toThrowWithMessage(
RangeError,
"start is a mathematical value, end is a mathematical value and end < start"
);
expect(() => {
new Intl.NumberFormat().formatRangeToParts(1, -Infinity);
}).toThrowWithMessage(RangeError, "start is a mathematical value, end is -∞");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(1, -0);
}).toThrowWithMessage(RangeError, "start is a mathematical value, end is -0 and start ≥ 0");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(Infinity, 0);
}).toThrowWithMessage(RangeError, "start is +∞, end is a mathematical value");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(Infinity, -Infinity);
}).toThrowWithMessage(RangeError, "start is +∞, end is -∞");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(Infinity, -0);
}).toThrowWithMessage(RangeError, "start is +∞, end is -0");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(-0, -1);
}).toThrowWithMessage(RangeError, "start is -0, end is a mathematical value and end < 0");
expect(() => {
new Intl.NumberFormat().formatRangeToParts(-0, -Infinity);
}).toThrowWithMessage(RangeError, "start is -0, end is -∞");
});
});
@ -165,4 +130,48 @@ describe("correct behavior", () => {
{ type: "integer", value: "5", source: "endRange" },
]);
});
test("numbers in reverse order", () => {
const en = new Intl.NumberFormat("en");
expect(en.formatRangeToParts(1, -Infinity)).toEqual([
{ type: "integer", value: "1", source: "startRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "minusSign", value: "-", source: "endRange" },
{ type: "infinity", value: "∞", source: "endRange" },
]);
expect(en.formatRangeToParts(Infinity, -Infinity)).toEqual([
{ type: "infinity", value: "∞", source: "startRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "minusSign", value: "-", source: "endRange" },
{ type: "infinity", value: "∞", source: "endRange" },
]);
expect(en.formatRangeToParts(-0, -Infinity)).toEqual([
{ type: "minusSign", value: "-", source: "startRange" },
{ type: "integer", value: "0", source: "startRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "minusSign", value: "-", source: "endRange" },
{ type: "infinity", value: "∞", source: "endRange" },
]);
const ja = new Intl.NumberFormat("ja");
expect(ja.formatRangeToParts(1, -Infinity)).toEqual([
{ type: "integer", value: "1", source: "startRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "minusSign", value: "-", source: "endRange" },
{ type: "infinity", value: "∞", source: "endRange" },
]);
expect(ja.formatRangeToParts(Infinity, -Infinity)).toEqual([
{ type: "infinity", value: "∞", source: "startRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "minusSign", value: "-", source: "endRange" },
{ type: "infinity", value: "∞", source: "endRange" },
]);
expect(ja.formatRangeToParts(-0, -Infinity)).toEqual([
{ type: "minusSign", value: "-", source: "startRange" },
{ type: "integer", value: "0", source: "startRange" },
{ type: "literal", value: " ", source: "shared" },
{ type: "minusSign", value: "-", source: "endRange" },
{ type: "infinity", value: "∞", source: "endRange" },
]);
});
});