Maybe

Maybe

new Maybe(val)

Private constructor. Use Maybe.of() instead.

Source:
Parameters:
Name Type Description
val *

Object, string, or number

Methods

(static) of(val) → {Maybe}

Public constructor. Creates an instance of Maybe.

Source:
Parameters:
Name Type Description
val *

Object, string, number, or function (direct object access)

Returns:
Type:
Maybe

A Maybe monad

Example
const exampleObj = {
     foo: 'bar',
     baz: [1,2,3]
};

const maybe1 = Maybe.of(exampleObj);
const maybe2 = Maybe.of(() => exampleObj.baz.1);

chain(fn) → {Maybe}

Chain together functions that return Maybe monads

Source:
Parameters:
Name Type Description
fn function

Function that is passed the value of the calling monad, and returns a monad.

Returns:
Type:
Maybe

A monad created from the result of the transformation

Example
function addOne (val) {
  return Maybe.of(val + 1);
}

const three = Maybe.of(1)
 .chain(addOne)
 .chain(addOne)
 .join();

isJust() → {boolean}

Determine whether the monad's value exists

Source:
Returns:
Type:
boolean

true if the value is defined, false if the monad is null or undefined.

Example
const maybe1 = Maybe.of(123);
const maybe2 = Maybe.of(undefined);

maybe1.isJust();    // true
maybe2.isJust();    // false

isNothing() → {boolean}

Determine whether the monad's value is null or undefined

Source:
Returns:
Type:
boolean

true if the value is null or undefined, false if the value is defined.

Example
const maybe1 = Maybe.of(null);
const maybe2 = Maybe.of(123);

maybe1.isNothing();    // true
maybe2.isNothing()     // false

join() → {*}

Get the monad's value

Source:
Returns:
Type:
*

Returns the value of the monad

Example
const maybe1 = Maybe.of(123);
const maybe2 = Maybe.of(null);

maybe1.join();   // 123
maybe2.join();   // null

map(transform) → {Maybe}

Apply a transformation to the monad

Source:
Parameters:
Name Type Description
transform function

The transformation function to apply to the monad

Returns:
Type:
Maybe

A monad created from the result of the transformation

Example
Maybe.of(1).map(val => val + 1);

orElse(defaultValue) → {Maybe}

Chain to the end of a monad as the default value to return if the isNothing() is true

Source:
Parameters:
Name Type Description
defaultValue string

Return this value when join() is called and isNothing() is true

Returns:
Type:
Maybe

A monad containing the default value

Example
const maybe1 = Maybe.of(null);

maybe1.orElse('N/A');
maybe1.join();   // 'N/A'

path(path) → {Maybe}

Get a value on the monad given a property path in string form

Source:
Parameters:
Name Type Description
path string

A period delimited string representing the path (e.g. 'foo.bar.baz) to search

Returns:
Type:
Maybe

A monad containing the value at a given path

Example
const exampleObj = {
     foo: 'bar',
     baz: [1,2,3]
};

const maybeArrayVal = Maybe.of(exampleObj).path('baz.0');

maybeArrayVal.join();     // 1

prop(property) → {Maybe}

Get a value on the monad given a single property

Source:
Parameters:
Name Type Description
property string | number

Look for this property on the monad

Returns:
Type:
Maybe

A monad containing the value of a given property or index

Example
const exampleObj = {
     foo: 'bar',
     baz: [1,2,3]
};

const maybeBar = Maybe.of(exampleObj).prop('foo');

maybeBar.join();     // 'bar'

props(properties) → {Maybe}

Get a value on the monad given a property path in argument form

Source:
Parameters:
Name Type Description
properties string | number

Argument list that represents the property path to search

Returns:
Type:
Maybe

A monad containing the value at a given path

Example
const exampleObj = {
     foo: 'bar',
     baz: [1,2,3]
};

const maybeArrayVal = Maybe.of(exampleObj).props('baz', 0);

maybeArrayVal.join();     // 1