mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 15:48:11 +00:00
Spreadsheet: Allow integer() to throw in weird cases
Also makes numericResolve use parseInt to avoid using our own integer()
This commit is contained in:
parent
75a02300ba
commit
4147b56e79
2 changed files with 37 additions and 1 deletions
|
@ -365,6 +365,9 @@ function randRange(min, max) {
|
|||
}
|
||||
|
||||
function integer(value) {
|
||||
const typeVal = typeof value;
|
||||
if ((typeVal !== "number" && typeVal !== "string") || Number.isNaN(Number(value)))
|
||||
throw new Error(`integer() called with unexpected type "${typeVal}"`);
|
||||
return value | 0;
|
||||
}
|
||||
|
||||
|
@ -381,7 +384,7 @@ function numericReduce(op, accumulator, cells) {
|
|||
}
|
||||
|
||||
function numericResolve(cells) {
|
||||
return resolve(cells).map(str => (str === "" || str == null ? NaN : integer(str)));
|
||||
return resolve(cells).map(val => parseInt(val));
|
||||
}
|
||||
|
||||
function resolve(cells) {
|
||||
|
|
|
@ -203,3 +203,36 @@ describe("Lookup", () => {
|
|||
expect(reflookup("80", R`A0:A9`, R`B0:B9`, undefined, "nextlargest").name).toEqual("B9");
|
||||
});
|
||||
});
|
||||
|
||||
describe("integer() function", () => {
|
||||
test("undefined", () => {
|
||||
expect(() => integer(undefined)).toThrow(Error);
|
||||
});
|
||||
test("null", () => {
|
||||
expect(() => integer(null)).toThrow(Error);
|
||||
});
|
||||
test("NaN", () => {
|
||||
expect(() => integer(NaN)).toThrow(Error);
|
||||
});
|
||||
test("object", () => {
|
||||
expect(() => integer({})).toThrow(Error);
|
||||
});
|
||||
test("function", () => {
|
||||
expect(() => integer(() => {})).toThrow(Error);
|
||||
});
|
||||
test("try 1 as string", () => {
|
||||
expect(integer("1")).toBe(1);
|
||||
});
|
||||
test("try 1 as number", () => {
|
||||
expect(integer(1)).toBe(1);
|
||||
});
|
||||
test("try 1000000 as string", () => {
|
||||
expect(integer("1000000")).toBe(1000000);
|
||||
});
|
||||
test("try 1000000 as number", () => {
|
||||
expect(integer(1000000)).toBe(1000000);
|
||||
});
|
||||
test("don't just allow any strings", () => {
|
||||
expect(() => integer("is this NaN yet?")).toThrow();
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue