Skip to content

Commit 67c750d

Browse files
committed
feat: add TakeLimitError and DropLimitError
1 parent 4a4d1bb commit 67c750d

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

drop.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
* ```
1717
*/
1818
export function drop<T>(iterable: Iterable<T>, limit: number): Iterable<T> {
19-
if (limit < 0) {
20-
throw new Error(
21-
`limit argument must be greater than or equal to 0, but got ${limit}.`,
22-
);
19+
if (limit < 0 || !Number.isSafeInteger(limit)) {
20+
throw new DropLimitError(limit);
2321
}
2422
return function* () {
2523
let i = 0;
@@ -30,3 +28,12 @@ export function drop<T>(iterable: Iterable<T>, limit: number): Iterable<T> {
3028
}
3129
}();
3230
}
31+
32+
/**
33+
* Error thrown when the 'limit' is negative or not a safe integer.
34+
*/
35+
export class DropLimitError extends Error {
36+
constructor(limit: number) {
37+
super(`The 'limit' must be 0 or positive safe integer, but got ${limit}.`);
38+
}
39+
}

drop_test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals, assertThrows } from "@std/assert";
22
import { assertType, type IsExact } from "@std/testing/types";
3-
import { drop } from "./drop.ts";
3+
import { drop, DropLimitError } from "./drop.ts";
44

55
Deno.test("drop", async (t) => {
66
await t.step("with positive limit", () => {
@@ -15,8 +15,7 @@ Deno.test("drop", async (t) => {
1515
() => {
1616
drop([0, 1, 2, 3, 4], -2);
1717
},
18-
Error,
19-
"limit argument must be greater than or equal to 0, but got -2.",
18+
DropLimitError,
2019
);
2120
});
2221

take.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
* ```
1717
*/
1818
export function take<T>(iterable: Iterable<T>, limit: number): Iterable<T> {
19-
if (limit < 0) {
20-
throw new Error(
21-
`limit argument must be greater than or equal to 0, but got ${limit}.`,
22-
);
19+
if (limit < 0 || !Number.isSafeInteger(limit)) {
20+
throw new TakeLimitError(limit);
2321
}
2422
return function* () {
2523
let i = 0;
@@ -31,3 +29,12 @@ export function take<T>(iterable: Iterable<T>, limit: number): Iterable<T> {
3129
}
3230
}();
3331
}
32+
33+
/**
34+
* Error thrown when the 'limit' is negative or not a safe integer.
35+
*/
36+
export class TakeLimitError extends Error {
37+
constructor(limit: number) {
38+
super(`The 'limit' must be 0 or positive safe integer, but got ${limit}.`);
39+
}
40+
}

take_test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assertEquals, assertThrows } from "@std/assert";
22
import { assertType, type IsExact } from "@std/testing/types";
3-
import { take } from "./take.ts";
3+
import { take, TakeLimitError } from "./take.ts";
44

55
Deno.test("take", async (t) => {
66
await t.step("with positive limit", () => {
@@ -15,8 +15,7 @@ Deno.test("take", async (t) => {
1515
() => {
1616
take([0, 1, 2, 3, 4], -2);
1717
},
18-
Error,
19-
"limit argument must be greater than or equal to 0, but got -2.",
18+
TakeLimitError,
2019
);
2120
});
2221

0 commit comments

Comments
 (0)