ts-monadable
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

TS-Monadable

A simple set of Monadic structures and typings for TypeScript projects.

Install

npm install ts-monadable

API

Maybe

The Maybe structures can be used to define an optional value. This value may be something or nothing. By wrapping return values in this monad, it forces explicit handling of empty returns.

import * as Monads from 'ts-monadable';

// simple constructor for an example
const func = (name: string, flag: boolean): Monads.Maybe<string> => {
    return flag ? Monads.Some(name) : Monads.None();
}

// various monadic methods can then be invoked
func.is('some'); // type-guard
func.unwrap(); // safe-unwrapping (throws on `none` with no alternative)

Result

Similar to the Maybe structure, Result structures explicitly define an alternative value for an error. This allows adding failure meta-data to results.

import * as Monads from 'ts-monadable';

// simple constructor for an example
const func = (name: string): Monads.Result<string, string> => {
    return name === 'ts-monadable' ? Monads.Okay(name) : Monads.Error("Invalid name given!");
}

License

MIT

Package Sidebar

Install

npm i ts-monadable

Weekly Downloads

3

Version

1.0.2

License

MIT

Unpacked Size

17.1 kB

Total Files

13

Last publish

Collaborators

  • rroesslerio