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';
const func = (name: string, flag: boolean): Monads.Maybe<string> => {
return flag ? Monads.Some(name) : Monads.None();
}
func.is('some');
func.unwrap();
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';
const func = (name: string): Monads.Result<string, string> => {
return name === 'ts-monadable' ? Monads.Okay(name) : Monads.Error("Invalid name given!");
}
License
MIT