JSPM

  • Created
  • Published
  • Downloads 11
  • Score
    100M100P100Q57086F
  • License MIT

Simply typesafe Result and Option monads in typescript and javascript.

Package Exports

  • simply-result
  • simply-result/dist/main.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simply-result) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Latest released version Minified and gzipped bundle size Type support CI status Code coverage Downloads from NPM MIT licensed

simply-result

Simply typesafe Result and Option monads in typescript and javascript. Only about 700b minified and gzipped. Branchless implementation, waisting no processing cycles on unnecessary operations. On average ~15% faster than try-catch.

See also the sister library simply-result-util for useful monadic helper functions such as Try, transpose, and flatten.

import { Result, Ok, Err, Some, None } from 'simply-result';

const doSomeWork = (): Result<number, Error> => Ok(3);

const fraction = doSomeWork()
  .elseThen(err => {
    console.error(err);
    return Err(err);
  })
  .intoOption()
  .andThen(it => it === 0 ? None : Some(it))
  .andThen(it => Some(1 / it))
  .match({
    Some: it => it.toPrecision(3),
    None: () => '0'
  });

console.log(fraction); // "0.333"

Installation

NPM

npm i simply-result

import {
    Result, Ok, Err,
    Option, Some, None,
} from 'simply-result';

Demo

See ./demo

Type Docs

Result

type Result<V, E = Error> =
  | Ok<V>
  | Err<E>

interface Ok<V> {
  isOk: true
  isErr: false
  ok: V
  match<T>(cases: {
    Ok: (ok: V) => T,
  }): T
  intoOption(): Some<V>
  intoErrOption(): None
  andThen<T>(fn: (ok: V) => T): T
  elseThen(fn: unknown): Ok<V>
  toString(): string
}

interface Err<E> {
  isOk: false
  isErr: true
  err: E
  match<T>(cases: {
    Err: (err: E) => T,
  }): T
  intoOption(): None
  intoErrOption(): Some<E>
  andThen(fn: unknown): Err<E>
  elseThen<T>(fn: (err: E) => T): T
  toString(): string
}

function Ok<V>(value: V): Ok<V>

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

Option

type Option<V> =
  | Some<V>
  | None

interface Some<V> {
  isSome: true
  isNone: false
  some: V
  match<T>(cases: {
    Some: (some: V) => T,
  }): T
  intoResult(error: unknown): Ok<V>
  andThen<T>(fn: (some: V) => T): T
  elseThen(fn: unknown): Some<V>
  toString(): string
}

interface None {
  isSome: false
  isNone: true
  match<T>(cases: {
    None: () => T,
  }): T
  intoResult<E>(error: E): Err<E>
  andThen(fn: unknown): None
  elseThen<T>(fn: () => T): T
  toString(): string
}

function Some<V>(value: V): Some<V>

const None: None

Performance

Code Result
Result Err(new Error()).elseThen(err => { String(err) }) 210,625 ops/sec ±0.26% (91 runs sampled)
Try Catch try { throw new Error() } catch (err) { String(err) } 183,401 ops/sec ±0.57% (89 runs sampled)
Baseline String(new Error()) 211,627 ops/sec ±0.39% (89 runs sampled)

Tests were ran on a 32gb MacBook M1 Pro running macOS 14.0. The test code can be found here.