From 745e767f36a54ed0841b36660ac4bc3e479896e6 Mon Sep 17 00:00:00 2001 From: Alex Nault Date: Sun, 5 Nov 2023 16:50:33 -0500 Subject: [PATCH] Rename to fn-try --- package-lock.json | 4 +- package.json | 20 ++-- rollup.config.ts | 6 +- tests/index.test.ts | 217 ---------------------------------- tests/tryCatch.test.ts | 201 +++++++++++++++++++++++++++++++ tests/tryCatchFinally.test.ts | 9 ++ tests/tryFinally.test.ts | 9 ++ 7 files changed, 234 insertions(+), 232 deletions(-) delete mode 100644 tests/index.test.ts create mode 100644 tests/tryCatch.test.ts create mode 100644 tests/tryCatchFinally.test.ts create mode 100644 tests/tryFinally.test.ts diff --git a/package-lock.json b/package-lock.json index f752579..de3e6de 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "try-fn", + "name": "fn-try", "version": "1.0.0-semantic-release", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "try-fn", + "name": "fn-try", "version": "1.0.0-semantic-release", "license": "MIT", "devDependencies": { diff --git a/package.json b/package.json index 2d2e1f6..cf2ddff 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,25 @@ { - "name": "try-fn", + "name": "fn-try", "version": "1.0.0-semantic-release", "description": "TODO", - "main": "./dist/cjs/try-fn.js", - "module": "./dist/esm/try-fn.mjs", - "types": "./dist/try-fn.d.ts", + "main": "./dist/cjs/fn-try.js", + "module": "./dist/esm/fn-try.mjs", + "types": "./dist/fn-try.d.ts", "exports": { ".": { - "types": "./dist/try-fn.d.ts", - "require": "./dist/cjs/try-fn.js", - "import": "./dist/esm/try-fn.mjs" + "types": "./dist/fn-try.d.ts", + "require": "./dist/cjs/fn-try.js", + "import": "./dist/esm/fn-try.mjs" }, "./package.json": "./package.json" }, "author": "Alex Nault", "keywords": [ - "try-fn" + "fn-try" ], "license": "MIT", - "repository": "https://github.com/alexnault/try-fn", - "homepage": "https://github.com/alexnault/try-fn#readme", + "repository": "https://github.com/alexnault/fn-try", + "homepage": "https://github.com/alexnault/fn-try#readme", "scripts": { "build": "rm -rf ./dist && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript", "format": "prettier ./ --write", diff --git a/rollup.config.ts b/rollup.config.ts index 142c454..7644816 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -9,13 +9,13 @@ const config: RollupOptions[] = [ plugins: [esbuild(), terser()], output: [ { - file: `dist/cjs/try-fn.js`, + file: `dist/cjs/fn-try.js`, format: "cjs", exports: "named", strict: false, // Don't emit "use strict" in output }, { - file: `dist/esm/try-fn.mjs`, + file: `dist/esm/fn-try.mjs`, format: "es", }, ], @@ -24,7 +24,7 @@ const config: RollupOptions[] = [ input: "src/index.ts", plugins: [dts()], output: { - file: `dist/try-fn.d.ts`, + file: `dist/fn-try.d.ts`, format: "es", }, }, diff --git a/tests/index.test.ts b/tests/index.test.ts deleted file mode 100644 index d3257a9..0000000 --- a/tests/index.test.ts +++ /dev/null @@ -1,217 +0,0 @@ -/* eslint-disable @typescript-eslint/no-empty-function */ -import { describe, it, expect, expectTypeOf } from "vitest"; - -import { tryFn } from "../src"; -import { - willThrowAsync, - willThrow, - sleep, - willPassAsync, - willPass, -} from "./utils"; - -describe("tryFn", () => { - describe("try catch", () => { - it("should have proper return types", () => { - const result = tryFn( - () => willPass("success"), - () => "failed" - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should have both return types", () => { - const result = tryFn( - () => willPass("success"), - () => 1 - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should have both return types", () => { - const result = tryFn( - () => {}, - () => {} - ); - expect(result).toBe(undefined); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should return the function value on success", () => { - const result = tryFn( - () => willPass("success"), - (error) => { - throw error; - } - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should return the catch value on error", () => { - const result = tryFn( - () => willThrow("success"), - () => "fail" - ); - expect(result).toBe("fail"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should return the undefined catch value on error", () => { - const result = tryFn( - () => willThrow("success"), - () => {} - ); - expect(result).toBe(undefined); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should throw an error on error", () => { - const wrapper = () => - tryFn( - () => willThrow("success"), - (error) => { - throw error; - } - ); - expect(wrapper).toThrowError("The function failed."); - expectTypeOf(wrapper).toEqualTypeOf<() => string>(); - }); - - // Async - it("should return the value async", async () => { - const result = await tryFn( - async () => willPassAsync("success"), - (error) => { - throw error; - } - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should have both return types", async () => { - const wrapper = () => - tryFn( - async () => "success", - () => 1 - ); - expect(await wrapper()).toBe("success"); - expectTypeOf(wrapper).toEqualTypeOf<() => Promise | number>(); - }); - - it("should have both return types", () => { - const result = tryFn( - () => willPass("success"), - async () => { - await sleep(1); - return 1; - } - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf>(); - }); - - it("should have both return types", async () => { - const result = await tryFn( - async () => {}, - async () => {} - ); - expect(result).toBe(undefined); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should have both return types", async () => { - const result = await tryFn( - async () => willPassAsync("success"), - async () => {} - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should have both return types", async () => { - const result = await tryFn( - async () => willPassAsync("success"), - async (error) => { - await sleep(1); - throw error; - } - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should have both return types", async () => { - const result = await tryFn( - async () => willPassAsync("success"), - async () => { - await sleep(1); - return 1; - } - ); - expect(result).toBe("success"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should return the value async", async () => { - const result = await tryFn( - async () => willThrowAsync("success"), - () => "fail" - ); - expect(result).toBe("fail"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should throw an error on error", async () => { - const result = await tryFn( - async () => willThrowAsync("success"), - async () => { - await sleep(1); - return "failed"; - } - ); - expect(result).toBe("failed"); - expectTypeOf(result).toEqualTypeOf(); - }); - - it("should throw an error on error", async () => { - const promise = tryFn( - async () => willThrowAsync("success"), - (error) => { - throw error; - } - ); - await expect(promise).rejects.toThrowError("The function failed."); - expectTypeOf(promise).toEqualTypeOf>(); - }); - - it("should throw an error on error", async () => { - const promise = tryFn( - async () => willThrowAsync("success"), - async (error) => { - await sleep(1); - throw error; - } - ); - await expect(promise).rejects.toThrowError("The function failed."); - expectTypeOf(promise).toEqualTypeOf | Promise>(); - }); - }); - - describe("try catch finally", () => { - // TODO - it("should return the value", () => { - expect(true).toBe(true); - }); - }); - - describe("try finally", () => { - // TODO - it("should return the value", () => { - expect(true).toBe(true); - }); - }); -}); diff --git a/tests/tryCatch.test.ts b/tests/tryCatch.test.ts new file mode 100644 index 0000000..3e44e4f --- /dev/null +++ b/tests/tryCatch.test.ts @@ -0,0 +1,201 @@ +/* eslint-disable @typescript-eslint/no-empty-function */ +import { describe, it, expect, expectTypeOf } from "vitest"; + +import { tryFn } from "../src"; +import { + willThrowAsync, + willThrow, + sleep, + willPassAsync, + willPass, +} from "./utils"; + +describe("try/catch", () => { + it("should have proper return types", () => { + const result = tryFn( + () => willPass("success"), + () => "failed" + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should have both return types", () => { + const result = tryFn( + () => willPass("success"), + () => 1 + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should have both return types", () => { + const result = tryFn( + () => {}, + () => {} + ); + expect(result).toBe(undefined); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should return the function value on success", () => { + const result = tryFn( + () => willPass("success"), + (error) => { + throw error; + } + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should return the catch value on error", () => { + const result = tryFn( + () => willThrow("success"), + () => "fail" + ); + expect(result).toBe("fail"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should return the undefined catch value on error", () => { + const result = tryFn( + () => willThrow("success"), + () => {} + ); + expect(result).toBe(undefined); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should throw an error on error", () => { + const wrapper = () => + tryFn( + () => willThrow("success"), + (error) => { + throw error; + } + ); + expect(wrapper).toThrowError("The function failed."); + expectTypeOf(wrapper).toEqualTypeOf<() => string>(); + }); + + // Async + it("should return the value async", async () => { + const result = await tryFn( + async () => willPassAsync("success"), + (error) => { + throw error; + } + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should have both return types", async () => { + const wrapper = () => + tryFn( + async () => "success", + () => 1 + ); + expect(await wrapper()).toBe("success"); + expectTypeOf(wrapper).toEqualTypeOf<() => Promise | number>(); + }); + + it("should have both return types", () => { + const result = tryFn( + () => willPass("success"), + async () => { + await sleep(1); + return 1; + } + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf>(); + }); + + it("should have both return types", async () => { + const result = await tryFn( + async () => {}, + async () => {} + ); + expect(result).toBe(undefined); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should have both return types", async () => { + const result = await tryFn( + async () => willPassAsync("success"), + async () => {} + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should have both return types", async () => { + const result = await tryFn( + async () => willPassAsync("success"), + async (error) => { + await sleep(1); + throw error; + } + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should have both return types", async () => { + const result = await tryFn( + async () => willPassAsync("success"), + async () => { + await sleep(1); + return 1; + } + ); + expect(result).toBe("success"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should return the value async", async () => { + const result = await tryFn( + async () => willThrowAsync("success"), + () => "fail" + ); + expect(result).toBe("fail"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should throw an error on error", async () => { + const result = await tryFn( + async () => willThrowAsync("success"), + async () => { + await sleep(1); + return "failed"; + } + ); + expect(result).toBe("failed"); + expectTypeOf(result).toEqualTypeOf(); + }); + + it("should throw an error on error", async () => { + const promise = tryFn( + async () => willThrowAsync("success"), + (error) => { + throw error; + } + ); + await expect(promise).rejects.toThrowError("The function failed."); + expectTypeOf(promise).toEqualTypeOf>(); + }); + + it("should throw an error on error", async () => { + const promise = tryFn( + async () => willThrowAsync("success"), + async (error) => { + await sleep(1); + throw error; + } + ); + await expect(promise).rejects.toThrowError("The function failed."); + expectTypeOf(promise).toEqualTypeOf | Promise>(); + }); +}); diff --git a/tests/tryCatchFinally.test.ts b/tests/tryCatchFinally.test.ts new file mode 100644 index 0000000..3079577 --- /dev/null +++ b/tests/tryCatchFinally.test.ts @@ -0,0 +1,9 @@ +/* eslint-disable @typescript-eslint/no-empty-function */ +import { describe, it, expect } from "vitest"; + +// TODO +describe("try/catch/finally", () => { + it("should return the value", () => { + expect(true).toBe(true); + }); +}); diff --git a/tests/tryFinally.test.ts b/tests/tryFinally.test.ts new file mode 100644 index 0000000..d1a2bd2 --- /dev/null +++ b/tests/tryFinally.test.ts @@ -0,0 +1,9 @@ +/* eslint-disable @typescript-eslint/no-empty-function */ +import { describe, it, expect } from "vitest"; + +// TODO +describe("try/finally", () => { + it("should return the value", () => { + expect(true).toBe(true); + }); +});