Options
All
  • Public
  • Public/Protected
  • All
Menu

A Some is an Option that contains a value.

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • new Some<T>(value: T): Some<T>

Methods

and

  • Returns None if the option is a None, otherwise returns optB.

    Examples

    const x = new Some(2);
    const y = new None();
    expect(x.and(y)).toEqual(new None());

    const x = new None();
    const y = new Some("foo");
    expect(x.and(y)).toEqual(new None());

    const x = new Some(2);
    const y = new Some("foo");
    expect(x.and(y)).toEqual(new Some("foo"));

    const x = new None();
    const y = new None();
    expect(x.and(y)).toEqual(new None());

    Type parameters

    • U

    Parameters

    Returns Option<U>

andThen

  • Returns None if the option is a None, otherwise calls fn with the wrapped value and returns the result.

    Some languages call this operation flatMap.

    Examples

    const sq = (x: number) => new Some(x * x);
    const nope = (x: number) => new None();

    expect(new Some(2).andThen(sq).andThen(sq)).toEqual(new Some(16));
    expect(new Some(2).andThen(sq).andThen(nope)).toEqual(new None());
    expect(new Some(2).andThen(nope).andThen(sq)).toEqual(new None());
    expect(new None().andThen(sq).andThen(sq)).toEqual(new None());

    Type parameters

    • U

    Parameters

    Returns Option<U>

expect

  • expect(message: string): T
  • Returns the contained Some value.

    Throws

    Throws if the value is a None with a custome error message provided by the errorMessage parameter.

    Examples

    const x = new Some("value");
    expect(x.expect("fruits are healthy")).toBe("value");

    const x = new None();
    x.expect("fruits are healthy"); // throws with "fruits are healthy"

    Parameters

    • message: string

    Returns T

filter

  • filter(predicate: (value: T) => boolean): Option<T>
  • Returns None if the option is a None, otherwise calls predicate with the wrapped value and returns:

    • Some(t) if predicate returns true (where t is the wrapped value), and
    • None if predicate returns false.

    Examples

    const isEven = (x: number) => x % 2 === 0;

    expect(new None().filter(isEven)).toEqual(new None());
    expect(new Some(3).filter(isEven)).toEqual(new None());
    expect(new Some(4).filter(isEven)).toEqual(new Some(4));

    Parameters

    • predicate: (value: T) => boolean
        • (value: T): boolean
        • Parameters

          • value: T

          Returns boolean

    Returns Option<T>

isNone

  • isNone(): this is None

isSome

  • isSome(): this is Some<T>

