Skip to content

Commit

Permalink
Merge pull request #4 from jsr-core/improve-errors
Browse files Browse the repository at this point in the history
feat: add `TakeLimitError` and `DropLimitError`
  • Loading branch information
lambdalisue authored Aug 8, 2024
2 parents 4a4d1bb + 67c750d commit 4f964ad
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
15 changes: 11 additions & 4 deletions drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
* ```
*/
export function drop<T>(iterable: Iterable<T>, limit: number): Iterable<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 function* () {
let i = 0;
Expand All @@ -30,3 +28,12 @@ export function drop<T>(iterable: Iterable<T>, limit: number): Iterable<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}.`);
}
}
5 changes: 2 additions & 3 deletions drop_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals, assertThrows } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { drop } from "./drop.ts";
import { drop, DropLimitError } from "./drop.ts";

Deno.test("drop", async (t) => {
await t.step("with positive limit", () => {
Expand All @@ -15,8 +15,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 take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
* ```
*/
export function take<T>(iterable: Iterable<T>, limit: number): Iterable<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 function* () {
let i = 0;
Expand All @@ -31,3 +29,12 @@ export function take<T>(iterable: Iterable<T>, limit: number): Iterable<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}.`);
}
}
5 changes: 2 additions & 3 deletions take_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertEquals, assertThrows } from "@std/assert";
import { assertType, type IsExact } from "@std/testing/types";
import { take } from "./take.ts";
import { take, TakeLimitError } from "./take.ts";

Deno.test("take", async (t) => {
await t.step("with positive limit", () => {
Expand All @@ -15,8 +15,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

0 comments on commit 4f964ad

Please sign in to comment.