Skip to main content

@oxi/result@0.2.0

A TypeScript implementation of the Rust-inspired Result type, offering a robust and functional approach to error handling and success management in asynchronous and synchronous operations.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
a month ago
indexResult
class Result
deno add @oxi/result
import { Result } from "@oxi/result";
npx jsr add @oxi/result
import { Result } from "@oxi/result";
yarn dlx jsr add @oxi/result
import { Result } from "@oxi/result";
pnpm dlx jsr add @oxi/result
import { Result } from "@oxi/result";
bunx jsr add @oxi/result
import { Result } from "@oxi/result";

Represents a result of an operation that could be successful (Ok) or failed (Err).

Constructors

new
Result(ok: boolean, value: T | E)

Type Parameters

Methods

and<U>(result: Result<U, E>): Result<U, E>

Returns the provided Ok result if the original Result is Ok, otherwise returns the original Err.

andThen<U>(mapper: (value: T) => Result<U, E>): Result<U, E>

Applies a function to the contained Ok value, or returns the original Err if the original Result is Err.

expect(message: string): T

Unwraps a Result, yielding the content of an Ok. Throws an error with the given message if the Result is Err.

expectErr(message: string): E

Unwraps a Result, yielding the content of an Err. Throws an error with the given message if the Result is Ok.

inspect(onOk: (value: T) => void): this

Performs a side-effect with the Ok value if the Result is Ok, and does nothing if it is Err.

inspectErr(onErr: (value: E) => void): this

Performs a side-effect with the Err value if the Result is Err, and does nothing if it is Ok.

Checks if the Result is Err.

isErrAnd(predicate: (value: E) => boolean): boolean

Checks if the Result is Err and the contained error satisfies a given predicate.

Checks if the Result is Ok.

isOkAnd(predicate: (value: T) => boolean): boolean

Checks if the Result is Ok and the contained value satisfies a given predicate.

map<U>(mapper: (value: T) => U): Result<U, E>

Transforms the Ok value of the Result using a given function, leaving an Err unchanged.

mapErr<U>(mapper: (value: E) => U): Result<T, U>

Transforms the Err value of the Result using a given function, leaving an Ok unchanged.

mapOr<U>(defaultValue: U, mapper: (value: T) => U): U

Transforms the Ok value of the Result or returns a default value if the Result is Err.

mapOrElse<U>(defaultMapper: () => U, mapper: (value: T) => U): U

Transforms the Ok value of the Result using a given function, or computes a default value if the Result is Err.

match<U>(onOk: (value: T) => U, onErr: (value: E) => U): U

Applies functions to the Result value, depending on whether it is Ok or Err.

or<F>(result: Result<T, F>): Result<T, F>

Returns the provided Err result if the original Result is Err, otherwise returns the original Ok.

orElse<F>(mapper: (value: E) => Result<T, F>): Result<T, F>

Applies a function to the contained Err value, or returns the original Ok if the original Result is Ok.

toJSON(): { ok: true; value: T; } | { ok: false; error: E; }

Serializes the Result to a JSON object representation with an ok field indicating success or failure and a value or error field containing the respective data. If the Result is Ok, the object will have { ok: true, value: T }. If the Result is Err, the object will have { ok: false, error: E }.

Returns a string representation of the Result, with Ok(value) if the Result is Ok, or Err(error) if the Result is Err. This method provides a human-readable representation of the Result, which can be useful for debugging or logging.

Unwraps a Result, yielding the content of an Ok. Throws an error if the Result is Err.

Unwraps a Result, yielding the content of an Err. Throws an error if the Result is Ok.

unwrapOr(defaultValue: T): T

Unwraps a Result, yielding the content of an Ok, or a default value if the Result is Err.

unwrapOrElse(defaultMapper: () => T): T

Unwraps a Result, yielding the content of an Ok, or computes a default value from a function if the Result is Err.

Static Methods

Err<T, E>(value: E): Result<T, E>

Creates an Err result containing an error value.

Ok<T, E>(value: T): Result<T, E>

Creates an Ok result containing a success value.

from<T extends GenericFunction>(fn: T): AsyncToResult<T>

Creates a Result from a function that may return a value directly or a Promise of a value. If the function returns a Promise, the resulting Promise will resolve to a Result.Ok with the resolved value or a Result.Err with the rejection reason. If the function returns a value directly, it will return a Result.Ok with the value, or catch any thrown error and return a Result.Err with the error. This method is generic and can handle both synchronous and asynchronous operations seamlessly.

isResult(value: unknown): value is Result<unknown, unknown>

Checks if a value is a Result instance.

wrap<T extends GenericFunction>(fn: T): (...args: Parameters<T>) => AsyncToResult<T>

Wraps a function to return a Result or a Promise of a Result, instead of throwing errors.