From 67c750d0a782d6728f69feb3f7c64119f6c99069 Mon Sep 17 00:00:00 2001 From: Alisue Date: Thu, 8 Aug 2024 11:21:47 +0900 Subject: [PATCH] feat: add `TakeLimitError` and `DropLimitError` --- drop.ts | 15 +++++++++++---- drop_test.ts | 5 ++--- take.ts | 15 +++++++++++---- take_test.ts | 5 ++--- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/drop.ts b/drop.ts index 6f04fef..beb24a5 100644 --- a/drop.ts +++ b/drop.ts @@ -16,10 +16,8 @@ * ``` */ export function drop(iterable: Iterable, limit: number): Iterable { - 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; @@ -30,3 +28,12 @@ export function drop(iterable: Iterable, limit: number): Iterable { } }(); } + +/** + * 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/drop_test.ts b/drop_test.ts index f8d650a..42711a0 100644 --- a/drop_test.ts +++ b/drop_test.ts @@ -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", () => { @@ -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, ); }); diff --git a/take.ts b/take.ts index aaa1153..7cdd895 100644 --- a/take.ts +++ b/take.ts @@ -16,10 +16,8 @@ * ``` */ export function take(iterable: Iterable, limit: number): Iterable { - 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; @@ -31,3 +29,12 @@ export function take(iterable: Iterable, limit: number): Iterable { } }(); } + +/** + * 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/take_test.ts b/take_test.ts index 8a22c85..e1ede62 100644 --- a/take_test.ts +++ b/take_test.ts @@ -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", () => { @@ -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, ); });