A symbol indicating whether the Either is Left or Right. Primarily for internal use.
Checks if the Either is a Left value.
true if the Either is Left, otherwise false.
console.log(Left('error').isLeft()); // true
console.log(Right(42).isLeft()); // false
Checks if the Either is a Right value.
true if the Either is Right, otherwise false.
console.log(Right(42).isRight()); // true
console.log(Left('error').isRight()); // false
Converts the Either to an Option of its Left value.
An Option containing the Left value if present, otherwise None.
const leftOption = Left('error').left(); // Some('error')
const rightOption = Right(42).left(); // None
Transforms the Either by applying a function to its Left value if present, chaining multiple operations.
The Either returned by the function if the original was Left, otherwise the original Right.
const either = Left('initial error');
const chained = either.leftAndThen((err) => Left(`Processed ${err}`));
console.log(chained.unwrapLeft()); // "Processed initial error"
Maps an Either by applying a function to its Left value, leaving Right untouched.
A new Either with the Left value transformed if it was Left, otherwise the original Right.
const either = Left('error');
const mapped = either.mapLeft((err) => `Error: ${err}`);
console.log(mapped.unwrapLeft()); // "Error: error"
Maps an Either by applying a function to its Right value, leaving Left untouched.
A new Either with the Right value transformed if it was Right, otherwise the original Left.
const either = Right(42);
const mapped = either.mapRight((val) => val.toString());
console.log(mapped.unwrapRight()); // "42"
Performs a match operation on the Either, allowing for branching logic based on its state.
The result of applying the corresponding function based on the Either's state.
const either = Left('error');
const result = either.match({
left: (err) => `Error: ${err}`,
right: (val) => `Success: ${val}`,
});
console.log(result); // "Error: error"
Converts the Either to an Option of its Right value.
An Option containing the Right value if present, otherwise None.
const leftOption = Left('error').right(); // None
const rightOption = Right(42).right(); // Some(42)
Transforms the Either by applying a function to its Right value if present, chaining multiple operations.
The Either returned by the function if the original was Right, otherwise the original Left.
const either = Right(42);
const chained = either.rightAndThen((val) => Right(val.toString()));
console.log(chained.unwrapRight()); // "42"
Returns the Left value if present, otherwise returns the provided default value.
The default value to return if the Either is Right.
The Left value or the default.
console.log(Left('error').unwrapLeftOr('default')); // 'error'
console.log(Right(42).unwrapLeftOr('default')); // 'default'
Returns the Right value if present, otherwise returns the provided default value.
The default value to return if the Either is Left.
The Right value or the default.
console.log(Right(42).unwrapRightOr(100)); // 42
console.log(Left('error').unwrapRightOr(100)); // 100
Implementation of the Either interface for a Right value.