Skip to content

rroessler/npm.ts-monadable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

TypeScript Monads Library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors