Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Option<T>

Manages access to a value that may or may not be present.

let option: Option<string>;

// A value that is present.
option = some('value');

// A value that is absent.
option = none();

Type parameters

  • T

    The type of the value.

Hierarchy

  • Option

Index

Methods

and

  • Returns the other option if this option is Some.

    let option = some('example');
    let other = some(123);
    let result = option.and(other);
    
    assert(result === other);
    

    If this option is None, then None is returned.

    option = none();
    result = option.and(other);
    
    assert(result.isNone() === true);
    

    Type parameters

    • U

      The type of the value in the other option.

    Parameters

    • other: Option<U>

      The other option.

    Returns Option<U>

    The other option, or None.

andThen

  • Maps and returns this option if it is Some.

    let option = some('example');
    let fn = v => some(v.length);
    let result = option.andThen(fn);
    
    assert(result.unwrap() === 7);
    

    If this option is None, then None is returned.

    option = none();
    result = option.andThen(fn);
    
    assert(result.isNone() === true);
    

    Type parameters

    • U

      The type of the computed value.

    Parameters

    Returns Option<U>

    The mapped option, or None.

expect

  • expect(message: string): T
  • Returns the value in this option if it is Some.

    let error = 'The example error message.';
    let option = some('example');
    let result = option.expect(error);
    
    assert(result === 'example');
    

    If this option is None, then an error is thrown.

    option = none();
    result = option.expect(error);
    
    // error thrown using given message
    

    Parameters

    • message: string

      The error message.

    Returns T

    The value in this option.

filter

  • Returns this option if the predicate returns true.

    let option = some('example');
    let predicate = o => o === 'example';
    let result = option.filter(predicate);
    
    assert(result === option);
    

    If the predicate returns false, then None is returned.

    option = some('other');
    result = option.filter(predicate);
    
    assert(result.isNone() === true);
    

    If the option is None, then None is returned.

    option = none();
    result = option.filter(predicate);
    
    assert(result.isNone() === true);
    

    Parameters

    • predicate: Predicate<T>

      The predicate to apply.

    Returns Option<T>

    This option or None.

getOrInsert

  • getOrInsert(value: T): T
  • Returns the value in this option if Some.

    let option = some('example');
    let other = 'other';
    let result = option.getOrInsert(other);
    
    assert(result === 'example');
    

    If this option is None, this option becomes Some and the given value is set and returned.

    option = none();
    result = option.getOrOinsert(other);
    
    assert(result === other);
    assert(option.isSome() === true);
    assert(option.unwrap() === other);
    
    see

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf For details on performance impact.

    Parameters

    • value: T

      The value to insert.

    Returns T

    The value in this option or the given value.

getOrInsertWith

  • getOrInsertWith(fn: Produce<T>): T
  • Returns the value in this option if Some.

    let option = some('example');
    let other = () => 'other';
    let result = option.getOrInsertWith(other);
    
    assert(result === 'example');
    

    If this option is None, the value is produced and this option becomes Some with the produced value set. The produced value is then returned.

    option = none();
    result = option.getOrOinsertWith(other);
    
    assert(result === 'other');
    assert(option.isSome() === true);
    assert(option.unwrap() === other);
    
    see

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf For details on performance impact.

    Parameters

    • fn: Produce<T>

      The producer for value to insert.

    Returns T

    The value in this option or the given value.

isNone

  • isNone(): boolean
  • Returns true if this option is None.

    let option = none();
    
    assert(option.isNone() === true);
    

    Returns boolean

    If this option is None, then true. Otherwise, false.

isSome

  • isSome(): boolean
  • Returns true if the option is Some.

    let option = some('example');
    
    assert(option.isSome() === true);
    

    Returns boolean

    If this option is Some, then true. Otherwise, false.

map

  • Returns the mapped option if this option is Some.

    let fn = o => o.length;
    let option = some('example');
    let result = option.map(fn);
    
    assert(result.unwrap() === 7);
    

    If this option is None, then None is returned.

    Type parameters

    • U

      The type of the computed value.

    Parameters

    • fn: Compute<T, U>

      The mapping function.

    Returns Option<U>

    The mapped option or None.

mapOr

  • mapOr<U>(def: U, fn: Compute<T, U>): U
  • Returns the mapped value if this option is Some.

    let def = 456;
    let fn = o => o.length;
    let option = some('example');
    let result = option.mapOr(def, fn);
    
    assert(result.unwrap() === 7);
    

    If the option is None, then the default value is returned.

    option = none();
    result = option.mapOr(def, fn);
    
    assert(result.unwrap() === 456);
    

    Type parameters

    • U

      The type of the computed or default value.

    Parameters

    • def: U

      The default value.

    • fn: Compute<T, U>

      The mapping function.

    Returns U

    The mapped or default value.

