From 709f27f09b43038635ca59bb2ab190655c1b41dd Mon Sep 17 00:00:00 2001 From: Alisue Date: Thu, 22 Aug 2024 05:47:09 +0900 Subject: [PATCH] feat: add `alter` and `alterElse` functions --- README.md | 24 ++++++++++++++++++++++++ alter.ts | 23 +++++++++++++++++++++++ alter_else.ts | 23 +++++++++++++++++++++++ alter_else_test.ts | 16 ++++++++++++++++ alter_test.ts | 16 ++++++++++++++++ deno.jsonc | 4 ++++ 6 files changed, 106 insertions(+) create mode 100644 alter.ts create mode 100644 alter_else.ts create mode 100644 alter_else_test.ts create mode 100644 alter_test.ts diff --git a/README.md b/README.md index e5ba441..6d6951e 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,30 @@ A utility pack for handling error. +## alter / alterElse + +`alter` and `alterElse` are functions that execute a function and return the +result. If the function throws an error, `alter` throws the given error, and +`alterElse` throws the result of the second function. + +```ts +import { assertThrows } from "@std/assert"; +import { alter } from "@core/errorutil/alter"; +import { alterElse } from "@core/errorutil/alter-else"; + +const fn = () => { + throw new Error("This is an error message"); +}; + +assertThrows(() => alter(fn, new Error("custom error")), Error, "custom error"); + +assertThrows( + () => alterElse(fn, () => new Error("custom error")), + Error, + "custom error", +); +``` + ## attempt `attempt` is a function that executes a function and returns the result diff --git a/alter.ts b/alter.ts new file mode 100644 index 0000000..b5f3d69 --- /dev/null +++ b/alter.ts @@ -0,0 +1,23 @@ +/** + * Alter the error of a function if an error is thrown. + * + * @param fn - The function to execute. + * @param alt - The value to throw if an error is thrown. + * @returns The result of the function. + * @throws The value of alt. + * @example + * + * ```ts + * import { alter } from "@core/errorutil/alter"; + * + * console.log(alter(() => 1, "err2")); // 1 + * console.log(alter(() => { throw "err1" }, "err2")); // "err2" is thrown + * ``` + */ +export function alter(fn: () => T, alt: E): T { + try { + return fn(); + } catch { + throw alt; + } +} diff --git a/alter_else.ts b/alter_else.ts new file mode 100644 index 0000000..9c6219c --- /dev/null +++ b/alter_else.ts @@ -0,0 +1,23 @@ +/** + * Alter the error of a function if an error is thrown. + * + * @param fn - The function to execute. + * @param modifier - The function to execute if an error is thrown. + * @returns The result of the function. + * @throws The result of the modifier function. + * @example + * + * ```ts + * import { alterElse } from "@core/errorutil/alter-else"; + * + * console.log(alterElse(() => 1, () => "err")); // 1 + * console.log(alterElse(() => { throw "err" }, (err) => "new " + err)); // "new err" is thrown + * ``` + */ +export function alterElse(fn: () => T, elseFn: (err: unknown) => E): T { + try { + return fn(); + } catch (err) { + throw elseFn(err); + } +} diff --git a/alter_else_test.ts b/alter_else_test.ts new file mode 100644 index 0000000..deca88b --- /dev/null +++ b/alter_else_test.ts @@ -0,0 +1,16 @@ +import { test } from "@cross/test"; +import { assertEquals, assertThrows } from "@std/assert"; +import { raise } from "./raise.ts"; +import { alterElse } from "./alter_else.ts"; + +test("alterElse should return a function result", () => { + assertEquals(alterElse(() => 1, () => "err2"), 1); +}); + +test("alterElse should throws an alt when the function throws error", () => { + assertThrows( + () => alterElse(() => raise("err"), (err) => new Error(`new ${err}`)), + Error, + "new err", + ); +}); diff --git a/alter_test.ts b/alter_test.ts new file mode 100644 index 0000000..db09021 --- /dev/null +++ b/alter_test.ts @@ -0,0 +1,16 @@ +import { test } from "@cross/test"; +import { assertEquals, assertThrows } from "@std/assert"; +import { raise } from "./raise.ts"; +import { alter } from "./alter.ts"; + +test("alter should return a function result", () => { + assertEquals(alter(() => 1, "err2"), 1); +}); + +test("alter should throws an alt when the function throws error", () => { + assertThrows( + () => alter(() => raise("err1"), new Error("err2")), + Error, + "err2", + ); +}); diff --git a/deno.jsonc b/deno.jsonc index 2acedf8..8512103 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -3,6 +3,8 @@ "version": "0.0.0", "exports": { ".": "./mod.ts", + "./alter": "./alter.ts", + "./alter-else": "./alter_else.ts", "./attempt": "./attempt.ts", "./error-object": "./error_object.ts", "./raise": "./raise.ts", @@ -28,6 +30,8 @@ }, "imports": { "@core/errorutil": "./mod.ts", + "@core/errorutil/alter": "./alter.ts", + "@core/errorutil/alter-else": "./alter_else.ts", "@core/errorutil/attempt": "./attempt.ts", "@core/errorutil/error-object": "./error_object.ts", "@core/errorutil/raise": "./raise.ts",