Interface Either<L, R>

An Either jonad.

Represents a value that can be one of two types, "Left" or "Right". Associates a set of utility functions to work with the jonad.

interface Either<L, R> {
    getLeftOrThrow(): L;
    getRightOrThrow(): R;
    isLeft(): boolean;
    isRight(): boolean;
    leftOr(fallback: L | Mapper<R, L>): L;
    leftOrAsync(fallback: L | Promise<L> | AsyncMapper<R, L>): Promise<L>;
    mapLeft<V>(mapper: Mapper<L, V>): Either<V, R>;
    mapLeftAsync<V>(mapper: AsyncMapper<L, V>): Promise<Either<V, R>>;
    mapRight<V>(mapper: Mapper<R, V>): Either<L, V>;
    mapRightAsync<V>(mapper: AsyncMapper<R, V>): Promise<Either<L, V>>;
    match<V>(onLeft: Mapper<L, V>, onRight: Mapper<R, V>): V;
    matchAsync<V>(onLeft: AsyncMapper<L, V>, onRight: AsyncMapper<R, V>): Promise<V>;
    rightOr(fallback: R | Mapper<L, R>): R;
    rightOrAsync(fallback: R | Promise<R> | AsyncMapper<L, R>): Promise<R>;
    tapLeft(callback: Consumer<L>): Either<L, R>;
    tapLeftAsync(callback: AsyncConsumer<L>): Promise<Either<L, R>>;
    tapRight(callback: Consumer<R>): Either<L, R>;
    tapRightAsync(callback: AsyncConsumer<R>): Promise<Either<L, R>>;
}

Type Parameters

  • L
  • R

Hierarchy (view full)

