1.0.2 • Published 3 years ago
ts-monadable v1.0.2
TS-Monadable
A simple set of Monadic structures and typings for TypeScript projects.
Install
npm install ts-monadableAPI
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!");
}