fix(collections): correct error class when chunk() throws (#4682)

refactor(collections): correct error class when `chunk()` throws
This commit is contained in:
Asher Gomez 2024-05-07 15:54:29 +10:00 committed by GitHub
parent 1a0907eb53
commit e7b2ca69ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 6 deletions

View File

@ -39,7 +39,7 @@
*/
export function chunk<T>(array: readonly T[], size: number): T[][] {
if (size <= 0 || !Number.isInteger(size)) {
throw new Error(
throw new RangeError(
`Expected size to be an integer greater than 0 but found ${size}`,
);
}

View File

@ -27,11 +27,31 @@ Deno.test({
Deno.test({
name: "chunk() throws on non naturals",
fn() {
assertThrows(() => chunk([], +.5));
assertThrows(() => chunk([], -4.7));
assertThrows(() => chunk([], -2));
assertThrows(() => chunk([], +0));
assertThrows(() => chunk([], -0));
assertThrows(
() => chunk([], +.5),
RangeError,
"Expected size to be an integer greater than 0 but found 0.5",
);
assertThrows(
() => chunk([], -4.7),
RangeError,
"Expected size to be an integer greater than 0 but found -4.7",
);
assertThrows(
() => chunk([], -2),
RangeError,
"Expected size to be an integer greater than 0 but found -2",
);
assertThrows(
() => chunk([], +0),
RangeError,
"Expected size to be an integer greater than 0 but found 0",
);
assertThrows(
() => chunk([], -0),
RangeError,
"Expected size to be an integer greater than 0 but found 0",
);
},
});