Options
All
  • Public
  • Public/Protected
  • All
Menu

Result of the Some data constructor, representing non-empty values in the Option disjunction.

Hierarchy

Implements

Index

Constructors

Protected constructor

  • new TNone(ref: never | undefined, isEmpty?: undefined | true | false): TNone

Properties

value

value: undefined

Methods

ap

  • Applicative apply operator.

    Resembles map, but the passed mapping function is lifted in the Either context.

    Type parameters

    • B

    Parameters

    Returns Option<B>

chain

  • chain<B>(f: function): Option<B>

contains

  • contains(elem: never): boolean
  • Returns true if this option is nonempty and the value it holds is equal to the given elem.

    Parameters

    • elem: never

    Returns boolean

equals

  • equals(that: Option<never>): boolean

exists

  • exists(p: function): boolean
  • Returns true if this option is nonempty and the given predicate returns true when applied on this option's value.

    Parameters

    • p: function

      is the predicate function to test

        • (a: never): boolean
        • Parameters

          • a: never

          Returns boolean

    Returns boolean

filter

  • filter<B>(p: function): Option<B>
  • filter(p: function): Option<never>
  • Returns this option if it is nonempty AND applying the predicate p to the underlying value yields true, otherwise return an empty option.

    Type parameters

    • B: never

    Parameters

    • p: function

      is the predicate function that is used to apply filtering on the option's value

        • (a: never): boolean
        • Parameters

          • a: never

          Returns boolean

    Returns Option<B>

    a new option instance containing the value of the source filtered with the given predicate

  • Parameters

    • p: function
        • (a: never): boolean
        • Parameters

          • a: never

          Returns boolean

    Returns Option<never>

flatMap

  • flatMap<B>(f: function): Option<B>
  • Returns the result of applying f to this option's value if the option is nonempty, otherwise returns an empty option.

    NOTE: this is similar with map, except that flatMap the result returned by f is expected to be boxed in an Option already.

    Example:

    const opt = Option.of(10)
    
    opt.flatMap(num => {
      if (num % 2 == 0)
        Some(num + 1)
      else
        None
    })
    

    Type parameters

    • B

    Parameters

    • f: function

      the mapping function that will transform the value of this option if nonempty.

        • Parameters

          • a: never

          Returns Option<B>

    Returns Option<B>

    a new option instance containing the value of the source mapped by the given function

fold

  • fold<B>(fallback: function, f: function): B
  • Returns the result of applying f to this option's value, or in case the option is empty, the return the result of evaluating the fallback function.

    This function is equivalent with:

    opt.map(f).getOrElseL(fallback)
    

    Type parameters

    • B

    Parameters

    • fallback: function

      is the function to be evaluated in case this option is empty

        • (): B
        • Returns B

    • f: function

      is the mapping function for transforming this option's value in case it is nonempty

        • (a: never): B
        • Parameters

          • a: never

          Returns B

    Returns B

forAll

  • forAll(p: function): boolean
  • Returns true if this option is empty or the given predicate returns true when applied on this option's value.

    Parameters

    • p: function

      is the predicate function to test

        • (a: never): boolean
        • Parameters

          • a: never

          Returns boolean

    Returns boolean

forEach

  • forEach(cb: function): void
  • Apply the given procedure cb to the option's value if this option is nonempty, otherwise do nothing.

    Parameters

    • cb: function

      the procedure to apply

        • (a: never): void
        • Parameters

          • a: never

          Returns void

    Returns void

get

  • get(): never
  • Returns the option's value.

    WARNING!

    This function is partial, the option must be non-empty, otherwise a runtime exception will get thrown. Use with care.

    throws

    NoSuchElementError in case the option is empty

    Returns never

getOrElse

  • getOrElse<AA>(fallback: AA): never | AA
  • Returns the option's value if the option is nonempty, otherwise return the given fallback.

    See Option.getOrElseL for a lazy alternative.

    Type parameters

    • AA

    Parameters

    • fallback: AA

    Returns never | AA

getOrElseL

  • getOrElseL<AA>(thunk: function): never | AA
  • Returns the option's value if the option is nonempty, otherwise return the result of evaluating thunk.

    See Option.getOrElse for a strict alternative.

    Type parameters

    • AA

    Parameters

    • thunk: function
        • (): AA
        • Returns AA

    Returns never | AA

hashCode

  • hashCode(): number

isEmpty

  • isEmpty(): boolean

map

  • map<B>(f: function): Option<B>
  • Returns an option containing the result of applying f to this option's value, or an empty option if the source is empty.

    NOTE: this is similar with flatMap, except with map the result of f doesn't need to be wrapped in an Option.

    Type parameters

    • B

    Parameters

    • f: function

      the mapping function that will transform the value of this option if nonempty.

        • (a: never): B
        • Parameters

          • a: never

          Returns B

    Returns Option<B>

    a new option instance containing the value of the source mapped by the given function

nonEmpty

  • nonEmpty(): boolean

orElse

  • Returns this option if it is nonempty, otherwise returns the given fallback.

    Type parameters

    • AA

    Parameters

    Returns Option<never | AA>

