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 🙏

© 2025 – Pkg Stats / Ryan Hefner

perchance

v1.0.1

Published

A simple maybe monad for JavaScript

Readme

perchance

A simple maybe monad for JavaScript. Handle nulls and undefineds more gracefully with a maybe monad similar to functional languages like Haskell.

Install

$ npm install perchance

Usage

Perchance comes with three functions, maybe, Just, and Nothing. You can use these functions to create more functional code that avoids lots of if-else checks for null or undefined. The real beauty of the maybe monad comes from being able to wrap a value, apply transformations to it, and then unwrap the final value whether it's a real value or null or undefined.

Just

Just is a function that returns an internal _Just object that wraps regular values ike numbers, strings, objects, etc. The only two values that you can't wrap are null and undefined. When you try to wrap them, you get back a _Nothing singleton instead. We'll see more of _Nothing later below.

import { Just } from 'perchance';

Just(42)
  .map(n => n * 2)
  .unwrap(); // returns 84

Just('hello')
  .map(greeting => greeting + ' world')
  .unwrap(phrase => phrase + '!'); // map in unwrap to return 'hello world!'

Nothing

Nothing is a function that returns an internal singleton object _Nothing. _Nothing acts as a placeholder for null and undefined but has the same API as _Just objects. This allows you to use OO-type functional code to deal with nulls and undefineds in your code without if-else checks.

The real power comes in the unwrap method. The unwrap method actually takes two functions. The first function will only be invoked if the receiver is an instance of _Just. If the receiver is _Nothing, then the second function will be invoked. With the syntactical sugar of ES2015 arrow functions, this allows you to write code close to pattern matching in functional languages.

Without the second function, unwrap will throw an error on Nothing.

import { Nothing } from 'perchance';

Nothing()
  .map(n => n * 2)
  .unwrap(
    n => n,               // won't be invoked
    _ => 'Got nothing'    // will be invoked
  ); // return 'Got nothing'

Nothing().unwrap(); // throws an error

maybe

maybe is a convenience function for wrapping values without using Just or Nothing explicitly. As you might guess, if you wrap null or undefined with maybe, then you'll get back _Nothing. Otherwise, you'll get back that value wrapped with _Just.

import { maybe } from 'perchance';

const half = (n) => {
  if (n % 2 === 0) {
    return n / 2;
  }

  return null;
}

maybe(half(4))
  .unwrap(
    v => v,
    _ => 'Could not halve integer'
  ); // returns 2

maybe(half(5))
  .unwrap(
    v => v,
    _ => 'Could not halve integer'
  ); // returns 'Could not halve integer'

API

maybe

maybe(null | undefined): _Nothing
maybe(value: T): _Just<T>

Just

Just(null | undefined): _Nothing
Just(value: T): _Just<T>

_Just#map

Transforms the wrapped value, returning a new instance of _Just. If the mapping function returns null or undefined, then it returns _Nothing.

alias: fmap

_Just<T>
  #map(fn: (value: T) => U): _Just<U>

_Just<T>
  #map(fn: (value: T) => null | undefined): _Nothing

_Just#ap

Applies a wrapped function to the wrapped value of another _Just instance or a no-op if passed _Nothing.

alias: apply

_Just<fn: (value: T) => U>
  #ap(mappable: _Nothing): _Nothing

_Just<fn: (value: T) => U>
  #ap(mappable: _Just<T>): _Just<U>

_Just#bind

Takes a function that returns a _Just or _Nothing, applies that function to the wrapped value, and returns a new _Just with that wrapped value (or _Nothing if the function argument returned _Nothing).

_Just<T>
  #bind(fn: (value: T) => _Just<U>): _Just<U>

_Just<T>
  #bind(fn: (value: T) => _Nothing): _Nothing

_Just#unwrap

Unwraps the inner value if no arguments are passed. Invokes and returns the return value of the first function argument otherwise, passing in the wrapped value to the function.

_Just<T>
  #unwrap(): T

_Just<T>
  #unwrap(fn: (value: T) => U): U

Nothing

Nothing(): _Nothing

_Nothing methods

aliases:

| method | alias name | | ------ | ---------- | | map | fmap | | ap | apply |

_Nothing<T>
  #map(fn: (value: T) => U): _Nothing

_Nothing<fn: (value: T) => U>
  #ap(mappable: any): _Nothing

_Nothing<T>
  #bind(fn: (value: T) => _Just<U>): _Nothing

_Nothing<T>
  #bind(fn: (value: T) => _Nothing): _Nothing

_Nothing#unwrap

Invokes and returns the return value of the second function argument. Throws an error if the second function argument is missing.

_Nothing<T>
  #unwrap(): throws

_Nothing<T>
  #unwrap(fn: () => T): T