-
staticNothing.from(value){Maybe}
-
Creates a new
Nothing
from a
value
. If the
value
is already a
Maybe
instance, the
value
is returned unchanged. Otherwise, a new
Nothing
is made with the
value
.
Name |
Type |
Description |
value |
*
|
Value to wrap in a Nothing . |
Returns:
Type |
Description |
Maybe
|
Maybe when is the value already wrapped or Nothing wrapped
value . |
Examples
Nothing from nothing
Nothing.from();
// => Nothing()
Nothing from arbitrary value
Nothing.from(true);
// => Nothing()
Nothing from Just
Nothing.from(Just.from(value));
// => Just.from(value)
Nothing from another Nothing
Nothing.from(Nothing.from());
// => Nothing()
-
-
Applies the function contained in the instance of a
Just
to the value contained in the provided
Just
, producing a
Just
containing the result. If the instance is a
Nothing
, the result
is the
Nothing
instance. If the instance is a
Just
and the provided
Maybe
is
Nothing
, the result is the provided
Nothing
.
Name |
Type |
Description |
other |
Maybe
|
Value to apply to the function wrapped in the Just . |
Returns:
Example
Just#ap
const findPerson = curryN(3, Person.find); // Person.find(name, birthdate, address)
Just.from(findPerson) // => Just(findPerson)
.ap(Just.ofNullable(name)) // => Just(name)
.ap(Just.ofNullable(birthdate)) // => Just(birthdate)
.ap(Just.ofNullable(address)) // => Just(address)
.ifJust(console.log); // => Log Person.find() response
-
-
Applies the provided function to the value contained for a
Just
. The function should return the value
wrapped in a
Maybe
. If the instance is a
Nothing
, the function is ignored and then instance is
returned unchanged.
Name |
Type |
Description |
method |
Chain.<Maybe>
|
The function to invoke with the value. |
Returns:
Type |
Description |
Maybe
|
Maybe wrapped value returned by the provided method . |
Example
Just#chain
// Using lodash/fp/curry and get
const getConfigOption = curry((path, config) => Maybe.ofNullable(get(path, config));
Maybe.ofNullable(config)
.chain(getConfigOption("path.to.option"))
-
-
Determines whether or not the other
is equal in value to the current (this
). This is
not a reference check.
Name |
Type |
Description |
other |
*
|
Other value to check. |
Returns:
Type |
Description |
Boolean
|
true if the two Maybes are equal; false if not equal. |
Examples
Reflexivity
v1.equals(v1) === true;
// => true
Symmetry
v1.equals(v2) === v2.equals(v1);
// => true
Transitivity
(v1.equals(v2) === v2.equals(v3)) && v1.equals(v3)
// => true
-
-
Extends the Maybe. This is used for workflow continuation where the context has shifted.
Type |
Description |
Extend.<Maybe>
|
method - The function to invoke with the value. |
Returns:
Example
Workflow continuation
// Workflow from makeRequest.js
const makeRequest = requestOptions => requestAsPromise(requestOptions)
.then(Just.from)
.catch(Nothing.from);
// Workflow from savePerson.js
const savePerson = curry((requestOptions, optionalPerson) => optionalPerson
.map(Person.from)
.map(person => set("body", person, requestOptions))
.map(makeRequest)
);
// Workflow from processResponse.js
const processResponse = optionalResponse => optionalResponse
.ifJust(console.log);
Maybe.ofNullable(person)
.extend(savePerson({ method: "POST" }))
.extend(processResponse);
-
-
Returns the value if the instance is a
Just
otherwise the
null
.
Returns:
Examples
Just#get
Just.from(value).get();
// => value
Nothing#get
Nothing.from().get();
// => null
-
-
Applies the provided function to the value contain for a
Just
. Any return value from the function is
ignored. If the instance is a
Nothing
, the function is ignored and the instance is returned.
Name |
Type |
Description |
method |
Consumer
|
The function to invoke with the value. |
Returns:
Type |
Description |
Maybe
|
Current instance. |
Examples
Just#ifJust
Just.from(value).ifJust(doSomething); // doSomething(value)
// => Just(value)
Nothing#ifJust
Nothing.from().ifJust(doSomething); // void
// => Nothing()
-
-
Applies the provided function to the value contain for a
Nothing
. Any return value from the function is
ignored. If the instance is a
Just
, the function is ignored and the instance is returned.
Name |
Type |
Description |
method |
Callable
|
The function to invoke. |
Returns:
Type |
Description |
Maybe
|
Current instance. |
Examples
Just#ifNothing
Just.from(value).ifNothing(doSomething); // void
// => Just(value)
Nothing#ifNothing
Nothing.from().ifNothing(doSomething); // doSomething()
// => Nothing()
-
-
Determines whether or not the instance is a
Just
.
Returns:
Type |
Description |
Boolean
|
true if the instance is a Just ; false is not. |
Examples
Just
Just.from(value).isNothing();
// => true
Nothing#isJust
Nothing.from().isNothing();
// => false
-
-
Determines whether or not the instance is a
Nothing
.
Returns:
Type |
Description |
Boolean
|
true if the instance is a Nothing ; false is not. |
Examples
Just#isNothing
Just.from(value).isNothing();
// => false
Nothing#isNothing
Nothing.from().isNothing();
// => true
-
-
Applies the provided function to the value contained for a
Just
which is, in turn, wrapped in a
Just
. If the instance is a
Nothing
, the function is ignored and then instance is returned
unchanged.
Name |
Type |
Description |
method |
function
|
The function to invoke with the value. |
Returns:
Type |
Description |
Maybe
|
Maybe wrapped value mapped with the provided method . |
Example
// Using lodash/fp/flow and sort
Just.from([1, 3, 2]).map(flow(sort, join(", ")));
// => Just("1, 2, 3")
Nothing.from().map(flow(sort, join(", ")));
// => Nothing()
-
-
- See:
-
-
-
Returns the value if the instance is a
Just
otherwise returns the value supplied if the instance is a
Nothing
.
Name |
Type |
Description |
method |
Consumer
|
The function to invoke with the value. |
Returns:
Examples
Just#orElse
Just.from(value).orElse(otherValue);
// => value
Nothing#orElse
Nothing.from().orElse(otherValue);
// => otherValue
-
-
Return the value if the instance is a
Just
otherwise returns the value from the function provided.
Name |
Type |
Description |
method |
Supplier
|
The function supplying the optional value. |
Returns:
Examples
Just#orElseGet
Just.from(value).orElseGet(getOtherValue);
// => value
Nothing#orElseGet
Nothing.from().orElseGet(getOtherValue);
// => otherValue
-
-
Returns the value if the instance is a
Just
otheriwse throws the
Error
supplied by the
function provided.
Name |
Type |
Description |
method |
Supplier
|
The function to invoke with the value. |
Throws:
-
returned by the provided function.
-
-
Type
-
Error
Returns:
Examples
Just#orElseThrow
Just.from(value).orElseThrow(createException);
// => value
Nothing#orElseThrow
Nothing.from().orElseThrow(createException); // throw createException()
-
-
Name |
Type |
Description |
either |
Either
|
Either implementation. |
Returns:
Examples
Just#toEither
const Either = require("lodash-fantasy/data/Either");
Just.from(value).toEither(Either);
// => Either.Right(value);
Nothing#toEither
const Either = require("lodash-fantasy/data/Either");
Nothing.from().toEither(Either);
// => Either.Left(null);
-
inherited
abstracttoPromise(promise){Promise}
-
Converts the Maybe to a Promise
using the provided Promise
implementation.
Name |
Type |
Description |
promise |
Promise
|
Promise implementation. |
Returns:
Type |
Description |
Promise
|
Promise wrapped value . |
Examples
Just#toPromise
const Bluebird = require("bluebird");
Just.from(value).toPromise(Bluebird);
// => Promise.resolve(value);
Nothing#toPromise
const Bluebird = require("bluebird");
Nothing.from().toPromise(Bluebird);
// => Promise.reject(null);
-
-
Returns a
String
representation of the
Maybe
.
Returns:
Type |
Description |
String
|
String representation. |
Examples
Just#toString
Just.from(1).toString();
// => "Maybe.Just(1)"
Nothing#toString
Nothing.from().toString();
// => "Maybe.Nothing(null)"
-
-
Name |
Type |
Description |
validation |
Validation
|
Validation implementation. |
Returns:
Examples
Just#toValidation
const Validation = require("lodash-fantasy/data/Validation");
Just.from(value).toValidation(Validation);
// => Validation.Success(value);
Nothing#toValidation
const Validation = require("lodash-fantasy/data/Validation");
Nothing.from().toValidation(Validation);
// => Validation.Failure([null]);