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

resultwire

v3.0.0

Published

Rust-like Result types, easily serialized over the wire

Downloads

173

Readme

ResultWire

license CI Tree Shakable

This library provides a minimal Result type and utility functions that can be sent from server to client with the design being inspired by Rust's Result<T, E> enum. It aims to provide better error handling in TypeScript by representing operations that might fail in a type-safe manner.

Why another error-as-value library?

ResultWire doesn't use classes, and instead uses plain objects. This means that it can be sent from server to client, and the helper methods can check the actual objects contents, instead of relying on instanceOf methods. Specifically, this library is designed to be used with Remix's turbo-stream. The Result type and its variants (Ok, Err) can be serialized and deserialized over the network, making it suitable for use in client-server communication.

Installation

npm install resultwire

Usage

import { ok, err, Result, ... } from 'resultwire'

API

Table of Contents

Types

Ok

Represents a successful value wrapped in an Ok variant.

Err

Represents an error value wrapped in an Err variant.

Result

A union type that represents either an Ok value or an Err value.

Functions

ok

Creates a new Ok result.

Example:

const result = ok(42);

err

Creates a new Err result.

Example:

const result = err('Something went wrong');

isOk

Checks if a Result is Ok.

Example:

const result = ok(42);
const isOkResult = isOk(result); // true

isErr

Checks if a Result is Err.

Example:

const result = err('Something went wrong');
const isErrResult = isErr(result); // true

map

Maps a function over the value of an Ok result.

Example:

const result = ok(42);
const mappedResult = map(result, (value) => value * 2);

mapErr

Maps a function over the error of an Err result.

Example:

const result = err('Something went wrong');
const mappedResult = mapErr(result, (error) => new Error(error));

unwrapOr

Unwraps a Result, returning the value if it's Ok, or a default value if it's Err.

Example:

const result = ok(42);
const unwrappedResult = unwrapOr(result, 0); // 42

andThen

Chains a function that returns a Result to the value of an Ok result.

Example:

const result = ok(42);
const chainedResult = andThen(result, (value) => ok(value * 2));

orElse

Chains a function that returns a Result to the error of an Err result.

Example:

const result = err('Something went wrong');
const chainedResult = orElse(result, (error) => ok('Default value'));

match

Matches a Result against two functions, one for Ok and one for Err.

Example:

const result = ok(42);
const matchedResult = match(result, (value) => value * 2, (error) => 0); // 84

fromThrowable

Executes a function that may throw an error and returns the result as a Result.

Example:

const result = fromThrowable(() => {
  // Code that may throw an error
});

fromPromise

Converts a Promise into a Promise<Result>.

Example:

const promise = Promise.resolve(42);
const result = await fromPromise(promise);

combine

Combines an array of Results into a single Result.

Example:

const results = [ok(1), ok(2), ok(3)];
const combinedResult = combine(results);

combineWithAllErrors

Combines an array of Results into a single Result, collecting all errors.

Example:

const results = [ok(1), err('Error 1'), ok(3), err('Error 2')];
const combinedResult = combineWithAllErrors(results);

unsafeUnwrap

Unwraps a Result, throwing an error if it's Err.

Example:

const result = ok(42);
const unwrappedResult = unsafeUnwrap(result); // 42