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

ts.data.maybe

v2.3.0

Published

A Typescript implementation of the Maybe data type

Downloads

245

Readme

Maybe

Build Status npm version

Maybe encapsulates the idea of a value that might not be there.

A Maybe value can either be Just some value or Nothing.

type Maybe<T> = Just<T> | Nothing;

Where Nothing is an instance of Nothing, null or undefined, and Just represents any non Nothing value.

If this is new to you, you may want to read the introductory article Safer code with container types about why and how to use this library.

Install

npm install ts.data.maybe --save

Example 1

import { Maybe, just, withDefault, map2 } from 'ts.data.maybe';

interface User {
  email: string;
  name: Maybe<string>;
  surname: Maybe<string>;
}
const user: User = {
  email: '[email protected]',
  name: just('John'),
  surname: just('Doe')
};

const getFullName = (name: string, surname: string) => `${name} ${surname}`;
const maybeFullname = map2(getFullName, user.name, user.surname); // Just<string>('John Doe')
console.log(withDefault(maybeFullname, '')); // 'John Doe'

Example 2

const prices: Maybe<number>[] = [
  just(300),
  nothing(),
  just(500),
  just(150),
  nothing()
];
const sum = (a: number, b: number) => a + b;
const total = prices
  .filter(n => !equals(n, nothing()))
  .reduce((acc, current) => map2(sum, acc, current), just(0));
console.log(withDefault(total, 0)); // 950

Api

(Inspired by elm-lang)

just

just<T>(value: T): Maybe<T>;

Wraps a value in an instance of Just.

just(5); // Just(5) (Maybe<number>)

nothing

nothing<T>(): Maybe<T>;

Creates an instance of Nothing.

nothing<number>(); // Nothing (Maybe<number>)

It's recommended to parametrize the function, otherwise the resolved type will be Maybe<unknown> and type won't flow through map, andThen and so on.

isJust

isJust<T>(value: Maybe<T>): value is Just<T>;

Returns true if a value is an instance of Just.

isJust(nothing()); // false

isNothing

isNothing<T>(value: Maybe<T>): value is Nothing;

Returns true if a value is an instance of Nothing.

isNothing(just(5)); // false
isNothing(undefined); // true
isNothing(null); // true
isNothing(nothing()); // true

withDefault

withDefault<A>(value: Maybe<A>, defaultValue: A): A;

If value is an instance of Just it returns its wrapped value, if it's an instance of Nothing it returns the defaultValue.

withDefault(just(5), 0); // 5
withDefault(nothing(), 'hola'); // 'hola'

caseOf

caseOf<A, B>(caseof: {Just: (v: A) => B; Nothing: () => B;}, value: Maybe<A>): B;

Run different computations depending on whether a Maybe is Just or Nothing and returns the result.

caseOf(
  {
    Nothing: () => 'Do nothing',
    Just: n => `Launch ${n} missiles`
  },
  just(5)
); // 'Launch 5 missiles'

map

map<A, B>(f: (a: A) => B, value: Maybe<A>): Maybe<B>;

Transforms a Maybe value with a given function.

const add1 = (n: number) => n + 1;
map(add1, just(4)); // Just<number>(5)
map(add1, nothing()); // Nothing

There are map2, map3, map4, map5 and mapN functions too:

map2<A, B, C>(
  f: (a: A, b: B) => C,
  a: Maybe<A>, b: Maybe<B>
): Maybe<C>

An example with map2:

const safeParseInt = (num: string): Maybe<number> => {
  const n = parseInt(num, 10);
  return isNaN(n) ? nothing() : just(n);
};
const sum = (x: number, y: number) => x + y;
const safeStringSum = (x: string, y: string) =>
  map2(sum, safeParseInt(x), safeParseInt(y));
safeStringSum('1', '2'); // Just<number>(3)
safeStringSum('a', '2'); // Nothing

If you need to parametrize the function with more than one argument, use the map that matches your parametrization.

mapN only accepts arguments of the same type)

map3<A, B, C, D>(
  f: (a: A, b: B, c: C) => D,
  a: Maybe<A>, b: Maybe<B>, c: Maybe<C>
): Maybe<D>
map4<A, B, C, D, E>(
  f: (a: A, b: B, c: C, d: D) => E,
  a: Maybe<A>, b: Maybe<B>, c: Maybe<C>, d: Maybe<D>
): Maybe<E>
map5<A, B, C, D, E, F>(
  f: (a: A, b: B, c: C, d: D, e: E) => F,
  a: Maybe<A>, b: Maybe<B>, c: Maybe<C>, d: Maybe<D>, e: Maybe<E>
): Maybe<F>
mapN<A, B>(
  f: (...a: A[]) => B,
  ...a: Maybe<A>[]
): Maybe<B>

andThen

andThen<A, B>(f: (a: A) => Maybe<B>, v: Maybe<A>): Maybe<B>;

Chains together many computations that may fail.

const head = (arr: string[]) => (arr.length > 0 ? just(arr[0]) : nothing());
andThen(head, just(['a', 'b', 'c'])); // Just<string>('a')
andThen(head, just([])); // Nothing

equals

equals<T>(a: Maybe<T>, b: Maybe<T>): boolean;

Compares two Maybe instances and returns true when both are Nothing or their wrapped values are strictly equal (===), false otherwise.

equals(just(5), just(5); // true
equals(just(6), just(5); // false
equals(nothing(), just(5); // false
equals(nothing(), nothing(); // true
equals(null, nothing(); // true
equals(undefined, nothing(); // true