Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions v0.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
71 changes: 71 additions & 0 deletions v0.ts
Original file line number Diff line number Diff line change
@@ -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<number, string> = ok(10);
* ```
*/
export type Ok<T> = {
ok: true;
Expand All @@ -8,6 +18,12 @@ export type Ok<T> = {

/**
* @description Represents an error result.
* @example
* ```ts
* import { err, Result } from "./v0.ts";
*
* const result: Result<number, string> = err("error");
* ```
*/
export type Err<E> = {
ok: false;
Expand All @@ -16,26 +32,81 @@ export type Err<E> = {

/**
* @description Represents either a successful or an error result.
* @example
* ```ts
* import { ok, err, Result, isOk, isErr } from "./v0.ts";
*
* const okResult: Result<number, string> = ok(10);
* const errResult: Result<number, string> = err("error");
*
* if (isOk(okResult)) {
* console.log(okResult.data);
* }
*
* if (isErr(errResult)) {
* console.log(errResult.error);
* }
* ```
*/
export type Result<T, E> = Ok<T> | Err<E>;

/**
* @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 = <T>(data: T): Ok<T> => ({ 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 = <E>(error: E): Err<E> => ({ 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 = <T, E>(result: Result<T, E>): result is Ok<T> => 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 = <T, E>(result: Result<T, E>): result is Err<E> =>
!result.ok;