From 6a6ac25b0508a3dbc33e486f9153d2ce7352d3b8 Mon Sep 17 00:00:00 2001 From: Alisue Date: Tue, 14 May 2024 11:15:05 +0900 Subject: [PATCH 1/2] :bug: Add version constraints --- error_object.ts | 2 +- error_object_test.ts | 2 +- raise_test.ts | 2 +- try_or_test.ts | 6 +++++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/error_object.ts b/error_object.ts index a716608..6cd9727 100644 --- a/error_object.ts +++ b/error_object.ts @@ -1,4 +1,4 @@ -import { is } from "jsr:@core/unknownutil"; +import { is } from "jsr:@core/unknownutil@3.18.0"; export type ErrorObject = { proto: string; diff --git a/error_object_test.ts b/error_object_test.ts index 7e1d051..36c913d 100644 --- a/error_object_test.ts +++ b/error_object_test.ts @@ -1,4 +1,4 @@ -import { assertEquals, assertInstanceOf } from "jsr:@std/assert"; +import { assertEquals, assertInstanceOf } from "jsr:@std/assert@0.225.1"; import { fromErrorObject, toErrorObject } from "./error_object.ts"; class CustomError extends Error { diff --git a/raise_test.ts b/raise_test.ts index 3aade56..6bd7e85 100644 --- a/raise_test.ts +++ b/raise_test.ts @@ -1,4 +1,4 @@ -import { assertThrows } from "jsr:@std/assert"; +import { assertThrows } from "jsr:@std/assert@0.225.1"; import { raise } from "./raise.ts"; Deno.test("raise", () => { diff --git a/try_or_test.ts b/try_or_test.ts index abd6f47..8c13cb8 100644 --- a/try_or_test.ts +++ b/try_or_test.ts @@ -1,4 +1,8 @@ -import { assertEquals, assertRejects, assertThrows } from "jsr:@std/assert"; +import { + assertEquals, + assertRejects, + assertThrows, +} from "jsr:@std/assert@0.225.1"; import { raise } from "./raise.ts"; import { tryOr, tryOrElse } from "./try_or.ts"; From 8dcdd302679c7a156798e61d22b29870f40c2800 Mon Sep 17 00:00:00 2001 From: Alisue Date: Tue, 14 May 2024 11:21:59 +0900 Subject: [PATCH 2/2] :memo: Deprecate packages on deno.land or npm --- README.md | 18 ++++++++++--- deno.jsonc | 18 +++++++++++-- mod.ts | 1 + raise.ts | 2 +- try_or.ts | 41 +++-------------------------- try_or_else.ts | 35 +++++++++++++++++++++++++ try_or_else_test.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++ try_or_test.ts | 63 ++------------------------------------------- unimplemented.ts | 2 +- unreachable.ts | 4 +-- 10 files changed, 137 insertions(+), 109 deletions(-) create mode 100644 try_or_else.ts create mode 100644 try_or_else_test.ts diff --git a/README.md b/README.md index 1c8628d..4967a52 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,27 @@ # errorutil [![JSR](https://jsr.io/badges/@lambdalisue/errorutil)](https://jsr.io/@lambdalisue/errorutil) -[![denoland](https://img.shields.io/github/v/release/lambdalisue/deno-errorutil?logo=deno&label=denoland)](https://deno.land/x/errorutil) -[![npm](http://img.shields.io/badge/available%20on-npm-lightgrey.svg?logo=npm&logoColor=white)](https://www.npmjs.com/package/@lambdalisue/errorutil) -[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/errorutil/mod.ts) [![Test](https://github.com/lambdalisue/deno-errorutil/workflows/Test/badge.svg)](https://github.com/lambdalisue/deno-errorutil/actions?query=workflow%3ATest) -[![npm version](https://badge.fury.io/js/@lambdalisue%2Ferrorutil.svg)](https://badge.fury.io/js/@lambdalisue%2Ferrorutil) [![codecov](https://codecov.io/gh/lambdalisue/deno-errorutil/graph/badge.svg?token=O0cA5Xj5la)](https://codecov.io/gh/lambdalisue/deno-errorutil) A utility pack for handling error. [deno]: https://deno.land/ +> [!WARN] +> +> The package on [deno.land] and [npm] is deprecated. Use the package on +> [jsr.io] instead. +> +> ``` +> $ deno add @lambdalisue/errorutil +> $ npx jsr add @lambdalisue/errorutil +> ``` + +[deno.land]: https://deno.land/x/errorutil +[npm]: https://www.npmjs.com/package/@lambdalisue/errorutil +[jsr.io]: https://jsr.io/@lambdalisue/errorutil + ## License The code follows MIT license written in [LICENSE](./LICENSE). Contributors need diff --git a/deno.jsonc b/deno.jsonc index 160e050..0de0a04 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -1,7 +1,15 @@ { "name": "@lambdalisue/errorutil", "version": "0.0.0", - "exports": "./mod.ts", + "exports": { + ".": "./mod.ts", + "./error-object": "./error_object.ts", + "./raise": "./raise.ts", + "./try-or": "./try_or.ts", + "./try-or-else": "./try_or_else.ts", + "./unimplemented": "./unimplemented.ts", + "./unreachable": "./unreachable.ts" + }, "exclude": [ ".coverage", "npm" @@ -16,6 +24,12 @@ "update:commit": "deno task -q update --commit --pre-commit=fmt,lint" }, "imports": { - "https://deno.land/x/errorutil@$MODULE_VERSION/": "./" + "@lambdalisue/errorutil": "./mod.ts", + "@lambdalisue/errorutil/error-object": "./error_object.ts", + "@lambdalisue/errorutil/raise": "./raise.ts", + "@lambdalisue/errorutil/try-or": "./try_or.ts", + "@lambdalisue/errorutil/try-or-else": "./try_or_else.ts", + "@lambdalisue/errorutil/unimplemented": "./unimplemented.ts", + "@lambdalisue/errorutil/unreachable": "./unreachable.ts" } } diff --git a/mod.ts b/mod.ts index 2de42d5..ef4c1a0 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,6 @@ export * from "./error_object.ts"; export * from "./raise.ts"; export * from "./try_or.ts"; +export * from "./try_or_else.ts"; export * from "./unimplemented.ts"; export * from "./unreachable.ts"; diff --git a/raise.ts b/raise.ts index b67923e..5b1640d 100644 --- a/raise.ts +++ b/raise.ts @@ -4,7 +4,7 @@ * This is function thus can be used as an expression. * * ```typescript - * import { raise } from "https://deno.land/x/errorutil@$MODULE_VERSION/raise.ts"; + * import { raise } from "@lambdalisue/errorutil/raise"; * * const fn = () => raise(new Error("fail")); * ``` diff --git a/try_or.ts b/try_or.ts index f413a60..80d10ec 100644 --- a/try_or.ts +++ b/try_or.ts @@ -1,9 +1,10 @@ +import { tryOrElse } from "./try_or_else.ts"; /** * Try to execute a function and return the result or a default value. * * ```ts - * import { tryOr } from "https://deno.land/x/errorutil@$MODULE_VERSION/try_or.ts"; - * import { raise } from "https://deno.land/x/errorutil@$MODULE_VERSION/raise.ts"; + * import { tryOr } from "@lambdalisue/errorutil/try-or"; + * import { raise } from "@lambdalisue/errorutil/raise"; * * // Sync * console.log(tryOr(() => 1, 2)); // 1 @@ -25,39 +26,3 @@ export function tryOr( ): T | Promise { return tryOrElse(fn, () => orValue); } - -/** - * Try to execute a function and return the result or execute another function. - * - * ```ts - * import { tryOrElse } from "https://deno.land/x/errorutil@$MODULE_VERSION/try_or.ts"; - * import { raise } from "https://deno.land/x/errorutil@$MODULE_VERSION/raise.ts"; - * - * // Sync - * console.log(tryOrElse(() => 1, () => 2)); // 1 - * console.log(tryOrElse(() => raise("err"), () => 2)); // 2 - * - * // Async - * console.log(await tryOrElse(() => Promise.resolve(1), () => 2)); // 1 - * console.log(await tryOrElse(() => Promise.reject("err"), () => 2)); // 2 - * ``` - */ -export function tryOrElse(fn: () => T, elseFn: (err: unknown) => T): T; -export function tryOrElse( - fn: () => Promise, - elseFn: (err: unknown) => T | Promise, -): Promise; -export function tryOrElse( - fn: () => T | Promise, - elseFn: (err: unknown) => T | Promise, -): T | Promise { - try { - const ret = fn(); - if (ret instanceof Promise) { - return ret.catch((err) => elseFn(err)); - } - return ret; - } catch (err) { - return elseFn(err); - } -} diff --git a/try_or_else.ts b/try_or_else.ts new file mode 100644 index 0000000..fd425e7 --- /dev/null +++ b/try_or_else.ts @@ -0,0 +1,35 @@ +/** + * Try to execute a function and return the result or execute another function. + * + * ```ts + * import { tryOrElse } from "@lambdalisue/errorutil/try-or-else"; + * import { raise } from "@lambdalisue/errorutil/raise"; + * + * // Sync + * console.log(tryOrElse(() => 1, () => 2)); // 1 + * console.log(tryOrElse(() => raise("err"), () => 2)); // 2 + * + * // Async + * console.log(await tryOrElse(() => Promise.resolve(1), () => 2)); // 1 + * console.log(await tryOrElse(() => Promise.reject("err"), () => 2)); // 2 + * ``` + */ +export function tryOrElse(fn: () => T, elseFn: (err: unknown) => T): T; +export function tryOrElse( + fn: () => Promise, + elseFn: (err: unknown) => T | Promise, +): Promise; +export function tryOrElse( + fn: () => T | Promise, + elseFn: (err: unknown) => T | Promise, +): T | Promise { + try { + const ret = fn(); + if (ret instanceof Promise) { + return ret.catch((err) => elseFn(err)); + } + return ret; + } catch (err) { + return elseFn(err); + } +} diff --git a/try_or_else_test.ts b/try_or_else_test.ts new file mode 100644 index 0000000..2e988b2 --- /dev/null +++ b/try_or_else_test.ts @@ -0,0 +1,62 @@ +import { + assertEquals, + assertRejects, + assertThrows, +} from "jsr:@std/assert@0.225.1"; +import { raise } from "./raise.ts"; +import { tryOrElse } from "./try_or_else.ts"; + +Deno.test("tryOrElse", async (t) => { + const err = new Error("error"); + const resolve = Promise.resolve.bind(Promise); + const reject = Promise.reject.bind(Promise); + + await t.step("sync", () => { + type T = number; + assertEquals(tryOrElse((): T => 1, () => 2), 1); + assertEquals(tryOrElse((): T => raise(err), () => 2), 2); + }); + + await t.step("sync (error)", () => { + type T = number; + assertThrows( + () => tryOrElse((): T => raise(err), () => raise(err)), + Error, + "error", + ); + }); + + await t.step("async", async () => { + type T = Promise; + assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1); + assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1); + assertEquals(await tryOrElse((): T => reject(err), () => 2), 2); + assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2); + assertEquals(await tryOrElse((): T => raise(err), () => 2), 2); + assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2); + }); + + await t.step("async (error)", async () => { + type T = Promise; + await assertRejects( + () => tryOrElse((): T => reject(err), () => reject(err)), + Error, + "error", + ); + await assertRejects( + () => tryOrElse((): T => reject(err), () => raise(err)), + Error, + "error", + ); + await assertRejects( + () => tryOrElse((): T => raise(err), () => reject(err)), + Error, + "error", + ); + assertThrows( + () => tryOrElse((): T => raise(err), () => raise(err)), + Error, + "error", + ); + }); +}); diff --git a/try_or_test.ts b/try_or_test.ts index 8c13cb8..baf88d5 100644 --- a/try_or_test.ts +++ b/try_or_test.ts @@ -1,10 +1,6 @@ -import { - assertEquals, - assertRejects, - assertThrows, -} from "jsr:@std/assert@0.225.1"; +import { assertEquals } from "jsr:@std/assert@0.225.1"; import { raise } from "./raise.ts"; -import { tryOr, tryOrElse } from "./try_or.ts"; +import { tryOr } from "./try_or.ts"; Deno.test("tryOr", async (t) => { const err = new Error("error"); @@ -27,58 +23,3 @@ Deno.test("tryOr", async (t) => { assertEquals(await tryOr((): T => raise(err), resolve(2)), 2); }); }); - -Deno.test("tryOrElse", async (t) => { - const err = new Error("error"); - const resolve = Promise.resolve.bind(Promise); - const reject = Promise.reject.bind(Promise); - - await t.step("sync", () => { - type T = number; - assertEquals(tryOrElse((): T => 1, () => 2), 1); - assertEquals(tryOrElse((): T => raise(err), () => 2), 2); - }); - - await t.step("sync (error)", () => { - type T = number; - assertThrows( - () => tryOrElse((): T => raise(err), () => raise(err)), - Error, - "error", - ); - }); - - await t.step("async", async () => { - type T = Promise; - assertEquals(await tryOrElse((): T => resolve(1), () => 2), 1); - assertEquals(await tryOrElse((): T => resolve(1), () => resolve(2)), 1); - assertEquals(await tryOrElse((): T => reject(err), () => 2), 2); - assertEquals(await tryOrElse((): T => reject(err), () => resolve(2)), 2); - assertEquals(await tryOrElse((): T => raise(err), () => 2), 2); - assertEquals(await tryOrElse((): T => raise(err), () => resolve(2)), 2); - }); - - await t.step("async (error)", async () => { - type T = Promise; - await assertRejects( - () => tryOrElse((): T => reject(err), () => reject(err)), - Error, - "error", - ); - await assertRejects( - () => tryOrElse((): T => reject(err), () => raise(err)), - Error, - "error", - ); - await assertRejects( - () => tryOrElse((): T => raise(err), () => reject(err)), - Error, - "error", - ); - assertThrows( - () => tryOrElse((): T => raise(err), () => raise(err)), - Error, - "error", - ); - }); -}); diff --git a/unimplemented.ts b/unimplemented.ts index 66d4de4..4913f22 100644 --- a/unimplemented.ts +++ b/unimplemented.ts @@ -16,7 +16,7 @@ export class UnimplementedError extends Error { * For example, defining a mock object with `unimplemented` function should look like this: * * ```ts - * import { unimplemented } from "https://deno.land/x/errorutil@$MODULE_VERSION/unimplemented.ts"; + * import { unimplemented } from "@lambdalisue/errorutil/unimplemented"; * * type Service = { * get(id: string): Promise; diff --git a/unreachable.ts b/unreachable.ts index 0888651..98a949d 100644 --- a/unreachable.ts +++ b/unreachable.ts @@ -19,7 +19,7 @@ export class UnreachableError extends Error { * For example, the following code passed type checking. * * ```ts - * import { unreachable } from "https://deno.land/x/errorutil@$MODULE_VERSION/unreachable.ts"; + * import { unreachable } from "@lambdalisue/errorutil/unreachable"; * * type Animal = "dog" | "cat"; * @@ -41,7 +41,7 @@ export class UnreachableError extends Error { * But the following code because a case for "bird" is missing. * * ```ts - * import { unreachable } from "https://deno.land/x/errorutil@$MODULE_VERSION/unreachable.ts"; + * import { unreachable } from "@lambdalisue/errorutil/unreachable"; * * type Animal = "dog" | "cat" | "bird"; *