diff --git a/async/drop.ts b/async/drop.ts index e979d27..35246bc 100644 --- a/async/drop.ts +++ b/async/drop.ts @@ -20,10 +20,8 @@ export function drop( iterable: Iterable | AsyncIterable, limit: number, ): AsyncIterable { - if (limit < 0) { - throw new Error( - `limit argument must be greater than or equal to 0, but got ${limit}.`, - ); + if (limit < 0 || !Number.isSafeInteger(limit)) { + throw new DropLimitError(limit); } return async function* () { let i = 0; @@ -34,3 +32,12 @@ export function drop( } }(); } + +/** + * Error thrown when the 'limit' is negative or not a safe integer. + */ +export class DropLimitError extends Error { + constructor(limit: number) { + super(`The 'limit' must be 0 or positive safe integer, but got ${limit}.`); + } +} diff --git a/async/drop_test.ts b/async/drop_test.ts index 0223e2d..680faa5 100644 --- a/async/drop_test.ts +++ b/async/drop_test.ts @@ -2,7 +2,7 @@ import { assertEquals, assertThrows } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; import { toArray } from "./to_array.ts"; -import { drop } from "./drop.ts"; +import { drop, DropLimitError } from "./drop.ts"; Deno.test("drop", async (t) => { await t.step("with async iterable", async (t) => { @@ -18,8 +18,7 @@ Deno.test("drop", async (t) => { () => { drop([0, 1, 2, 3, 4], -2); }, - Error, - "limit argument must be greater than or equal to 0, but got -2.", + DropLimitError, ); }); @@ -44,8 +43,7 @@ Deno.test("drop", async (t) => { () => { drop([0, 1, 2, 3, 4], -2); }, - Error, - "limit argument must be greater than or equal to 0, but got -2.", + DropLimitError, ); }); diff --git a/async/take.ts b/async/take.ts index 6ee014d..5118d85 100644 --- a/async/take.ts +++ b/async/take.ts @@ -20,10 +20,8 @@ export function take( iterable: Iterable | AsyncIterable, limit: number, ): AsyncIterable { - if (limit < 0) { - throw new Error( - `limit argument must be greater than or equal to 0, but got ${limit}.`, - ); + if (limit < 0 || !Number.isSafeInteger(limit)) { + throw new TakeLimitError(limit); } return async function* () { let i = 0; @@ -35,3 +33,12 @@ export function take( } }(); } + +/** + * Error thrown when the 'limit' is negative or not a safe integer. + */ +export class TakeLimitError extends Error { + constructor(limit: number) { + super(`The 'limit' must be 0 or positive safe integer, but got ${limit}.`); + } +} diff --git a/async/take_test.ts b/async/take_test.ts index 1d0752e..d181ec8 100644 --- a/async/take_test.ts +++ b/async/take_test.ts @@ -2,7 +2,7 @@ import { assertEquals, assertThrows } from "@std/assert"; import { assertType, type IsExact } from "@std/testing/types"; import { toAsyncIterable } from "./to_async_iterable.ts"; import { toArray } from "./to_array.ts"; -import { take } from "./take.ts"; +import { take, TakeLimitError } from "./take.ts"; Deno.test("take", async (t) => { await t.step("with async iterable", async (t) => { @@ -18,8 +18,7 @@ Deno.test("take", async (t) => { () => { take([0, 1, 2, 3, 4], -2); }, - Error, - "limit argument must be greater than or equal to 0, but got -2.", + TakeLimitError, ); }); @@ -44,8 +43,7 @@ Deno.test("take", async (t) => { () => { take([0, 1, 2, 3, 4], -2); }, - Error, - "limit argument must be greater than or equal to 0, but got -2.", + TakeLimitError, ); });