Interface Option<T>

An Option jonad.

Represents a value that may or may not be present. The left-side value is Some, while the right-side value is None (represented by the value null).

interface Option<T> {
    andThen<U>(mapper: Mapper<T, Option<U>>): Option<U>;
    andThenAsync<U>(mapper: AsyncMapper<T, Option<U>>): Promise<Option<U>>;
    getLeftOrThrow(): T;
    getRightOrThrow(): null;
    isLeft(): boolean;
    isNone(): boolean;
    isRight(): boolean;
    isSome(): boolean;
    leftOr(fallback: T | Mapper<null, T>): T;
    leftOrAsync(fallback: T | Promise<T> | AsyncMapper<null, T>): Promise<T>;
    map<U>(mapper: Mapper<T, U>): Option<U>;
    mapAsync<U>(mapper: AsyncMapper<T, U>): Promise<Option<U>>;
    mapLeft<V>(mapper: Mapper<T, V>): Either<V, null>;
    mapLeftAsync<V>(mapper: AsyncMapper<T, V>): Promise<Either<V, null>>;
    mapRight<V>(mapper: Mapper<null, V>): Either<T, V>;
    mapRightAsync<V>(mapper: AsyncMapper<null, V>): Promise<Either<T, V>>;
    match<V>(onLeft: Mapper<T, V>, onRight: Mapper<null, V>): V;
    matchAsync<V>(onLeft: AsyncMapper<T, V>, onRight: AsyncMapper<null, V>): Promise<V>;
    okOr<E>(error: E | Producer<E>): Result<T, E>;
    okOrAsync<E>(error: E | Promise<E> | AsyncProducer<E>): Promise<Result<T, E>>;
    okOrError(message: string | Producer<string>): Result<T, Error>;
    okOrErrorAsync(message: string | Promise<string> | AsyncProducer<string>): Promise<Result<T, Error>>;
    rightOr(fallback: Mapper<T, null>): null;
    rightOrAsync(fallback: Promise<null> | AsyncMapper<T, null>): Promise<null>;
    tapLeft(callback: Consumer<T>): Either<T, null>;
    tapLeftAsync(callback: AsyncConsumer<T>): Promise<Either<T, null>>;
    tapRight(callback: Consumer<null>): Either<T, null>;
    tapRightAsync(callback: AsyncConsumer<null>): Promise<Either<T, null>>;
    valueOr(fallback: T | Producer<T>): T;
    valueOrAsync(fallback: T | Promise<T> | AsyncProducer<T>): Promise<T>;
}

Type Parameters

  • T

Hierarchy (view full)

