From 8c529bd304314a43af4842d307454a0663037cac Mon Sep 17 00:00:00 2001 From: "s.uchihori" Date: Mon, 14 Apr 2025 17:06:19 +0900 Subject: [PATCH] docs --- deno.json | 2 +- v0.test.ts | 14 +++++++++++ v0.ts | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) diff --git a/deno.json b/deno.json index 76b7224..c144895 100644 --- a/deno.json +++ b/deno.json @@ -4,7 +4,7 @@ "@std/assert": "jsr:@std/assert@1" }, "name": "@showichiro/result", - "version": "0.1.0", + "version": "0.1.1", "exports": { ".": "./mod.ts", "./v0": "./v0.ts" diff --git a/v0.test.ts b/v0.test.ts index 4bdbcad..92cba59 100644 --- a/v0.test.ts +++ b/v0.test.ts @@ -41,3 +41,17 @@ Deno.test("Result type", () => { assertEquals(errResult.error, "error message"); } }); + +Deno.test("ok with string", () => { + const result = ok("hello"); + assertEquals(result, { ok: true, data: "hello" }); + assert(isOk(result)); + assertEquals(result.data, "hello"); +}); + +Deno.test("err with number", () => { + const result = err(42); + assertEquals(result, { ok: false, error: 42 }); + assert(isErr(result)); + assertEquals(result.error, 42); +}); diff --git a/v0.ts b/v0.ts index 0985512..400f6f1 100644 --- a/v0.ts +++ b/v0.ts @@ -1,5 +1,15 @@ +/** + * @module result + * @description Represents a result that can be either successful or an error. + */ /** * @description Represents a successful result. + * @example + * ```ts + * import { ok, Result } from "./v0.ts"; + * + * const result: Result = ok(10); + * ``` */ export type Ok = { ok: true; @@ -8,6 +18,12 @@ export type Ok = { /** * @description Represents an error result. + * @example + * ```ts + * import { err, Result } from "./v0.ts"; + * + * const result: Result = err("error"); + * ``` */ export type Err = { ok: false; @@ -16,26 +32,81 @@ export type Err = { /** * @description Represents either a successful or an error result. + * @example + * ```ts + * import { ok, err, Result, isOk, isErr } from "./v0.ts"; + * + * const okResult: Result = ok(10); + * const errResult: Result = err("error"); + * + * if (isOk(okResult)) { + * console.log(okResult.data); + * } + * + * if (isErr(errResult)) { + * console.log(errResult.error); + * } + * ``` */ export type Result = Ok | Err; /** * @description Creates a successful result. + * @param data The data to wrap in a successful result. + * @returns An `Ok` result containing the data. + * @example + * ```ts + * import { ok } from "./v0.ts"; + * + * const result = ok(10); + * ``` */ export const ok = (data: T): Ok => ({ ok: true, data }); /** * @description Creates an error result. + * @param error The error to wrap in an error result. + * @returns An `Err` result containing the error. + * @example + * ```ts + * import { err } from "./v0.ts"; + * + * const result = err("error"); + * ``` */ export const err = (error: E): Err => ({ ok: false, error }); /** * @description Checks if a result is a successful result. + * @param result The result to check. + * @returns `true` if the result is an `Ok` result, `false` otherwise. + * @example + * ```ts + * import { ok, isOk } from "./v0.ts"; + * + * const result = ok(10); + * + * if (isOk(result)) { + * console.log("Result is Ok"); + * } + * ``` */ export const isOk = (result: Result): result is Ok => result.ok; /** * @description Checks if a result is an error result. + * @param result The result to check. + * @returns `true` if the result is an `Err` result, `false` otherwise. + * @example + * ```ts + * import { err, isErr } from "./v0.ts"; + * + * const result = err("error"); + * + * if (isErr(result)) { + * console.log("Result is Err"); + * } + * ``` */ export const isErr = (result: Result): result is Err => !result.ok;