orElseL

  • orElseL<AA>(thunk: function): Option<never | AA>
  • Returns this option if it is nonempty, otherwise returns the given result of evaluating the given thunk.

    Type parameters

    • AA

    Parameters

    • thunk: function

      a no-params function that gets evaluated and whose result is returned in case this option is empty

    Returns Option<never | AA>

orNull

  • orNull(): never | null
  • Returns the option's value if the option is nonempty, otherwise return null. `

    Returns never | null

orUndefined

  • orUndefined(): never | undefined

Static empty

  • Returns an empty Option.

    Similar to Option.none, but this one allows specifying a type parameter (in the context of TypeScript or Flow or other type system).

    NOTE: Because Option is immutable, this function returns the same cached reference is on different calls.

    Type parameters

    • A

    Returns Option<A>

Static map2

  • Maps 2 optional values by the mapping function, returning a new optional reference that is Some only if both option values are Some, otherwise it returns a None.

    // Yields Some(3)
    Option.map2(Some(1), Some(2),
      (a, b) => a + b
    )
    
    // Yields None, because the second arg is None
    Option.map2(Some(1), None,
      (a, b) => a + b
    )
    

    This operation is the Applicative.map2.

    Type parameters

    • A1

    • A2

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • f: function
        • (a1: A1, a2: A2): R
        • Parameters

          • a1: A1
          • a2: A2

          Returns R

    Returns Option<R>

Static map3

  • Maps 3 optional values by the mapping function, returning a new optional reference that is Some only if all 3 option values are Some, otherwise it returns a None.

    // Yields Some(6)
    Option.map3(Some(1), Some(2), Some(3),
      (a, b, c) => a + b + c
    )
    
    // Yields None, because the second arg is None
    Option.map3(Some(1), None, Some(3),
      (a, b, c) => a + b + c
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • f: function
        • (a1: A1, a2: A2, a3: A3): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3

          Returns R

    Returns Option<R>

Static map4

  • Maps 4 optional values by the mapping function, returning a new optional reference that is Some only if all 4 option values are Some, otherwise it returns a None.

    // Yields Some(10)
    Option.map4(Some(1), Some(2), Some(3), Some(4),
      (a, b, c, d) => a + b + c + d
    )
    
    // Yields None, because the second arg is None
    Option.map4(Some(1), None, Some(3), Some(4),
      (a, b, c, d) => a + b + c + d
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • A4

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • fa4: Option<A4>
    • f: function
        • (a1: A1, a2: A2, a3: A3, a4: A4): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3
          • a4: A4

          Returns R

    Returns Option<R>

Static map5

  • Maps 5 optional values by the mapping function, returning a new optional reference that is Some only if all 5 option values are Some, otherwise it returns a None.

    // Yields Some(15)
    Option.map5(Some(1), Some(2), Some(3), Some(4), Some(5),
      (a, b, c, d, e) => a + b + c + d + e
    )
    
    // Yields None, because the second arg is None
    Option.map5(Some(1), None, Some(3), Some(4), Some(5),
      (a, b, c, d, e) => a + b + c + d + e
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • A4

    • A5

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • fa4: Option<A4>
    • fa5: Option<A5>
    • f: function
        • (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3
          • a4: A4
          • a5: A5

          Returns R

    Returns Option<R>

Static map6

  • Maps 6 optional values by the mapping function, returning a new optional reference that is Some only if all 6 option values are Some, otherwise it returns a None.

    // Yields Some(21)
    Option.map6(Some(1), Some(2), Some(3), Some(4), Some(5), Some(6),
      (a, b, c, d, e, f) => a + b + c + d + e + f
    )
    
    // Yields None, because the second arg is None
    Option.map6(Some(1), None, Some(3), Some(4), Some(5), Some(6),
      (a, b, c, d, e, f) => a + b + c + d + e + f
    )
    

    Type parameters

    • A1

    • A2

    • A3

    • A4

    • A5

    • A6

    • R

    Parameters

    • fa1: Option<A1>
    • fa2: Option<A2>
    • fa3: Option<A3>
    • fa4: Option<A4>
    • fa5: Option<A5>
    • fa6: Option<A6>
    • f: function
        • (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6): R
        • Parameters

          • a1: A1
          • a2: A2
          • a3: A3
          • a4: A4
          • a5: A5
          • a6: A6

          Returns R

    Returns Option<R>

Static none

  • Returns an empty Option.

    NOTE: Because Option is immutable, this function returns the same cached reference is on different calls.

    Type parameters

    • A

    Returns Option<A>

Static of

  • of<A>(value: A | null | undefined): Option<A>
  • Builds an Option reference that contains the given value.

    If the given value is null or undefined then the returned option will be empty.

    Type parameters

    • A

    Parameters

    • value: A | null | undefined

    Returns Option<A>

Static pure

  • pure<A>(value: A): Option<A>

Static some

  • some<A>(value: A): Option<A>
  • Builds an Option reference that contains the given reference.

    Note that value is allowed to be null or undefined, the returned option will still be non-empty. Use Option.of if you want to avoid this problem. This means:

    const opt = Some<number | null>(null)
    
    opt.isEmpty()
    //=> false
    
    opt.get()
    //=> null
    

    Type parameters

    • A

    Parameters

    • value: A

    Returns Option<A>

Static tailRecM

  • tailRecM<A, B>(a: A, f: function): Option<B>

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc