[Maybe Monad](https://en.wikibooks.org/wiki/Haskell/Understanding_monads/Maybe), The Maybe monad represents computations which might "go wrong" by not returning a value.
npm install @sweet-monads/maybeMaybe Monad, The Maybe monad represents computations which might "go wrong" by not returning a value.
> 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,
maybe
identity
> npm install @sweet-monads/maybe
``typescript
import { Maybe, just, none } from "@sweet-monads/maybe";
type User = { email: string; password: string };
function getUser(id: number): Maybe
return just({ email: "test@gmail.com", password: "test" });
}
// Maybe
const user = getUser(1).map(({ email }) => email);
`
- chain
- merge
- none
- just
- from
- fromNullable
- isMaybe
- Maybe#isNone
- Maybe#isJust
- Maybe#or
- Maybe#join
- Maybe#map
- Maybe#asyncMap
- Maybe#apply
- Maybe#asyncApply
- Maybe#chain
- Maybe#asyncChain
- Maybe#fold
- Helpers
#### chain
`typescript`
function chain(fn: (v: A) => Promise
- fn: (v: A) => Promise - function which should be applied asynchronously to Maybe valueMaybe
- Returns function with argument and promised Maybe with Maybe.None or mapped by fn value (could be used inside Promise#then function).
Example:
`typescript
const getValue = async () => just(1);
// Maybe
const result = await getValue()
.then(Maybe.chain(async v => just(v * 2)))
.then(Maybe.chain(async v => none()));
`
#### merge
`typescript`
function merge
function merge
function merge
// ... until 10 elements
- values: Array - Array of Maybe values which will be merged into Maybe of ArrayMaybe
- Returns Maybe of Array which will contain Just if all of array elements was Just else None.
Example:
`typescript
const v1 = just(2); // Maybe
const v2 = just("test"); // Maybe
const v3 = none
merge([v1, v2]); // Maybe<[number, string]>.Just
merge([v1, v2, v3]); // Maybe<[number, string, boolean]>.None
`
#### none
`typescript`
function none
- Returns Maybe with None state
Example:
`typescript`
const v1 = none(); // Maybe
const v2 = none
#### just
`typescript`
function just
- Returns Maybe with Just state which contain value with T type.
Example:
`typescript`
const v1 = just(2); // Maybe
const v2 = just<2>(2); // Maybe<2>.Just
#### from
The same as just
`typescript`
function from
- Returns Maybe with Just state which contain value with T type.
Example:
`typescript`
const v1 = from(2); // Maybe
const v2 = from<2>(2); // Maybe<2>.Just
#### fromNullable
`typescript`
function fromNullable
- Returns Maybe with Just state which contain value with T type if value is not null or undefined and None otherwise.
Example:
`typescript`
const v1 = fromNullable(2); // Maybe
#### isMaybe
`typescript`
function isMaybe
- Returns boolean if given value is instance of Maybe constructor.
Example:
`typescript`
const value: unknown = 2;
if (isMaybe(value)) {
// ... value is Maybe
}
#### Maybe#isNone
`typescript`
function isNone(): boolean;
- Returns true if state of Maybe is None otherwise false
Example:
`typescript
const v1 = just(2);
const v2 = none();
v1.isNone(); // false
v2.isNone(); // true
`
#### Maybe#isJust
`typescript`
function isJust(): boolean;
- Returns true if state of Maybe is Just otherwise false
Example:
`typescript
const v1 = just(2);
const v2 = none();
v1.isJust(); // true
v2.isJust(); // false
`
#### Maybe#or
`typescript`
function or
- Returns Maybe. If state of this is Just then this will be returned otherwise x argument will be returned
Example:
`typescript
const v1 = just(1);
const v2 = none
const v3 = none
const v4 = just(4);
v1.or(v2); // v1 will be returned
v2.or(v1); // v1 will be returned
v2.or(v3); // v3 will be returned
v1.or(v4); // v1 will be returned
v2.or(v3).or(v1); // v1 will be returned
v2.or(v1).or(v3); // v1 will be returned
v1.or(v2).or(v3); // v1 will be returned
`
#### Maybe#join
`typescript`
function join
- this: Maybe - Maybe instance which contains other Maybe instance as Just value.Maybe
- Returns unwrapped - if current Maybe has Just state and inner Maybe has Just state then returns inner Maybe Just, otherwise returns Maybe None.
Example:
`typescript
const v1 = just(just(2));
const v2 = just(none());
const v3 = none
v1.join(); // Maybe.Just with value 2
v2.join(); // Maybe.None without value
v3.join(); // Maybe.None without value
`
#### Maybe#map
`typescript`
function map
- Returns mapped by fn function value wrapped by Maybe if Maybe is Just otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
const newVal1 = v1.map(a => a.toString()); // Maybe
const newVal2 = v2.map(a => a.toString()); // Maybe
`
#### Maybe#mapNullable
`typescript`
function mapNullable
- Returns mapped by fn function value wrapped by Maybe if Maybe is Just and the returned value is not null or undefined otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
const newVal1 = v1.mapNullable(a => a.toString()); // Maybe
const newVal2 = v2.mapNullable(a => a.toString()); // Maybe
const newVal3 = v2.mapNullable
const newVal4 = v2.mapNullable
`
#### Maybe#mapNullable
`typescript`
function map
- Returns mapped by fn function value wrapped by Maybe if Maybe is Just otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
const newVal1 = v1.map(a => a.toString()); // Maybe
const newVal2 = v2.map(a => a.toString()); // Maybe
`
##### Maybe#asyncMap
`typescript`
function asyncMap
- Returns Promise with mapped by fn function value wrapped by Maybe if Maybe is Just otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
// Promise
const newVal1 = v1.asyncMap(a => Promise.resolve(a.toString()));
// Promise
const newVal2 = v2.asyncMap(a => Promise.resolve(a.toString()));
`
##### Maybe#apply
`typescript`
function apply(this: Maybe<(a: A) => B>, arg: Maybe): Maybe;
function apply(this: Maybe, fn: Maybe<(a: A) => B>): Maybe;
- this | fn - function wrapped by Maybe, which should be applied to value argarg | this
- - value which should be applied to fnfn
- Returns mapped by function value wrapped by Maybe if Maybe is Just otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
const fn1 = just((a: number) => a * 2);
const fn2 = none<(a: number) => number>();
const newVal1 = fn1.apply(v1); // Maybe
const newVal2 = fn1.apply(v2); // Maybe
const newVal3 = fn2.apply(v1); // Maybe
const newVal4 = fn2.apply(v2); // Maybe
`
##### Maybe#asyncApply
Async variant of Maybe#apply
`typescript`
function asyncApply(
this: Maybe<(a: Promise | A) => Promise>,
arg: Maybe
): Promise
function asyncApply(this: Maybe
- this | fn - function wrapped by Maybe, which should be applied to value argarg | this
- - value which should be applied to fnPromise
- Returns with mapped by fn function value wrapped by Maybe if Maybe is Just otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
const fn1 = just((a: number) => Promise, resolve(a * 2));
const fn2 = none<(a: number) => Promise
const newVal1 = fn1.apply(v1); // Promise
const newVal2 = fn1.apply(v2); // Promise
const newVal3 = fn2.apply(v1); // Promise
const newVal4 = fn2.apply(v2); // Promise
`
#### Maybe#chain
`typescript`
function chain
- Returns mapped by fn function value wrapped by Maybe if Maybe is Just and returned by fn value is Just too otherwise None
Example:
`typescript
const v1 = just(2);
const v2 = none
const newVal1 = v1.chain(a => just(a.toString())); // Maybe
const newVal2 = v1.chain(a => none()); // Maybe
const newVal3 = v2.chain(a => just(a.toString())); // Maybe
const newVal4 = v2.chain(a => none()); // Maybe
`
##### Maybe#asyncChain
`typescript`
function asyncChain
- Returns Promise with mapped by fn function value wrapped by Maybe if Maybe is Just otherwise None
Example:
`typescript`
const v1 = just(2);
const v2 = none
// Promise
const newVal1 = v1.asyncChain(a => Promise.resolve(just(a.toString())));
// Promise
const newVal2 = v1.asyncChain(a => Promise.resolve(none()));
// Promise
const newVal3 = v2.asyncChain(a => Promise.resolve(just(a.toString())));
// Promise
const newVal4 = v2.asyncChain(a => Promise.resolve(none()));
##### Maybe#fold
`typescript`
function fold
- Returns value mapped by one of mapper functions. If Maybe is Just then mapJust is result of mapJust mapNone
is returned, otherwise return of gets returned.
Example:
`typescript
const v1 = just(2);
const v2 = none
// "just: 4"
const newVal1 = v1.fold(() => 'none', value => 'just: '+value*2)
// "none"
const newVal2 = v1.fold(() => 'none', value => 'just: '+value*2)
`
#### Helpers
`typescript`
// Value from Maybe instance
const { value } = just(2); // number | undefined
`typescript
just(2).unwrap(); // returns 2
none().unwrap(); // Throws error
just(2).unwrap(() => new Error("NEVER!")); // returns 2
none().unwrap(() => new CustomError("My error")); // Throws CustomError
just(2).unwrapOr(3) // returns 3
none().unwrapOr(3) // returns 2
just(2).unwrapOrElse(num => num * 2) // returns 4
none().unwrapOrElse(num => num * 2) // returns 2
``
MIT (c) Artem Kobzar see LICENSE file.