mapOrElse

  • Returns the mapped value if this option is Some.

    let def = () => 456;
    let fn = o => o.length;
    let option = some('example');
    let result = option.mapOrElse(def, fn);
    
    assert(result.unwrap() === 7);
    

    If this option is None, then the default value is produced and returned.

    option = none();
    result = option.mapOrElse(def, fn);
    
    assert(result.unwrap() === 456);
    

    Type parameters

    • U

      The type of the computed or default value.

    Parameters

    • def: Produce<U>

      The default value producer.

    • fn: Compute<T, U>

      The mapping function.

    Returns U

    The mapped or default value.

okOr

  • okOr<E>(error: E): Result<T, E>
  • Transforms this option into Ok<T, E> if it is `Some.

    let error = 'error';
    let option = some('example');
    let result = option.okOr(error);
    
    assert(result.isOk() === true);
    

    If this option is None, the option is transformed into Err<T, E>.

    option = none();
    result = option.okOr(error);
    
    assert(result.isErr() === true);
    

    Type parameters

    • E

      The type of the error value.

    Parameters

    • error: E

      The possible error.

    Returns Result<T, E>

    The result.

okOrElse

  • Transforms this Option<T> into Ok<T, E> if it is Some.

    let error = () => 'error';
    let option = some('example');
    let result = option.okOrElse(error);
    
    assert(result.isOk() === true);
    

    If this option is None, the Err<T, E> is produced and returned.

    option = none();
    result = option.okOrElse(error);
    
    assert(result.isErr() === true);
    

    Type parameters

    • E

      The type of the error value.

    Parameters

    • error: Produce<E>

      The producer for the possible error.

    Returns Result<T, E>

    The result.

or

  • Returns this option if Some.

    let option = some('example');
    let other = some('other');
    let result = option.or(other);
    
    assert(result === option);
    

    If this option is None, the other option is returned.

    option = none();
    result = option.or(other);
    
    assert(result === other);
    

    Parameters

    • other: Option<T>

      The other option.

    Returns Option<T>

    This or the other option.

orElse

  • Returns this option if Some.

    let option = some('example');
    let fn = () => some('other');
    let result = option.orElse(fn);
    
    assert(result === option);
    

    If this option is None, the other option is produced and returned.

    option = none();
    result = option.orElse(fn);
    
    assert(result.unwrap() === 'other);
    

    Parameters

    Returns Option<T>

    This or the other option.

replace

  • replace(value: T): Option<T>
  • Replaces the value in this option and returns the old one.

    let option = some('example');
    let result = option.replace('other');
    
    assert(result.unwrap() === 'example');
    assert(option.unwrap() === 'other');
    

    If this option is None, it will be converted to Some.

    option = none();
    result = option.replace('other');
    
    assert(result.isNone() === true);
    assert(option.isSome() === true);
    assert(option.unwrap() === 'other');
    
    throws

    {OptionError} If not Some.

    see

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf For details on performance impact.

    Parameters

    • value: T

      The new value.

    Returns Option<T>

    The old value.

unwrap

  • unwrap(): T
  • Returns the value in this option.

    let option = some('example');
    let result = option.unwrap();
    
    assert(result === 'example');
    
    throws

    {OptionError} If no value is present.

    Returns T

    The value.

unwrapOr

  • unwrapOr(def: T): T
  • Returns the value in this option if Some.

    let def = 'default';
    let option = some('example');
    let result = option.unwrapOr(def);
    
    assert(result === 'example');
    

    If this option is None, the default value is returned.

    option = none();
    result = option.unwrapOr(def);
    
    assert(result === def);
    

    Parameters

    • def: T

      The default value.

    Returns T

    The unwrapped or default value.

unwrapOrElse

  • Returns this value in this option if Some.

    let fn = () => 'default';
    let option = some('example');
    let result = option.unwrapOrElse(fn);
    
    assert(result === 'example');
    

    If this option is None, the default value is produced and returned.

    option = none();
    result = option.unwrapOrElse(fn);
    
    assert(result === 'default');
    

    Parameters

    • fn: Produce<T>

      The producer for the default value.

    Returns T

    The unwrapped or default value.

xor

  • Returns this option if Some and the other option is None.

    let option = some('example');
    let other = none();
    let result = option.xor(other);
    
    assert(result === option);
    

    If this option is None and the other option is Some, the other option is returned.

    option = none();
    other = some('other');
    result = option.xor(other);
    
    assert(result === other);
    

    If this option is Some and the other option is Some, then None is returned.

    option = some('example');
    other = some('other');
    result = option.xor(other);
    
    assert(result.isNone() === true);
    

    Parameters

    • other: Option<T>

      The other option.

    Returns Option<T>

    This option, the other option, or None.

zip

  • Returns this option and the other option, if both are Some, into a new Some.

    let option = some('example');
    let other = some('other');
    let result = option.zip(other);
    
    assert(result.isSome() === true);
    
    let values = result.unwrap();
    
    assert(values[0] === 'example');
    assert(values[1] === 'other');
    

    Type parameters

    • U

      The type of the value in the other option.

    Parameters

    • other: Option<U>

      The other option.

    Returns Option<[T, U]>

    The zipped option or None.

Generated using TypeDoc