@sweet-monads/maybe
Maybe Monad, The Maybe monad represents computations which might "go wrong" by not returning a value.
This library belongs to sweet-monads project
sweet-monads — easy-to-use monads implementation with static types definition and separated packages.
- No dependencies, one small file
- Easily auditable TypeScript/JS code
- Check out all libraries:
either,
iterator,
interfaces,
Usage
npm install @sweet-monads/maybe
import { Maybe, just, none } from "@sweet-monads/maybe";
type User = { email: string, password: string };
function getUser(id: number): Maybe<User> {
return just({ email: "test@gmail.com", password: "test" });
}
const user = getUser(1).map(({ email }) => email);
API
merge
function merge<V1>(values: [Maybe<V1>]): Maybe<[V1]>;
function merge<V1, V2>(values: [Maybe<V1>, Maybe<V2>]): Maybe<[V1, V2]>;
function merge<V1, V2, V3>(values: [Maybe<V1>, Maybe<V2>, Maybe<V3>]): Maybe<[V1, V2, V3]>;
values: Array<Maybe<T>>
- Array of Maybe values which will be merged into Maybe of Array- Returns
Maybe<Array<T>>
Maybe of Array which will contain Just<Array<T>>
if all of array elements was Just<T>
else None
.
Example:
const v1 = just(2);
const v2 = just("test");
const v3 = none<boolean>();
merge([v1, v2])
merge([v1, v2, v3])
none
function none<T>(): Maybe<T>;
- Returns
Maybe
with None
state
Example:
const v1 = none();
const v2 = none<number>();
just
function just<T>(value: T): Maybe<T>;
- Returns
Maybe
with Just
state which contain value with T
type.
Example:
const v1 = just(2);
const v2 = just<2>(2);
from
The same as just
function from<T>(value: T): Maybe<T>;
- Returns
Maybe
with Just
state which contain value with T
type.
Example:
const v1 = from(2);
const v2 = from<2>(2);
isMaybe
function isMaybe<T>(value: unknown | Maybe<T>): value is Maybe<T>;
- Returns
boolean
if given value
is instance of Maybe constructor.
Example:
const value: unknown = 2;
if (isMaybe(value)) {
}
Maybe#isNone
function isNone(): boolean;
- Returns
true
if state of Maybe
is None
otherwise false
Example:
const v1 = just(2);
const v2 = none();
v1.isNone()
v2.isNone()
Maybe#isJust
function isJust(): boolean;
- Returns
true
if state of Maybe
is Just
otherwise false
Example:
const v1 = just(2);
const v2 = none();
v1.isJust()
v2.isJust()
Maybe#or
function or<T>(x: Maybe<T>): Maybe<T>;
- Returns
Maybe<T>
. If state of this
is Just
then this
will be returned otherwise x
argument will be returned
Example:
const v1 = just(2);
const v2 = none();
const v3 = none();
const v4 = none();
v1.or(v2)
v2.or(v1)
v2.or(v3)
v1.or(v4)
v2.or(v3).or(v1)
v2.or(v1).or(v3)
v1.or(v2).or(v3)
Maybe#join
function join<V>(this: Maybe<Maybe<V>>): Maybe<V>;
this: Maybe<Maybe<V>>
- Maybe
instance which contains other Maybe
instance as Just
value.- Returns unwrapped
Maybe
- if current Maybe
has Just
state and inner Maybe
has Just
state then returns inner Maybe
Just
, otherwise returns Maybe
None
.
Example:
const v1 = just(just(2));
const v2 = just(none());
const v3 = none<Maybe<number>>();
v1.join()
v2.join()
v3.join()
Maybe#map
function map<Val, NewVal>(fn: (val: Val) => NewVal): Maybe<NewVal>;
- Returns mapped by
fn
function value wrapped by Maybe
if Maybe
is Just
otherwise None
Example:
const v1 = just(2);
const v2 = none<number>();
const newVal1 = v1.map(a => a.toString());
const newVal2 = v2.map(a => a.toString());
Maybe#asyncMap
function asyncMap<Val, NewVal>(fn: (val: Val) => Promise<NewVal>): Promise<Maybe<NewVal>>;
- Returns
Promise
with mapped by fn
function value wrapped by Maybe
if Maybe
is Just
otherwise None
Example:
const v1 = just(2);
const v2 = none<number>();
const newVal1 = v1.asyncMap(a => Promise.resolve(a.toString()));
const newVal2 = v2.asyncMap(a => Promise.resolve(a.toString()));
Maybe#apply
function apply<A, B>(this: Maybe<(a: A) => B>, arg: Maybe<A>): Maybe<B>;
function apply<A, B>(this: Maybe<A>, fn: Maybe<(a: A) => B>): Maybe<B>;
this | fn
- function wrapped by Maybe, which should be applied to value arg
arg | this
- value which should be applied to fn
- Returns mapped by
fn
function value wrapped by Maybe
if Maybe
is Just
otherwise None
Example:
const v1 = just(2);
const v2 = none<number>();
const fn1 = just((a: number) => a * 2);
const fn2 = none<(a: number) => number>();
const newVal1 = fn1.apply(v1);
const newVal2 = fn1.apply(v2);
const newVal3 = fn2.apply(v1);
const newVal4 = fn2.apply(v2);
Maybe#asyncApply
Async variant of Maybe#apply
function asyncApply<A, B>(this: Maybe<(a: Promise<A> | A) => Promise<B>>, arg: Maybe<Promise<A> | A>): Promise<Maybe<B>>;
function asyncApply<A, B>(this: Maybe<Promise<A> | A>, fn: Maybe<(a: Promise<A> | A) => Promise<B>>): Promise<Maybe<B>>;
this | fn
- function wrapped by Maybe, which should be applied to value arg
arg | this
- value which should be applied to fn
- Returns
Promise
with mapped by fn
function value wrapped by Maybe
if Maybe
is Just
otherwise None
Example:
const v1 = just(2);
const v2 = none<number>();
const fn1 = just((a: number) => Promise,resolve(a * 2));
const fn2 = none<(a: number) => Promise<number>>();
const newVal1 = fn1.apply(v1);
const newVal2 = fn1.apply(v2);
const newVal3 = fn2.apply(v1);
const newVal4 = fn2.apply(v2);
Maybe#chain
function chain<Val, NewVal>(fn: (val: Val) => Maybe<NewVal>): Maybe<NewVal>;
- Returns mapped by
fn
function value wrapped by Maybe
if Maybe
is Just
and returned by fn
value is Just
too otherwise None
Example:
const v1 = just(2);
const v2 = none<number>();
const newVal1 = v1.chain(a => just(a.toString()));
const newVal2 = v1.chain(a => none());
const newVal3 = v2.chain(a => just(a.toString()));
const newVal4 = v2.chain(a => none());
Maybe#asyncChain
function asyncChain<Val, NewVal>(fn: (val: Val) => Promise<Maybe<NewVal>>): Promise<Maybe<NewVal>>;
- Returns
Promise
with mapped by fn
function value wrapped by Maybe
if Maybe
is Just
otherwise None
Example:
const v1 = just(2);
const v2 = none<number>();
const newVal1 = v1.asyncChain(a => Promise.resolve(just(a.toString())));
const newVal2 = v1.asyncChain(a => Promise.resolve(none()));
const newVal3 = v2.asyncChain(a => Promise.resolve(just(a.toString())));
const newVal4 = v2.asyncChain(a => Promise.resolve(none()));
Helpers
const { value } = just(2);
License
MIT (c) Artem Kobzar see LICENSE file.