IF Commons GitHub

result

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0001-core FR-001

Errors as values: a step that can fail returns a Result whose error is a type of its own, usually a union told apart by kind (0001-core FR-001). Taken from erga's kernel and the machine setup's result.ts, which wrote the same thing twice.

import { andThen, err, ok, type Result } from "./result.ts";
const half = (n: number): Result<number, string> => n % 2 === 0 ? ok(n / 2) : err(`${n} is odd`);
andThen(half(8), half); // { kind: "ok", value: 2 }

Source: commons/core/src/result.ts

15 of 17 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

allfunction
function all<T, E>(results: readonly Result<T, E>[]): Result<readonly T[], E>

Every result's value in order, or the first failure.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

andThenfunction
function andThen<T, U, E>(result: Result<T, E>, f: (value: T) => Result<U, E>): Result<U, E>

A second step run on the value of a success; a failure unchanged.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

attemptfunction
function attempt<T, E>(f: () => T, onThrow: (thrown: unknown) => E): Result<T, E>

What a function that throws returns, as a result: its value, or what it threw turned into an error by onThrow.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

doneconst
const done: Ok<void>

The success that carries nothing: a step that only had to happen.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

errfunction
function err<E>(error: E): Err<E>

A failure.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

isErrfunction
function isErr<T, E>(result: Result<T, E>): result is Err<E>

Whether a result is a failure (a type guard).

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

isOkfunction
function isOk<T, E>(result: Result<T, E>): result is Ok<T>

Whether a result is a success (a type guard).

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

mapfunction
function map<T, U, E>(result: Result<T, E>, f: (value: T) => U): Result<U, E>

The value of a success transformed; a failure unchanged.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

mapErrfunction
function mapErr<T, E, F>(result: Result<T, E>, f: (error: E) => F): Result<T, F>

The error of a failure transformed; a success unchanged.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

matchfunction
function match<T, E, R>(result: Result<T, E>, on: { readonly ok: (value: T) => R; readonly err: (error: E) => R; }): R

One function for each alternative, and what the one that applies returns.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

okfunction
function ok<T>(value: T): Ok<T>

A success.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

orElsefunction
function orElse<T, E, F>(result: Result<T, E>, f: (error: E) => Result<T, F>): Result<T, F>

A second step run on the error of a failure, which may recover; a success unchanged.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

settlefunction
function settle<T, E>(promise: PromiseLike<T>, onReject: (reason: unknown) => E): Promise<Result<T, E>>

What a promise settles to, as a result: its value, or its rejection turned into an error by onReject.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

unwrapOrfunction
function unwrapOr<T, E>(result: Result<T, E>, fallback: T): T

The value of a success, or fallback for a failure.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

Errtypeno test imports it
type Err<E> = { readonly kind: "err"; readonly error: E };

A step's failure, with its typed error.

Oktypeno test imports it
type Ok<T> = { readonly kind: "ok"; readonly value: T };

A step's success, with its value.

Resulttype
type Result<T, E> = Ok<T> | Err<E>;

What a step that can fail returns.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.