Class Optional<Embedded>

An Optional is a container that may or may not contain a value.

Example

// Returns 6
const value = Optional.of(5);
const result = value.map(value => value + 1).getOrElse(() => 5);

// Returns 5
const empty = Optional.empty();
const emptyResult = empty.map(value => value + 1).getOrElse(() => 5);

Type Parameters

  • Embedded

Constructors

Properties

value?: Embedded

Methods

  • Filter the Optional. If the Optional is empty or the predicate returns false, this will return an empty Optional.

    Parameters

    • predicate: ((embedded) => boolean)

      A function that takes the Optional's value and returns a boolean.

        • (embedded): boolean
        • Parameters

          Returns boolean

    Returns Optional<Embedded>

    The Optional if the predicate returns true, an empty Optional otherwise.

  • Get the value of the Optional, or throw an error if the Optional is empty.

    Parameters

    • failureMapper: ((error) => Error) = ...

      A function that takes an error and returns an error.

        • (error): Error
        • Parameters

          • error: Error

          Returns Error

    Returns Embedded

    The value of the Optional.

  • Execute the consumer with the value of the Optional if the Optional is present. If the Optional is empty, this will do nothing.

    Parameters

    • consumer: ((embedded) => void)

      A function that takes the Optional's value.

        • (embedded): void
        • Parameters

          Returns void

    Returns Optional<Embedded>

    The Optional.

  • Check if the Optional does not contain a value. If the value supplied to the optional is null or undefined, this will return true.

    Returns boolean

    true if the Optional does not contain a value, false otherwise.

  • Check if the Optional contains a value. If the value supplied to the optional is null or undefined, this will return false.

    Returns boolean

    true if the Optional contains a value, false otherwise.

Generated using TypeDoc