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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nand.computer/monads

v1.0.0

Published

TypeScript monad utilities for functional programming

Readme

A minimal functional utility library for TypeScript, inspired by Rust's Result and Option. It provides safe, composable handling of errors and optional values.

  • Result<T> – represents a computation that may succeed (ok) or fail (err).
  • Option<T> / Maybe<T> – represents a value that may exist (Some) or be absent (None).
  • SafePromise<T> – wraps async computations, converting exceptions into Result<T>.
  • Supports chainable operations: map, andThen, filter, zip, transpose, mapErr, or, orElse, unwrap, expect, inspect.

Goals:

  1. Explore functional programming in TypeScript.
  2. Provide a small, dependency-free, composable monads toolkit.
  3. Enable safe handling of errors and optional values without exceptions.

Status: eperimental. API may change. contributions welcome.

Installation

bun add @nand.computer/monads
import { ok, err, Some, None, wrap, todo } from "@nand.computer/monads";

Result

Result<T> is either:

  • ok(value: T) – success
  • err(message: string) – failure

Methods

  • .map(fn) – transform success value
  • .andThen(fn) – chain computations
  • .mapErr(fn) – transform the error
  • .filter(predicate, errMsg) – fail if predicate is false
  • .zip(otherResult) – combine two results
  • .transpose() – convert Result<Option<T>>Option<Result<T>>
  • .unwrap() – return value or throw
  • .expect(msg) – return value or throw with custom message
  • .ok() – returns the value or null
  • .err() – returns the error message or null

Option / Maybe

Option<T> represents an optional value:

  • Some(value) – value exists
  • None() – value absent

Methods

  • .map(fn) – transform value
  • .andThen(fn) – chain computations
  • .unwrap() – return value or throw
  • .expect(msg) – return value or throw with message
  • .filter(predicate) – convert to None if predicate fails

SafePromise

Wraps async functions to automatically return Result<T>:

const safe = await wrap(async () => 10 * 2);
console.log(safe.ok()); // 20

Examples

// Basic map/andThen/mapErr
const r1 = ok(10)
  .map((x) => x * 2)
  .andThen((x) => (x > 15 ? ok(x) : err<number>("Too small")))
  .mapErr((e) => `Error happened: ${e}`);
console.log(r1.ok(), r1.err()); // 20, null

// Filter
const filtered = ok(100).filter((x) => x > 50, "Less than 50");
console.log(filtered.ok(), filtered.err()); // 100, null

// Zip
const zipped = ok(10).zip(ok("hello"));
console.log(zipped.ok()); // [10, "hello"]

// Option + transpose
const rOpt: Result<Option<number>> = ok(Some(5));
const transposed = rOpt.transpose();
console.log(transposed.isSome ? transposed.value!.ok() : null); // 5

const rOptNone: Result<Option<number>> = ok(None());
const transposedNone = rOptNone.transpose();
console.log(transposedNone.isNone); // true

// SafePromise / wrap
const asyncFunc = async (x: number) =>
  x > 0
    ? x * 2
    : (() => {
        throw new Error("Negative");
      })();
const safe = await wrap(() => asyncFunc(5));
console.log(safe.ok()); // 10
const safeErr = await wrap(() => asyncFunc(-1));
console.log(safeErr.err()); // "Unexpected error"

// Complex chaining
const complex = await wrap(async () => 20).then((r) =>
  r
    .filter((x) => x > 10, "Too small")
    .andThen((x) => ok(x * 3))
    .zip(ok(50))
    .map(([a, b]) => a + b),
);
console.log(complex.ok()); // 110

Goals

  1. Learn, experiment, and have fun with functional patterns.
  2. Provide a safe, composable substrate for async and optional computations.
  3. Maintain simplicity and extensibility.