Methods

  • Returns the value if it is a Left, otherwise throws an error.

    NOTE: This function is unsafe and should only be used for testing.

    Returns L

    The value if it is a Left.

    GetValueError if the value is a Right.

  • Returns the value if it is a Right, otherwise throws an error.

    NOTE: This function is unsafe and should only be used for testing.

    Returns R

    The value if it is a Right.

    GetValueError if the value is a Left.

  • Checks if the value is a Left.

    Returns boolean

    true if the value is a Left, false otherwise.

    const value = Either.left(42);
    value.isLeft(); // => true
  • Checks if the value is a Right.

    Returns boolean

    true if the value is a Right, false otherwise.

    const value = Either.right(42);
    value.isRight(); // => true
  • Returns the value if it is a Left, otherwise returns a default value.

    Parameters

    • fallback: L | Mapper<R, L>

      The default value (as-is or produced from a callback) to return if the value is a Right.

    Returns L

    The value if it is a Left, otherwise the default value.

    const value = Either.left(42);
    value.leftOr(0); // => 42
    const value = Either.right(42);
    value.leftOr(0); // => 0
  • Returns the value if it is a Left, otherwise returns a default value asynchronously.

    Parameters

    • fallback: L | Promise<L> | AsyncMapper<R, L>

      The default value (as-is or produced from a callback) to return if the value is a Right.

    Returns Promise<L>

    The value if it is a Left, otherwise the default value.

    const value = Either.left(42);
    await value.leftOrAsync(async () => Promise.resolve(0)); // => 42
    const value = Either.right(42);
    await value.leftOrAsync(async () => Promise.resolve(0)); // => 0
  • Maps the value if it is a Left, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: Mapper<L, V>

      The function to apply to the value if it is a Left.

    Returns Either<V, R>

    A new Either with the mapped value if it is a Left, otherwise the value as-is.

    const value = Either.left(1);
    value.mapLeft((value) => value + 1); // => Left(2)
    const value = Either.right(1);
    value.mapLeft((value) => value + 1); // => Right(1)
  • Maps the value if it is a Left asynchronously, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: AsyncMapper<L, V>

      The async function to apply to the value if it is a Left.

    Returns Promise<Either<V, R>>

    A new Either with the mapped value if it is a Left, otherwise the value as-is.

    const value = Either.left(1);
    await value.mapLeftAsync(async (value) => Promise.resolve(value + 1)); // => Left(2)
    const value = Either.right(1);
    await value.mapLeftAsync(async (value) => Promise.resolve(value + 1)); // => Right(1)
  • Maps the value if it is a Right, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: Mapper<R, V>

      The function to apply to the value if it is a Right.

    Returns Either<L, V>

    A new Either with the mapped value if it is a Right, otherwise the value as-is.

    const value = Either.right(1);
    value.mapRight((value) => value + 1); // => Right(2)
    const value = Either.left(1);
    value.mapRight((value) => value + 1); // => Left(1)
  • Maps the value if it is a Right asynchronously, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: AsyncMapper<R, V>

      The async function to apply to the value if it is a Right.

    Returns Promise<Either<L, V>>

    A new Either with the mapped value if it is a Right, otherwise the value as-is.

    const value = Either.right(1);
    await value.mapRightAsync(async (value) => Promise.resolve(value + 1)); // => Right(2)
    const value = Either.left(1);
    await value.mapRightAsync(async (value) => Promise.resolve(value + 1)); // => Left(1)
  • Matches the jonad by calling the appropriate callback based on the value type.

    Type Parameters

    • V

    Parameters

    • onLeft: Mapper<L, V>

      If the value is a Left, this callback is called with the value.

    • onRight: Mapper<R, V>

      If the value is a Right, this callback is called with the value.

    Returns V

    The result of the callback that was called.

    const value = Either.left(42);
    value.match(
    (lvalue) => console.log(`Answer to life: ${lvalue}`),
    (rvalue) => console.log(`Hello, ${rvalue}`)
    ); // => "Answer to life: 42"
    const value = Either.right("world!");
    value.match(
    (value) => console.log(`Answer to life: ${lvalue}`),
    (value) => console.log(`Hello, ${rvalue}`)
    ); // => "Hello, world!"
  • Matches the jonad by calling the appropriate callback based on the value type asynchronously.

    Type Parameters

    • V

    Parameters

    • onLeft: AsyncMapper<L, V>

      If the value is a Left, this callback is called with the value.

    • onRight: AsyncMapper<R, V>

      If the value is a Right, this callback is called with the value.

    Returns Promise<V>

    The result of the callback that was called.

    const value = Either.left(1);
    await value.matchAsync(
    async (lvalue) => `${lvalue} + 2 = ${lvalue + 2}`,
    async (rvalue) => `1 + ${rvalue} = ${1 + rvalue}`
    ); // => "1 + 2 = 3"
    const value = Either.right(2);
    await value.matchAsync(
    async (lvalue) => `${lvalue} + 2 = ${lvalue + 2}`,
    async (rvalue) => `1 + ${rvalue} = ${1 + rvalue}`
    ); // => "1 + 2 = 3"
  • Returns the value if it is a Right, otherwise returns a default value.

    Parameters

    • fallback: R | Mapper<L, R>

      The default value (as-is or produced from a callback) to return if the value is a Left.

    Returns R

    The value if it is a Right, otherwise the default value.

    const value = Either.right(42);
    value.rightOr(0); // => 42
    const value = Either.left(42);
    value.rightOr(0); // => 0
  • Returns the value if it is a Right, otherwise returns a default value asynchronously.

    Parameters

    • fallback: R | Promise<R> | AsyncMapper<L, R>

      The default value (as-is or produced from a callback) to return if the value is a Left.

    Returns Promise<R>

    The value if it is a Right, otherwise the default value.

    const value = Either.right(42);
    await value.rightOrAsync(async () => Promise.resolve(0)); // => 42
    const value = Either.left(42);
    await value.rightOrAsync(async () => Promise.resolve(0)); // => 0
  • Applies a function to the value if it is a Left, returning itself.

    Parameters

    • callback: Consumer<L>

      The function to apply to the value if it is a Left.

    Returns Either<L, R>

    The Either as-is.

    const value = Either.left([1, 2]);
    value.tapLeft((value) => value.push(3)); // => Left([1, 2, 3])
    const value = Either.right([1, 2]);
    value.tapLeft((value) => value.push(3)); // => Right([1, 2])
  • Asynchronously applies a function to the value if it is a Left, returning itself.

    Parameters

    • callback: AsyncConsumer<L>

      The async function to apply to the value if it is a Left.

    Returns Promise<Either<L, R>>

    The Either as-is.

    const value = Either.left([1, 2]);
    await value.tapLeftAsync(async (value) => value.push(3)); // => Left([1, 2, 3])
    const value = Either.right([1, 2]);
    await value.tapLeftAsync(async (value) => value.push(3)); // => Right([1, 2])
  • Applies a function to the value if it is a Right, returning itself.

    Parameters

    • callback: Consumer<R>

      The function to apply to the value if it is a Right.

    Returns Either<L, R>

    The Either as-is.

    const value = Either.right([1, 2]);
    value.tapRight((value) => value.push(3)); // => Right([1, 2, 3])
    const value = Either.left([1, 2]);
    value.tapRight((value) => value.push(3)); // => Left([1, 2])
  • Asynchronously applies a function to the value if it is a Right, returning itself.

    Parameters

    • callback: AsyncConsumer<R>

      The async function to apply to the value if it is a Right.

    Returns Promise<Either<L, R>>

    The Either as-is.

    const value = Either.right([1, 2]);
    await value.tapRightAsync(async (value) => value.push(3)); // => Right([1, 2, 3])
    const value = Either.left([1, 2]);
    await value.tapRightAsync(async (value) => value.push(3)); // => Left([1, 2])