map

  • map<U>(f: (value: T) => U): Option<U>
  • Maps an Option<T> to an Option<U> by applying a function to a contained value.

    Examples

    Converts an Option<string> into an Option`:

    const maybeSomeString = new Some ("Hello, World!");
    const maybeSomeLength = maybeSomeString.map(s => s.length);

    expect (maybeSomeLength).toEqual(new Some(13));

    Type parameters

    • U

    Parameters

    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns Option<U>

mapOr

  • mapOr<U>(defaultValue: U, f: (value: T) => U): U
  • Returns the provided default result (if none), or applies a function to the contained value (if any).

    Arguments passed to mapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use mapOrElse, which is lazily evaluated.

    Examples

    const x = new Some("foo");
    expect(x.mapOr(42, s => s.length)).toBe(3);

    const x = new None();
    expect(x.mapOr(42, s => s.length)).toBe(42);

    Type parameters

    • U

    Parameters

    • defaultValue: U
    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns U

mapOrElse

  • mapOrElse<U>(defaultFunc: () => U, f: (value: T) => U): U
  • Computes a default function result (if none), or applies a different function to the contained value (if any).

    Examples

    const x = new Some("foo");
    expect(x.mapOrElse(() => 2 * 21, s => s.length)).toBe(3);

    const x = new None();
    expect(x.mapOrElse(() => 2 * 21, s => s.length)).toBe(42);

    Type parameters

    • U

    Parameters

    • defaultFunc: () => U
        • (): U
        • Returns U

    • f: (value: T) => U
        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns U

or

  • Returns the option if it contains a value, otherwise returns optB.

    Arguments passed to or are eagerly evaluated; if you are passing the result of a function call, it is recommended to use orElse, which is lazily evaluated.

    Examples

    const x = new Some(2);
    const y = new None();
    expect(x.or(y)).toEqual(new Some(2));

    const x = new None();
    const y = new Some(100);
    expect(x.or(y)).toEqual(new Some(100));

    const x = new Some(2);
    const y = new Some(100);
    expect(x.or(y)).toEqual(new Some(2));

    const x = new None();
    const y = new None();
    expect(x.or(y)).toEqual(new None());

    Parameters

    Returns Option<T>

orElse

  • Returns the option if it contains a value, otherwise calls fn and returns the result.

    Examples

    const nobody = () => new None();
    const vikings = () => new Some("vikings");

    expect(new Some("barbarians").orElse(vikings)).toEqual(new Some("barbarians"));
    expect(new None().orElse(vikings)).toEqual(new Some("vikings"));
    expect(new None().orElse(nobody)).toEqual(new None());

    Parameters

    Returns Option<T>

unwrap

  • unwrap(): T
  • Returns the contained Some value.

    Because this function may throw, its use is generally discouraged. Instead, handle the None case explicitly, or call unwrapOr or unwrapOrElse.

    Throws

    Throws if the value is a None.

    Examples

    const x = new Some("air");
    expect(x.unwrap()).toBe("air");

    const x = new None();
    x.unwrap(); // throws an Error

    Returns T

unwrapOr

  • unwrapOr(defaultValue: T): T
  • Returns the contained Some value or a provided default.

    Arguments passed to unwrapOr are eagerly evaluated; if you are passing the result of a function call, it is recommended to use unwrapOrElse, which is lazily evaluated.

    Examples

    expect(new Some("car").unwrapOr("bike")).toBe("car");
    expect(new None().unwrapOr("bike")).toBe("bike");

    Parameters

    • defaultValue: T

    Returns T

unwrapOrElse

  • unwrapOrElse(fn: () => T): T
  • Returns the contained Some value or computes it from a provided function.

    Examples

    expect(new Some(4).unwrapOrElse(() => 2 * 10)).toBe(4);
    expect(new None().unwrapOrElse(() => 2 * 10)).toBe(20);

    Parameters

    • fn: () => T
        • (): T
        • Returns T

    Returns T

xor

  • Returns Some if exactly one of this, optB is Some, otherwise returns None.

    Examples

    const x = new Some(2);
    const y = new None();
    expect(x.xor(y)).toEqual(new Some(2));

    const x = new None();
    const y = new Some(2);
    expect(x.xor(y)).toEqual(new Some(2));

    const x = new Some(2);
    const y = new Some(2);
    expect(x.xor(y)).toEqual(new None());

    const x = new None();
    const y = new None();
    expect(x.xor(y)).toEqual(new None());

    Parameters

    Returns Option<T>

zip

  • Zips this with another Option.

    If this is Some(s) and other is Some(o), this method returns Some([s, o]). Otherwise, it returns None.

    Examples

    const x = new Some(1);
    const y = new Some("hi");
    const z = new None();

    expect(x.zip(y)).toEqual(new Some([1, "hi"]));
    expect(x.zip(z)).toEqual(new None());

    Type parameters

    • U

    Parameters

    Returns Option<[T, U]>

zipWith

  • zipWith<U, R>(other: Option<U>, fn: (a: T, b: U) => R): Option<R>
  • Zips this and another Option with function fn.

    If this is Some(s) and other is Some(o), this method returns Some(fn([s, o])). Otherwise, it returns None.

    Examples

    const x = new Some(17.5);
    const y = new Some(42.7);
    const point = (x: number, y: number) => ({ x, y });

    expect(x.zipWith(y, point)).toEqual(new Some({ x: 17.5, y: 42.7 }));
    expect(x.zipWith(new None(), point)).toEqual(new None());

    Type parameters

    • U

    • R

    Parameters

    • other: Option<U>
    • fn: (a: T, b: U) => R
        • (a: T, b: U): R
        • Parameters

          • a: T
          • b: U

          Returns R

    Returns Option<R>

Generated using TypeDoc