lift.js
Because it can always be shorter.
Introduction
lift.js is a compact monad opinionated javascript library. It implements Just, Maybe, Validation(Valid) and a nice Monad factory. The unit comes with a lift function so you can add functionnality later in code to your monad. It's ment to be flexible and faster to use. It's written with es6 so it's less than 100 lines.
Installation
lift.js
can be required directly for es next project or you can use the lift-min.js
for all legacy applications.
npm
npm install liftjs
lift (lifting)
const justWithLog = Just(5).lift('log',console.log);
justWithValue.log();
All Monads
The folowing function are available on all monads.
bind, alias: chain flatMap
bind(func, args)
const justWithValue = Just(5).bind((value)=> Just(value));
of, alias: pure
of(value)
const justWithValue = Just(5).of(6);
const justWithValue = Just(5).of(Just(6));
get
get()
const value = Just(5).get();
map
map(func)
const justWithValue = Just(7).map(value => value * 2);
join
join()
const justWithValue = Just(Just(5)).join()
toMaybe
toMaybe()
const maybeWithValue = Just(5).toMaybe();
run
run(func)
Just(5).run(value => console.log(value));
Maybe
none, alias: nothing
none()
const maybeWithValue = Maybe().none()
const maybeWithValue = Maybe().nothing()
const maybeWithValue = Maybe()
const maybeWithValue = Maybe(undefined)
const maybeWithValue = Maybe(null)
isNone, alias: isNothing
isNone()
const value = Maybe(5).isNone();
isJust, alias: orSome
isJust()
const value = Maybe(5).isJust();
orJust, alias: orSome
orJust()
const maybeWithValue = Maybe().orJust(15);
orElse
orElse(monad)
const maybeWithValue = Maybe(5).orElse(Maybe(15));
const maybeWithValue = Maybe().orElse(Just(15));
Links
Author
Written and maintained by @pre63.
Sponsored by @atomable.
Based on Douglas Crockford MONAD.
Special tanks to Monet for the inspiration and a bunch of tests.