Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Some<T>

Some represents an Option with a value. "static methods" available through OptionStatic

Some and None have the same methods, except that Some has the extra Some.get method that None doesn't have.

Type parameters

  • T

    the item type

Hierarchy

  • Some

Implements

  • any

Index

Methods

__computed

  • __computed(): string
  • Used by the node REPL to display values.

    Returns string

asOption

  • View this Some a as Option. Useful to help typescript type inference sometimes.

    Returns Option<T>

contains

  • Returns true if the option is a Some and contains the value you give, false otherwise.

    Parameters

    Returns boolean

equals

  • Two objects are equal if they represent the same value, regardless of whether they are the same object physically in memory.

    Parameters

    Returns boolean

filter

  • filter<U>(fn: function): Option<U>
  • filter(fn: function): Option<T>
  • If this is None, will return None. If it's a Some, and the contents match your predicate, return the option. If the contents don't match the predicate, return None.

    Type parameters

    • U: T

    Parameters

    • fn: function
        • (v: T): boolean
        • Parameters

          • v: T

          Returns boolean

    Returns Option<U>

  • Parameters

    • fn: function
        • (v: T): boolean
        • Parameters

          • v: T

          Returns boolean

    Returns Option<T>

flatMap

  • flatMap<U>(mapper: function): Option<U>
  • If this is a Some, calls the function you give on the item in the option and return its result. If the option is a None, return none. This is the monadic bind.

    Type parameters

    • U

    Parameters

    • mapper: function

    Returns Option<U>

get

  • get(): T
  • Get the value contained in this option. NOTE: we know it's there, since this method belongs to Some, not Option.

    Returns T

getOrCall

  • getOrCall(fn: function): T
  • Get the value from this option; if it's a None (no value present), then return the value returned by the function that you give.

    Option.of(5).getOrCall(() => 6)
    => 5
    
    Option.none<number>().getOrCall(() => 6)
    => 6
    

    Parameters

    • fn: function
        • (): T
        • Returns T

    Returns T

getOrElse

  • getOrElse(alt: T): T
  • Get the value from this option; if it's a None (no value present), then return the default value that you give.

    Parameters

    • alt: T

    Returns T

getOrNull

  • getOrNull(): T | null
  • Get the value contained in the option if it's a Some, return null if it's a None.

    Option.of(5).getOrNull()
    => 5
    
    Option.none<number>().getOrNull()
    => null
    

    Returns T | null

getOrThrow

  • getOrThrow(errorInfo?: Error | string): T
  • Get the value from this option if it's a Some, otherwise throw an exception. You can optionally pass a message that'll be used as the exception message.

    Parameters

    • Optional errorInfo: Error | string

    Returns T

getOrUndefined

  • getOrUndefined(): T | undefined
  • Get the value contained in the option if it's a Some, return undefined if it's a None.

    Option.of(5).getOrUndefined()
    => 5
    
    Option.none<number>().getOrUndefined()
    => undefined
    

    Returns T | undefined

hasTrueEquality

  • hasTrueEquality<T>(): boolean

hashCode

  • hashCode(): number
  • Get a number for that object. Two different values may get the same number, but one value must always get the same number. The formula can impact performance.

    Returns number

ifNone

  • ifNone(fn: function): Option<T>
  • Execute a side-effecting function if the option is a None; returns the option.

    Parameters

    • fn: function
        • (): void
        • Returns void

    Returns Option<T>

ifSome

  • ifSome(fn: function): Option<T>
  • Execute a side-effecting function if the option is a Some; returns the option.

    Parameters

    • fn: function
        • (v: T): void
        • Parameters

          • v: T

          Returns void

    Returns Option<T>

isNone

  • isNone(): boolean
  • Returns false since this is a Some (contains a value)

    Returns boolean

isSome

  • isSome(): boolean
  • Returns true since this is a Some (contains a value)

    Returns boolean

map

  • map<U>(fn: function): Option<U>
  • Return a new option where the element (if present) was transformed by the mapper function you give. If the option was None it'll stay None.

    Option.of(5).map(x => x*2)
    => Option.of(10)
    
    Option.of(5).map(x => null)
    => Option.of(null)
    

    Also see Some.mapNullable, Some.flatMap

    Type parameters

    • U

    Parameters

    • fn: function
        • (v: T): U
        • Parameters

          • v: T

          Returns U

    Returns Option<U>

mapNullable

  • mapNullable<U>(fn: function): Option<U>
  • Return a new option where the element (if present) was transformed by the mapper function you give. If the mapped value is null or undefined, then a Some will turn into a None. If the option was None it'll stay None.

    Option.of(5).mapNullable(x => x*2)
    => Option.of(10)
    
    Option.of(5).mapNullable(x => null)
    => Option.none()
    

    Also see Some.map, Some.flatMap

    Type parameters

    • U

    Parameters

    • fn: function
        • (v: T): U | null | undefined
        • Parameters

          • v: T

          Returns U | null | undefined

    Returns Option<U>

match

  • match<U>(cases: object): U
  • Handle both branches of the option and return a value (can also be used for side-effects). This is the catamorphism for option.

    Option.of(5).match({
        Some: x  => "got " + x,
        None: () => "got nothing!"
    });
    => "got 5"
    

    Type parameters

    • U

    Parameters

    • cases: object

    Returns U

orCall

  • orCall(_: function): Option<T>
  • If this is a Some, return this object. If this is a None, return the result of the function.

    Parameters

    Returns Option<T>

orElse

  • Combines two options. If this option is a Some, returns it. If it's a None, returns the other one.

    Parameters

    Returns Option<T>

toEither

  • toEither<L>(left: L): Either<L, T>
  • Convert to an either. You must provide a left value in case this is a None.

    Type parameters

    • L

    Parameters

    • left: L

    Returns Either<L, T>

toString

  • toString(): string
  • Get a human-friendly string representation of that value.

    Returns string

toVector

  • Convert to a vector. If it's a None, it's the empty vector, if it's a Some, it's a one-element vector with the contents of the option.

    Returns Vector<T>

transform

  • transform<U>(converter: function): U
  • Transform this value to another value type. Enables fluent-style programming by chaining calls.

    Type parameters

    • U

    Parameters

    • converter: function

    Returns U

Generated using TypeDoc