diff --git a/package.json b/package.json index d89004d..e62628b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trycat", - "version": "0.2.0", + "version": "0.2.1", "description": "A lightweight, type-safe, zero-dependency implementation of the Result type.", "keywords": ["result", "result-type", "error-handling", "error", "monad", "rust", "rust-result"], "author": "kennethnym", diff --git a/trycat.test.ts b/trycat.test.ts index e1b260c..8cb5a77 100644 --- a/trycat.test.ts +++ b/trycat.test.ts @@ -1,5 +1,5 @@ import { describe, expect, expectTypeOf, it, test, vi } from "vitest" -import { type Result, err, ok, tryp, trys } from "./trycat" +import { type Result, err, ok, tryp, trys, Err } from "./trycat" describe("Ok", () => { describe("isOk", () => { @@ -23,6 +23,11 @@ describe("Ok", () => { }) }) + test("it accepts empty value", () => { + const result = ok() + expect(result.value).toBeUndefined() + }) + test("inspect calls the given function with the contained value", () => { const value = [1, 2, 3] const fn = vi.fn() @@ -142,6 +147,11 @@ describe("Err", () => { } }) + test("it accepts empty error", () => { + const result = err() + expect(result.error).toBeUndefined() + }) + test("inspect does nothing and returns Err", () => { const fn = vi.fn() const result = err(401).inspect(fn) @@ -250,6 +260,15 @@ describe("trys", () => { throw new Error("Expected an Err, received an Ok") } }) + + it("works with void function", () => { + const result = trys(() => {}) + if (result.isOk()) { + expect(result.value).toBeUndefined() + } else { + throw new Error("Expected an Ok value, received an Err") + } + }) }) describe("tryp", () => { @@ -270,4 +289,13 @@ describe("tryp", () => { throw new Error("Expected an Err, received an Ok") } }) + + it("works with void Promise", async () => { + const result = await tryp(Promise.resolve()) + if (result.isOk()) { + expect(result.value).toBeUndefined() + } else { + throw new Error("Expected an Ok value, received an Err") + } + }) }) diff --git a/trycat.ts b/trycat.ts index 68ad44c..85f45d6 100644 --- a/trycat.ts +++ b/trycat.ts @@ -324,21 +324,28 @@ class Err implements ResultBase { */ type Result = Ok | Err -function ok(value: T): Ok { - return new Ok(value) +function ok(): Ok +function ok(value: T): Ok +function ok(value?: T): Ok | Ok { + return value ? new Ok(value) : new Ok(undefined) } -function err(error: TErr): Err { - return new Err(error) +function err(): Err +function err(error: T): Err +function err(error?: TErr): Err | Err { + return error ? new Err(error) : new Err(undefined) } /** * Calls the given function, catches any thrown error into an {@link Err}, * and wraps the returned value with an {@link Ok} if nothing goes wrong. */ -function trys(fn: () => T): Result { +function trys(fn: () => void): Result +function trys(fn: () => T): Result +function trys(fn: () => T | undefined): Result | Result { try { - return ok(fn()) + const retval = fn() + return retval ? ok(retval) : ok() } catch (e: unknown) { return err(e) } @@ -354,7 +361,9 @@ function trys(fn: () => T): Result { * return err(res.error) * } */ -function tryp(promise: Promise): Promise> { +function tryp(promise: Promise): Promise> +function tryp(promise: Promise): Promise> +function tryp(promise: Promise): Promise | Result> { return promise.then((value) => ok(value)).catch((e) => err(e)) }