Methods

  • Chains a new Option to the current one if it is Some.

    Type Parameters

    • U

    Parameters

    • mapper: Mapper<T, Option<U>>

      The mapper function to apply to the value if it is Some.

    Returns Option<U>

    A new Option with the mapped value if it is Some, otherwise the value as-is.

    const object = Option.from({name: "Alice"});
    const name = object.andThen(o => Option.from(o.name)); // Some("Alice")
    const age = object.andThen(o => Option.from(o.age)); // None
  • Chains a new Option to the current one if it is Some, but asynchronously.

    Type Parameters

    • U

    Parameters

    • mapper: AsyncMapper<T, Option<U>>

      The async mapper function to apply to the value if it is Some.

    Returns Promise<Option<U>>

    A new Option with the mapped value if it is Some, otherwise the value as-is.

    // const userId: Option<number>;
    const url = userId.map(n => `https://api.example.com/users/${n}`);
    const name = await url.andThenAsync(async url => {
    const res = await fetch(url));
    const userObj = await res.json();
    return Option.from(userObj.name);
    }); // Some("Alice")
  • 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 T

    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 null

    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 None.

    Returns boolean

    true if the value is None, false otherwise.

    const something = Option.from(123);
    something.isNone(); // false

    const nothing = Option.from(null);
    nothing.isNone(); // 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
  • Checks if the value is Some.

    Returns boolean

    true if the value is Some, false otherwise.

    const something = Option.from(123);
    something.isSome(); // true

    const nothing = Option.from(null);
    nothing.isSome();
  • Returns the value if it is a Left, otherwise returns a default value.

    Parameters

    • fallback: T | Mapper<null, T>

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

    Returns T

    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: T | Promise<T> | AsyncMapper<null, T>

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

    Returns Promise<T>

    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 Some.

    Type Parameters

    • U

    Parameters

    • mapper: Mapper<T, U>

      The mapper function to apply to the value if it is Some.

    Returns Option<U>

    A new Option with the mapped value if it is Some, otherwise the Option as-is.

    const something = Option.from(123);
    something.map(n => n.toString()); // Some("123")
  • Maps the value if it is Some, but asynchronously.

    Type Parameters

    • U

    Parameters

    • mapper: AsyncMapper<T, U>

      The async mapper function to apply to the value if it is Some.

    Returns Promise<Option<U>>

    A new Option with the mapped value if it is Some, otherwise the Option as-is.

    const something = Option.from("https://example.com");
    something.mapAsync(async url => await fetch(url)); // Promise(Some(Response))
  • Maps the value if it is a Left, otherwise returns the value as-is.

    Type Parameters

    • V

    Parameters

    • mapper: Mapper<T, V>

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

    Returns Either<V, null>

    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<T, V>

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

    Returns Promise<Either<V, null>>

    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<null, V>

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

    Returns Either<T, 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<null, V>

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

    Returns Promise<Either<T, 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<T, V>

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

    • onRight: Mapper<null, 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<T, V>

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

    • onRight: AsyncMapper<null, 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"
  • Maps the Option to a Result.

    If the Option is Some, the value will be wrapped in an Ok. If the Option is None, the provided error will be returned.

    Type Parameters

    • E extends Error

    Parameters

    • error: E | Producer<E>

      The error to return if the Option is None.

    Returns Result<T, E>

    A Result of the Option.

    const something = Option.from(123);
    something.okOr(new Error("Value is None")); // Ok(123)

    const nothing = Option.none();
    nothing.okOr(new Error("Value is None")); // Err(Error("Value is None"))
  • Maps the Option to a promised Result.

    If the Option is Some, the value will be wrapped in an Ok. If the Option is None, the provided error will be returned.

    Type Parameters

    • E extends Error

    Parameters

    • error: E | Promise<E> | AsyncProducer<E>

      The error to return if the Option is None.

    Returns Promise<Result<T, E>>

    A Result of the Option.

    const something = Option.from(123);
    something.okOrAsync(async () => {
    await error_reporter.send("error", "Value is None");
    return new Error("Value is None");
    });
  • Maps the Option to a Result, creating a new error from the message if the Option is None.

    Parameters

    • message: string | Producer<string>

      The message to create the error from if the Option is None.

    Returns Result<T, Error>

    A Result of the Option.

    const something = Option.from(123);
    something.okOrError("Value is None"); // Ok(123)

    const nothing = Option.none();
    nothing.okOrError("Value is None"); // Err(Error("Value is None"))
  • Maps the Option to a promised Result, creating a new error from the message if the Option is None.

    Parameters

    • message: string | Promise<string> | AsyncProducer<string>

      The message to create the error from if the Option is None.

    Returns Promise<Result<T, Error>>

    A Result of the Option.

    const something = Option.from(123);
    something.okOrErrorAsync(async () => {
    await error_reporter.send("error", "Value is None");
    return "Value is None";
    }); // Promise(Ok(123))
  • Returns the value if it is a Right, otherwise returns a default value.

    Parameters

    • fallback: Mapper<T, null>

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

    Returns null

    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: Promise<null> | AsyncMapper<T, null>

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

    Returns Promise<null>

    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<T>

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

    Returns Either<T, null>

    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<T>

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

    Returns Promise<Either<T, null>>

    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<null>

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

    Returns Either<T, null>

    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<null>

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

    Returns Promise<Either<T, null>>

    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])
  • Returns the value if it is Some, otherwise returns a default value.

    Parameters

    • fallback: T | Producer<T>

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

    Returns T

    The value if it is Some, otherwise the default value.

    const something = Option.from(123);
    something.valueOr(0); // 123

    const nothing = Option.none();
    nothing.valueOr(0); // 0
  • Returns the value if it is Some, otherwise returns a default value asynchronously.

    Parameters

    • fallback: T | Promise<T> | AsyncProducer<T>

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

    Returns Promise<T>

    The value if it is an Ok, otherwise the default value.

    const something = Option.from(123);
    something.valueOrAsync(async () => 0); // 123

    const nothing = Option.none();
    nothing.valueOrAsync(async () => 0); // 0