Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions async/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export function drop<T>(
iterable: Iterable<T> | AsyncIterable<T>,
limit: number,
): AsyncIterable<T> {
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;
Expand All @@ -34,3 +32,12 @@ export function drop<T>(
}
}();
}

/**
* 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}.`);
}
}
8 changes: 3 additions & 5 deletions async/drop_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
);
});

Expand All @@ -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,
);
});

Expand Down
15 changes: 11 additions & 4 deletions async/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ export function take<T>(
iterable: Iterable<T> | AsyncIterable<T>,
limit: number,
): AsyncIterable<T> {
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;
Expand All @@ -35,3 +33,12 @@ export function take<T>(
}
}();
}

/**
* 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}.`);
}
}
8 changes: 3 additions & 5 deletions async/take_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
);
});

Expand All @@ -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,
);
});

Expand Down