Skip to main content
Home

@oxi/result@0.3.1
Built and signed on GitHub Actions

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.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
11 months ago (0.3.1)

This module provides the Result class, a TypeScript implementation of the Rust's Result type, offering a robust way to handle errors and success values without throwing exceptions. It is particularly useful in functional programming patterns and scenarios where errors are expected and need to be handled gracefully.

The Result type is a union type that can either be Ok, representing success and containing a success value, or Err, representing failure and containing an error value.

Methods:

  • Ok: Creates an Ok Result instance.
  • Err: Creates an Err Result instance.
  • from: Converts a function's return value or a promise to a Result.
  • wrap: Wraps a function, ensuring it returns a Result.
  • match: Applies functions based on the Result's state (Ok or Err).
  • isOk, isErr: Check the state of the Result.
  • map, mapErr: Transform the value inside the Result.
  • and, or, andThen, orElse: Combine Result instances in various ways.
  • unwrap, unwrapOr, unwrapOrElse: Extract values from the Result, with or without defaults.
  • expect, expectErr: Unwrap with custom error messages.
  • toJSON: Serialize the Result to a JSON object.
  • toString: Get a string representation of the Result.

Examples

Example 1

// Creating an Ok Result
const okResult = Result.Ok(123);
console.log(okResult.toString()); // "Ok(123)"

// Creating an Err Result
const errResult = Result.Err("An error occurred");
console.log(errResult.toString()); // "Err(An error occurred)"

// Handling a Result with `match`
const result = Result.Ok(5);
const message = result.match(
  value => `Success with value: ${value}`,
  error => `Failed due to error: ${error}`
);
console.log(message); // "Success with value: 5"

// Using `map` to transform an Ok value
const doubled = Result.Ok(2).map(x => x * 2);
console.log(doubled.unwrap()); // 4

// Using `unwrapOr` to provide a default value for an Err
const value = Result.Err("Error").unwrapOr(42);
console.log(value); // 42

// Using `from` to handle asynchronous operations
async function fetchData() {
  return "Data";
}
Result.from(fetchData).then(res => console.log(res.unwrap())); // "Data"

// Combining Results with `andThen`
const queryResult = Result.Ok("Query");
const processResult = queryResult.andThen(query => Result.Ok(`Processed ${query}`));
console.log(processResult.unwrap()); // "Processed Query"

This module is designed to enhance error handling and functional programming patterns in TypeScript applications, providing a more declarative approach to dealing with operations that can succeed or fail.

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@oxi/result

Import symbol

import * as result from "@oxi/result";
or

Import directly with a jsr specifier

import * as result from "jsr:@oxi/result";

Add Package

pnpm i jsr:@oxi/result
or (using pnpm 10.8 or older)
pnpm dlx jsr add @oxi/result

Import symbol

import * as result from "@oxi/result";

Add Package

yarn add jsr:@oxi/result
or (using Yarn 4.8 or older)
yarn dlx jsr add @oxi/result

Import symbol

import * as result from "@oxi/result";

Add Package

npx jsr add @oxi/result

Import symbol

import * as result from "@oxi/result";

Add Package

bunx jsr add @oxi/result

Import symbol

import * as result from "@oxi/result";