npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@badrap/result

v0.2.13

Published

A TypeScript result type taking cues from Rust's Result and Haskell's Either types

Downloads

20,188

Readme

@badrap/result tests npm

A TypeScript result type taking cues from Rust's Result and Haskell's Either types. It's goals are:

  • Small, idiomatic API surface: Mix and match parts from Rust's Result and Haskell's Either types, but modify them to make the experience TypeScript-y (TypeScriptic? TypeScriptalicious?). Of course this is pretty subjective.
  • Coding errors should throw: While Result#map and Result#chain together somewhat resemble Promise#then but differ in that they don't implicitly wrap errors thrown in callbacks.
  • Be ergonomic but safe: Leverage TypeScript's type inference to make common cases simple while keeping type safety. This also helps to get a nice editor experience in e.g. Visual Studio Code.

Installation

$ npm i @badrap/result

Usage

import { Result } from "@badrap/result";

API

Result<T, E extends Error = Error> is a type that wraps either a value that is the result of a succesful computation and of type T, or an error of type E denoting a failed computation.

The type is actually an union of two types: Result.Ok<T, E> that wraps a success value and Result.Err<T, E> that wraps an error.

Result.ok / Result.err

Result.ok returns a new Result.Ok wrapping the given value, while Result.err returns a new Result.Err wrapping the given error.

const res = Result.ok(1);
res.isOk; // true

const res = Result.err(new Error());
res.isOk; // false

const res = Result.err(); // functionally equal to Result.err(new Error())
res.isOk; // false

Result.Ok has an additional property value containing the wrapped value. Similarly, Result.Err has the property error containing the wrapped error. They can be accessed after asserting to TypeScript's type checker that it's safe to do so. The isErr and isOk properties (see below) are handy for this.

const res = Math.random() < 0.5 ? Result.ok(1) : Result.err(new Error("oh no"));

if (res.isErr) {
  // TypeScript now knows that res is a Result.Err, and we can access res.error
  res.error; // Error("oh no")
}

if (res.isOk) {
  // TypeScript now knows that res is a Result.Ok, and we can access res.value
  res.value; // 1
}

Result#isOk / Result#isErr

Result#isOk and Result#isErr are complementary readonly properties. isOk is true for Result.Ok and false for Result.Err.

const ok = Result.ok(1);
ok.isOk; // true

const err = Result.err(new Error());
err.isOk; // false

isErr is the inverse of isOk: false for Result.Ok and true for Result.Err.

const ok = Result.ok(1);
ok.isErr; // false

const err = Result.err(new Error());
err.isErr; // true

Result#unwrap

Return the wrapped value for Result.Ok and throw the wrapped error for Result.Err. This can be modified for providing functions to map the value and error to some value.

const ok = Result.ok(1);
const err = Result.err(new Error("oh no"));

ok.unwrap(); // 1
err.unwrap(); // throws Error("oh no")

ok.unwrap((value) => value + 1); // 2
err.unwrap((value) => value + 2); // throws Error("oh no")

ok.unwrap(
  (value) => value + 1,
  (error) => 0
); // 2
err.unwrap(
  (value) => value + 2,
  (error) => 0
); // 0

As a small extra convenience the result types from the callbacks don't have to be the same. Here's an example Koa.js handler demonstrating this, using an imaginary validate function that returns a Result:

app.use(async ctx =>
  await validate(ctx.request.body).unwrap(
    async (value: any) => {
      ...
    },
    error => {
      ctx.status = 422;
      ctx.body = {
        message: "validation failed"
      };
    }
  )
);

Result#map

Return a new Result where the given function/functions have been applied to the wrapped value and error.

const ok = Result.ok(1);
const err = Result.err(new Error("oh no"));

ok.map((value) => value + 1).unwrap(); // 2
err.map((value) => value + 1).unwrap(); // throws Error("oh no")

ok.map(
  (value) => value + 1,
  (error) => new Error("mapped")
).unwrap(); // 2
err
  .map(
    (value) => value + 1,
    (error) => new Error("mapped")
  )
  .unwrap(); // throws Error("mapped")

Result#chain

const ok = Result.ok(1);
const err = Result.err(new Error("oh no"));

ok.chain((value) => Result.ok(value + 1)).unwrap(); // 2
err.chain((value) => Result.ok(value + 1)).unwrap(); // throws Error("oh no")

ok.chain(
  (value) => Result.ok(value + 1),
  (error) => Result.ok(0)
).unwrap(); // 2
err
  .chain(
    (value) => Result.ok(value + 1),
    (error) => Result.ok(0)
  )
  .unwrap(); // 0

Result.all

Return a new Result where the wrapped value is the collection of the wrapped values of the input array.

Result.all([Result.ok(1), Result.ok("test")]).unwrap(); // [1, "test"]

If any of the input results wraps an error then that result is returned as-is.

Result.all([Result.ok(1), Result.err(new Error("oh no"))]).unwrap(); // throws Error("oh no")

Non-array objects can also be given as arguments. In that case the wrapped output value is also an object.

Result.all({
  x: Result.ok(1),
  y: Result.ok("test"),
}).unwrap(); // { x: 1, y: "test" }

License

This library is licensed under the MIT license. See LICENSE.