Skip to content

Commit 7b1593b

Browse files
authored
Merge pull request #5 from jsr-core/fix-take-drop-on-async
feat: add `TakeLimitError` and `DropLimitError` on async
2 parents 4f964ad + 27bca81 commit 7b1593b

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

async/drop.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ export function drop<T>(
2020
iterable: Iterable<T> | AsyncIterable<T>,
2121
limit: number,
2222
): AsyncIterable<T> {
23-
if (limit < 0) {
24-
throw new Error(
25-
`limit argument must be greater than or equal to 0, but got ${limit}.`,
26-
);
23+
if (limit < 0 || !Number.isSafeInteger(limit)) {
24+
throw new DropLimitError(limit);
2725
}
2826
return async function* () {
2927
let i = 0;
@@ -34,3 +32,12 @@ export function drop<T>(
3432
}
3533
}();
3634
}
35+
36+
/**
37+
* Error thrown when the 'limit' is negative or not a safe integer.
38+
*/
39+
export class DropLimitError extends Error {
40+
constructor(limit: number) {
41+
super(`The 'limit' must be 0 or positive safe integer, but got ${limit}.`);
42+
}
43+
}

async/drop_test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assertEquals, assertThrows } from "@std/assert";
22
import { assertType, type IsExact } from "@std/testing/types";
33
import { toAsyncIterable } from "./to_async_iterable.ts";
44
import { toArray } from "./to_array.ts";
5-
import { drop } from "./drop.ts";
5+
import { drop, DropLimitError } from "./drop.ts";
66

77
Deno.test("drop", async (t) => {
88
await t.step("with async iterable", async (t) => {
@@ -18,8 +18,7 @@ Deno.test("drop", async (t) => {
1818
() => {
1919
drop([0, 1, 2, 3, 4], -2);
2020
},
21-
Error,
22-
"limit argument must be greater than or equal to 0, but got -2.",
21+
DropLimitError,
2322
);
2423
});
2524

@@ -44,8 +43,7 @@ Deno.test("drop", async (t) => {
4443
() => {
4544
drop([0, 1, 2, 3, 4], -2);
4645
},
47-
Error,
48-
"limit argument must be greater than or equal to 0, but got -2.",
46+
DropLimitError,
4947
);
5048
});
5149

async/take.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ export function take<T>(
2020
iterable: Iterable<T> | AsyncIterable<T>,
2121
limit: number,
2222
): AsyncIterable<T> {
23-
if (limit < 0) {
24-
throw new Error(
25-
`limit argument must be greater than or equal to 0, but got ${limit}.`,
26-
);
23+
if (limit < 0 || !Number.isSafeInteger(limit)) {
24+
throw new TakeLimitError(limit);
2725
}
2826
return async function* () {
2927
let i = 0;
@@ -35,3 +33,12 @@ export function take<T>(
3533
}
3634
}();
3735
}
36+
37+
/**
38+
* Error thrown when the 'limit' is negative or not a safe integer.
39+
*/
40+
export class TakeLimitError extends Error {
41+
constructor(limit: number) {
42+
super(`The 'limit' must be 0 or positive safe integer, but got ${limit}.`);
43+
}
44+
}

async/take_test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assertEquals, assertThrows } from "@std/assert";
22
import { assertType, type IsExact } from "@std/testing/types";
33
import { toAsyncIterable } from "./to_async_iterable.ts";
44
import { toArray } from "./to_array.ts";
5-
import { take } from "./take.ts";
5+
import { take, TakeLimitError } from "./take.ts";
66

77
Deno.test("take", async (t) => {
88
await t.step("with async iterable", async (t) => {
@@ -18,8 +18,7 @@ Deno.test("take", async (t) => {
1818
() => {
1919
take([0, 1, 2, 3, 4], -2);
2020
},
21-
Error,
22-
"limit argument must be greater than or equal to 0, but got -2.",
21+
TakeLimitError,
2322
);
2423
});
2524

@@ -44,8 +43,7 @@ Deno.test("take", async (t) => {
4443
() => {
4544
take([0, 1, 2, 3, 4], -2);
4645
},
47-
Error,
48-
"limit argument must be greater than or equal to 0, but got -2.",
46+
TakeLimitError,
4947
);
5048
});
5149

0 commit comments

Comments
 (0)