@bytebury/sauce - v1.13.0
    Preparing search index...

    Interface Option<T>

    Represents a thing that may or may not have a value.

    Very useful when dealing with things that might be null or undefined.

    const option = wrap(mightBeNull);

    const value1 = option.unwrapOr(100);
    const value2 = option.isNoneOr(isWhitespace);
    const value3 = option.isSomeAnd(isEven);
    interface Option<T> {
        expect(message: string): NonNullable<T>;
        filter(fn: (x: T) => boolean): Option<T>;
        inspect(fn: (x: T) => void): Option<T>;
        isNone(): boolean;
        isNoneOr(fn: (x: T) => boolean): boolean;
        isSome(): boolean;
        isSomeAnd(fn: (x: T) => boolean): boolean;
        map<U>(fn: (x: T) => U): Option<U>;
        mapOr<U>(defaultVal: U, fn: (x: T) => U): U;
        null(): null;
        or<U>(other: Option<U>): Option<T | U>;
        undefined(): undefined;
        unwrap(): NonNullable<T>;
        unwrapOr<U>(value: U): T | U;
    }

    Type Parameters

    • T

    Implemented by

    Index

    Methods

    • Unwraps the underlying value and returns it. If the value is None then this function will throw an error with the given message.

      Parameters

      • message: string

      Returns NonNullable<T>

      Usually preferred over unwrap.

      An error when the underlying value is None. This error will contain the provided message.

      Some("hi").expect("should never fail"); // "hi"
      None.expect("missing value"); // throws Error("missing value")
    • Returns None if the Option is None. Otherwise, this will call the predicate. If the predicate returns true then this will return Some(T). If the predicate returns false, then this will return None.

      Parameters

      • fn: (x: T) => boolean

      Returns Option<T>

      Some(10).filter(isEven); // Some(10)
      Some(3).filter(isEven); // None
      None.filter(isEven); // None
    • Reads the current value wrapped in this Option and performs a side-effect. Useful for logging situations.

      Parameters

      • fn: (x: T) => void

      Returns Option<T>

      Some("hello").inspect(console.log); // logs "hello", returns Some("hello")
      None.inspect(console.log); // does nothing, returns None
    • Determines if the underlying value is None.

      Returns boolean

      None.isNone(); // true
      Some("hi").isNone(); // false
    • Determines if the underlying value is None, or if the predicate using that same value returns true.

      Parameters

      • fn: (x: T) => boolean

      Returns boolean

      None.isNoneOr(() => false); // true
      Some(5).isNoneOr(isOdd); // true
      Some(2).isNoneOr(isOdd); // false
    • Determines if the underlying value is something.

      Returns boolean

      Some(5).isSome(); // true
      None.isSome(); // false
    • Determines if the underlying value is something, and the predicate using that same value is also true.

      Parameters

      • fn: (x: T) => boolean

      Returns boolean

      Some(4).isSomeAnd(isEven); // true
      Some(3).isSomeAnd(isEven); // false
      None.isSomeAnd(() => true); // false
    • Maps an Option to Option by applying the function when the contained value is Some. If it is None, then it will return None.

      Type Parameters

      • U

      Parameters

      • fn: (x: T) => U

      Returns Option<U>

      Some(2).map(x => x * 3); // Some(6)
      None.map(x => x * 3); // None
    • Maps an Option to a value by applying the function when the contained value is Some. If it is None, then it will return the defaultVal given.

      Type Parameters

      • U

      Parameters

      • defaultVal: U
      • fn: (x: T) => U

      Returns U

      Some(4).mapOr(0, x => x * 2); // 8
      None.mapOr(0, x => x * 2); // 0
    • Forces an unwrap of a null. This will always return null, even if there is a value. This is only useful if you want to always return null from this option.

      Returns null

      Some(42).null(); // null
      None.null(); // null
    • Returns the option if it contains a value, otherwise will return other, which is an Option. Very similar to unwrap_or, except this will return another Option instead of a value.

      Type Parameters

      • U

      Parameters

      Returns Option<T | U>

      Some(2).or(Some(3)); // Some(2)
      Some(None).or(Some(4)); // Some(4)
    • Forces an unwrap of undefined. This will always return undefined, even if there is a value. This is only useful if you want to always return undefined from this option.

      Returns undefined

      Some(42).undefined(); // undefined
      None.undefined(); // undefined
    • Unwraps the underlying value and returns it. If the value is None then this function will throw an error.

      Returns NonNullable<T>

      An error when the underlying value is None.

      Some(42).unwrap(); // 42
      None.unwrap(); // throws Error
    • Unwraps the underlying value, or returns the given value if the underlying value is None.

      Type Parameters

      • U

      Parameters

      • value: U

      Returns T | U

      Some(10).unwrapOr(0); // 10
      None.unwrapOr(0); // 0