Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Optional<T>

An optional is just like Stream but contains no more than one item. All stream properties (laziness, statelessness) fully apply to optionals.

Like stream, optional has intermediate and terminal operations. When a terminal operation is executed, the result value is computed — this process is described as “an optional resolves to a value” if there is an item, or “an optional resolves to empty” if there's no one.

Type parameters

  • T

Hierarchy

  • Iterable<T>
    • Optional

Index

Methods

[Symbol.iterator]

  • [Symbol.iterator](): Iterator<T>
  • Creates an iterator which yields an item if this optional has an item; otherwise iterator yields no items.

    Returns Iterator<T>

filter

  • filter(predicate: (item: T) => boolean): Optional<T>
  • Creates an optional resolving to original item if this optional has an item and predicate evaluates to true for it, or resolving to empty otherwise.

    Parameters

    • predicate: (item: T) => boolean

      A predicate to evaluate an item

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns Optional<T>

flatMap

  • flatMap<U>(mapper: (item: T) => Iterable<U>): Optional<U>
  • Creates an optional with the following behavior:

    • If this optional contains an item, applies mapper to it, and tries to retrieve the first item from the returned iterable. If there's an item, resolves to it, otherwise resolves to empty.
    • It this optional is empty resolves to empty.

    Type parameters

    • U

    Parameters

    • mapper: (item: T) => Iterable<U>

      Function which takes an item and returns an iterable to create an optional from.

        • (item: T): Iterable<U>
        • Parameters

          • item: T

          Returns Iterable<U>

    Returns Optional<U>

flatMapToStream

  • flatMapToStream<U>(mapper: (item: T) => Iterable<U>): Stream<U>
  • Creates a stream with the following behavior:

    • If this optional contains an item, applies mapper to it, and uses a returned iterable as an input of the created stream.
    • If this optional is empty, the created stream is empty.

    Type parameters

    • U

    Parameters

    • mapper: (item: T) => Iterable<U>

      Function which takes an item and returns an iterable to create new stream from.

        • (item: T): Iterable<U>
        • Parameters

          • item: T

          Returns Iterable<U>

    Returns Stream<U>

get

  • get(): T
  • If this optional has an item, returns that item, otherwise throws an error.

    Returns T

has

  • has(predicate: (item: T) => boolean): boolean
  • Returns true if this optional has an item and predicate evaluates to true for it; false otherwise.

    Parameters

    • predicate: (item: T) => boolean

      The predicate to test an item

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

hasNot

  • hasNot(predicate: (item: T) => boolean): boolean
  • Returns true if this optional has an item and predicate evaluates to false for it, or if this optional is empty. False otherwise.

    Parameters

    • predicate: (item: T) => boolean

      The predicate to test an item

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

is

  • is(item: T): boolean
  • Returns true if this optional has an item and it strict-equals (===) the passed item. False otherwise.

    Parameters

    • item: T

      An item to test an item

    Returns boolean

isPresent

  • isPresent(): boolean
  • Returns true if this optional has an item, false otherwise

    Returns boolean

map

  • map<U>(mapper: (item: T) => U): Optional<U>
  • Returns an optional with the following behavior:

    • If this optional has an item, invokes mapper passing this item as an argument, and resolves to the value returned by mapper
    • If this optional is empty, resolves to empty

    Type parameters

    • U

    Parameters

    • mapper: (item: T) => U

      The function to transform an item with

        • (item: T): U
        • Parameters

          • item: T

          Returns U

    Returns Optional<U>

mapNullable

  • mapNullable<U>(mapper: (item: T) => U | null | undefined): Optional<U>
  • Returns an optional with the following behavior:

    • If this optional has an item, invokes mapper passing this item as an argument; if mapper returns null or undefined resolves to empty; otherwise resolves to the value returned by mapper
    • If this optional is empty, resolves to empty

    Type parameters

    • U

    Parameters

    • mapper: (item: T) => U | null | undefined

      The function to transform an item with

        • (item: T): U | null | undefined
        • Parameters

          • item: T

          Returns U | null | undefined

    Returns Optional<U>

orElse

  • orElse<U>(other: U): T | U
  • If this optional has an item returns this item; otherwise returns other

    Type parameters

    • U

    Parameters

    • other: U

      Value to return if this optional is empty

    Returns T | U

orElseGet

  • orElseGet<U>(get: () => U): T | U
  • If this optional has an item returns this item; otherwise returns value returned by get.

    Type parameters

    • U

    Parameters

    • get: () => U

      Function to take result from if this optional is empty

        • (): U
        • Returns U

    Returns T | U

orElseNull

  • orElseNull(): T | null
  • If this optional has an item returns this item; otherwise returns null

    Returns T | null

orElseThrow

  • orElseThrow(createError: () => Error): T
  • If this optional has an item returns this item; otherwise throws an error created by createError

    Parameters

    • createError: () => Error

      Function to create error if this optional is empty

        • (): Error
        • Returns Error

    Returns T

orElseUndefined

  • orElseUndefined(): T | undefined
  • If this optional has an item returns this item; otherwise returns undefined

    Returns T | undefined

resolve

  • resolve(): { has: true; val: T } | { has: false }
  • Returns {has: true, val: item} if this optional contains an item, {has: false} otherwise

    Returns { has: true; val: T } | { has: false }

toArray

  • toArray(): T[]
  • If this optional has an item returns an array containing that item as the only element, otherwise returns an empty array

    Returns T[]

toStream

  • Creates a stream with an item provided by this optional if it has an item; otherwise the new stream is empty.

    Returns Stream<T>

Generated using TypeDoc