new Right()
Extends
Methods
-
staticRight.from(value){Either}
-
Creates a new
Right
from avalue
. If thevalue
is already aEither
instance, thevalue
is returned unchanged. Otherwise, a newRight
is made with thevalue
.Name Type Description value
* Value to wrap in a Right
.Returns:
Type Description Either Either
when is thevalue
already wrapped orRight
wrappedvalue
.Examples
Right from nothing
Right.from(); // => Right()
Right from arbitrary value
Right.from(true); // => Right(true)
Right from another Right
Right.from(Right.from(value)); // => Right(value)
Right from Left
Right.from(Left.from(error)); // => Left(error)
-
Applies the function contained in the instance of a
Right
to the value contained in the providedRight
, producing aRight
containing the result. If the instance is aLeft
, the result is theLeft
instance. If the instance is aRight
and the providedEither
isLeft
, the result is the providedLeft
.Name Type Description other
Either Value to apply to the function wrapped in the Right
.Returns:
Type Description Either Right
wrapped applied function orLeft
.Example
Right#ap
const findPerson = curryN(3, Person.find); // Person.find(name, birthdate, address) Right.from(findPerson) // => Right(findPerson) .ap(Right.try(getName())) // => Right(name) .ap(Right.try(getBirthdate())) // => Right(birthdate) .ap(Right.try(getAddress())) // => Right(address) .ifRight(console.log); // => Log Person.find() response
-
Transforms a
Either
by applying the first function to the contained value for aLeft
or the second function for aRight
. The result of each map is wrapped in the corresponding type.Name Type Description failureMap
function Map to apply to the Left
.successMap
function Map to apply to the Right
.Returns:
Type Description Either Either
wrapped value mapped with the corresponding mapping function.Example
// Using lodash/fp/get Either.try(loadFile) .bimap(get("message"), parseFile) // ... other actions in workflow
-
Applies the provided function to the value contained for a
Right
. The function should return the value wrapped in aEither
. If the instance is aLeft
, the function is ignored and then instance is returned unchanged.Name Type Description method
Chain.<Either> The function to invoke with the value. Returns:
Type Description Either Either
wrapped value returned by the providedmethod
.Example
Right#chain
// Using lodash/fp/curry and getOr const getConfigOption = curry((path, config) => Either.Right.from(getOr( Either.Left.from(`Value not found at "${path}"`), path, config ))); Either.of(config) .chain(getConfigOption("path.to.option"))
-
inherited equals(other){Boolean}
-
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 Eithers 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 Either. This is used for workflow continuation where the context has shifted.
Type Description Extend.<Either> method - The function to invoke with the value. Returns:
Type Description Either Example
Workflow continuation
// Workflow from makeRequest.js const makeRequest = requestOptions => requestAsPromise(requestOptions) .then(Right.from) .catch(Left.from); // Workflow from savePerson.js const savePerson = curry((requestOptions, eitherPerson) => eitherPerson .map(Person.from) .map(person => set("body", person, requestOptions)) .map(makeRequest) ); // Workflow from processResponse.js const processResponse = eitherResponse => eitherResponse .ifLeft(console.error) .ifRight(console.log); Either.of(person) .extend(savePerson({ method: "POST" })) .extend(processResponse);
-
inherited get(){*}
-
Returns the value if the instance is a
Right
otherwise thenull
.Returns:
Type Description * Examples
Right#get
Right.from(value).get(); // => value
Left#get
Left.from(error).get(); // => null
-
Applies the provided function to the value contain for a
Left
. Any return value from the function is ignored. If the instance is aRight
, the function is ignored and the instance is returned.Name Type Description method
Consumer The function to invoke with the value. Returns:
Type Description Either Current instance. Examples
Right#ifLeft
Right.from(value).ifLeft(doSomething); // void // => Right(value)
Left#ifLeft
Left.from(error).ifLeft(doSomething); // doSomething(error) // => Left(error)
-
Applies the provided function to the value contain for a
Right
. Any return value from the function is ignored. If the instance is aLeft
, the function is ignored and the instance is returned.Name Type Description method
Consumer The function to invoke with the value. Returns:
Type Description Either Current instance. Examples
Right#ifRight
Right.from(value).ifRight(doSomething); // doSomething(value) // => Right(value)
Left#ifRight
Left.from(error).ifRight(doSomething); // void // => Left(error)
-
inherited isLeft(){Boolean}
-
Determines whether or not the instance is a
Left
.Returns:
Type Description Boolean true
if the instance is aLeft
;false
is not.Examples
Right#isLeft
Right.from(value).isLeft(); // => false
Left#isLeft
Left.from(error).isLeft(); // => true
-
inherited isRight(){Boolean}
-
Determines whether or not the instance is a
Right
.Returns:
Type Description Boolean true
if the instance is aRight
;false
is not.Examples
Right
Right.from(value).isLeft(); // => true
Left#isRight
Left.from(error).isLeft(); // => false
-
Applies the provided function to the value contained for a
Right
which is, in turn, wrapped in aRight
. If the instance is aLeft
, 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 Either Either
wrapped value mapped with the providedmethod
.Example
// Using lodash/fp/flow and sort Right.from([1, 3, 2]).map(flow(sort, join(", "))); // => Right("1, 2, 3") Left.from(error).map(flow(sort, join(", "))); // => Left(error)
-
inherited of()
-
- See:
-
inherited abstractorElse(method){*}
-
Returns the value if the instance is a
Right
otherwise returns the value supplied if the instance is aLeft
.Name Type Description method
Consumer The function to invoke with the value. Returns:
Type Description * Examples
Right#orElse
Right.from(value).orElse(otherValue); // => value
Left#orElse
Left.from(error).orElse(otherValue); // => otherValue
-
inherited abstractorElseGet(method){*}
-
Return the value if the instance is a
Right
otherwise returns the value from the function provided.Name Type Description method
Supplier The function supplying the optional value. Returns:
Type Description * Examples
Right#orElseGet
Right.from(value).orElseGet(getOtherValue); // => value
Left#orElseGet
Left.from(error).orElse(getOtherValue); // => otherValue
-
inherited abstractorElseThrow(method){*}
-
Returns the value if the instance is a
Right
otheriwse throws theError
supplied by the function provided. The function receives the value of theLeft
as its argument.Name Type Description method
function The function to invoke with the value. Throws:
-
returned by the provided function.
- Type
- Error
Returns:
Type Description * Examples
Right#orElseThrow
Right.from(value).orElseThrow(createException); // => value
Left#orElseThrow
Left.from(error).orElseThrow(createException); // throw createException(error)
-
-
Name Type Description maybe
Maybe Maybe implementation. Returns:
Type Description Maybe Maybe
wrappedvalue
.Examples
Right#toMaybe
Right.from(value).toMaybe(Maybe); // => Maybe.Just(value);
Left#toMaybe
Left.from(error).toMaybe(Maybe); // => Maybe.Nothing();
-
inherited abstracttoPromise(promise){Promise}
-
Converts the Either to a
Promise
using the providedPromise
implementation.Name Type Description promise
Promise Promise implementation. Returns:
Type Description Promise Promise
wrappedvalue
.Examples
Right#toPromise
const Bluebird = require("bluebird"); Right.from(value).toPromise(Bluebird); // => Promise.resolve(value);
Left#toPromise
const Bluebird = require("bluebird"); Left.from(error).toPromise(Bluebird); // => Promise.reject(error);
-
inherited abstracttoString(){String}
-
Returns a
String
representation of theEither
.Returns:
Type Description String String
representation.Examples
Right#toString
Right.from(1).toString(); // => "Either.Right(1)"
Left#toString
Left.from(error).toString(); // => "Either.Left(error)"
-
inherited abstracttoValidation(validation){Validation}
-
Name Type Description validation
Validation Validation implementation. Returns:
Type Description Validation Validation
wrappedvalue
.Examples
Right#toValidation
Right.from(value).toValidation(Validation); // => Validation.Success(value);
Left#toValidation
Left.from(error).toValidation(Validation); // => Validation.Failure();