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

tsmonads

v3.4.0

Published

Scala-styled monads in TypeScript

Downloads

2,556

Readme

tsmonads

Build Status Dependencies codecov

Monads in TypeScript

tsmonads provides some basic, strictly typed monadic data types for use with TypeScript, namely MayBe, Try and Either monads. It's API is heavily inspired by the according types in the Scala lanaguge API. It has no dependencies and can even be used (although without most of it's benefits) in JavaScript.

Install

NPM: npm install tsmonads

Yarn: yarn add tsmonads

Usage

Simply import the needed functions and classes from "tsmonad.

Breaking changes in 3.x

  • We improved statical type analysis in 3.0. This might lead to incompatibilities when using the library without checking the return values correctly.
  • Global functions (map, flatMap) now return the correct type inferred from the incoming monad's type and the mapping function
  • unsafeLift() was removed from all monads to ensure type safety
  • Where default types are set for Either::Left, Error is now used instead of string

Maybe

A Maybe either contains just a value or nothing. You can easily create new Maybe instances using the maybe factory function:

import {maybe} from "tsmonads";

const five = maybe(5); // Type of Maybe is infereed to be "number"
const n?: number;
[...]
const maybeANumber = maybe(n); // maybeANumber will be a Maybe<number>. Wether it contains a value depends on n being a number or undefined.
const nothingAtAll = maybe<number>(null); // nothingAtAll is Nothing. The generic type has to given explicitly because it cannot be inferred from the argument.

You can use map() and forEach() to transform the Maybe into another contained type or to perform side-effects on it's value. If the maybe is nothing, this is a no-op.

import {maybe} from "tsmonads";
const m = maybe(5);
const seven = m.map(x => x + 2);
const show = m.forEach(x => console.log(x));

You can extract the value from a maybe safely by supplying a default to its orElse()-function. If you need more control, you can use match() and suppy functions to handle the value and nothing case explicitly:

import {maybe} from "tsmonads";
const m: Maybe<number>;
[...]
const extractedValueOrZero = m.orElse(0);
const doSomethingWithTheValue = m.match(x => 2*x, () => -1);

Try

Use Try to safely encapsulate a function call that may throw an exception. Try will either be a Success wrapping the return value of the function or a Failure. Use try via the call factory function.

import {call} from "tsmonads";
const retVal = call(() => 2+5); // retVal is Success<number>(7)
const fail = call(() => throw "Error"); // retVal is Failure("Error");

Like a Maybe, you can use map() and forEach() on a Try. For more control, supply functions to its onSuccess() and onFailure() handlers:

import {call} from "tsmonads";
const retVal = call(() => 2+5);
retVal.onSuccess(s => console.log(s)).onFailure(e => console.log("An error occured"));

If the Error encapsulated by the Failure doesn't matter to you, you can also convert the Try into a Maybe using its toMaybe() method.

Either

An Either holds either a left or a right value. Usually, the left value denotes an error an the right value represents a successful result. Construct it using the left() and right() factory functions.

Further utility functions

Besides the above functions, we also provide some utilities you can use when working with arrays of Monads. The clean() function will extract all values from the monads, throw away any non-values (Nothing, Failure, Left) and return an array of the extracted values. The flatten() function will turn an Array<Monad<T>> into a Monad<Array<T>>. Note that you will receive a "negatively typed" array if even single value in the original array is Nothing/Failure/Left. Otherwise, you'll get a Just<Array<T>>, Success<Array<T>> or Right<Array<T>>. All monad types provide a typed wrapper of flatten as a static method so you don't need to type-cast any results of the flatten method.