Function unwrapOr

  • Safely get the value out of a Maybe.

    Returns the content of a Just or defaultValue if Nothing. This is the recommended way to get a value out of a Maybe most of the time.

    import Maybe from 'true-myth/maybe';

    const notAString = Maybe.nothing<string>();
    const isAString = Maybe.just('look ma! some characters!');

    console.log(Maybe.unwrapOr('<empty>', notAString)); // "<empty>"
    console.log(Maybe.unwrapOr('<empty>', isAString)); // "look ma! some characters!"

    Type Parameters

    • T

      The type of the wrapped value.

    • U

    Parameters

    • defaultValue: U

      The value to return if maybe is a Nothing.

    • maybe: Maybe<T>

      The Maybe instance to unwrap if it is a Just.

    Returns T | U

    The content of maybe if it is a Just, otherwise defaultValue.

  • Safely get the value out of a Maybe.

    Returns the content of a Just or defaultValue if Nothing. This is the recommended way to get a value out of a Maybe most of the time.

    import Maybe from 'true-myth/maybe';

    const notAString = Maybe.nothing<string>();
    const isAString = Maybe.just('look ma! some characters!');

    console.log(Maybe.unwrapOr('<empty>', notAString)); // "<empty>"
    console.log(Maybe.unwrapOr('<empty>', isAString)); // "look ma! some characters!"

    Type Parameters

    • T

      The type of the wrapped value.

    • U

    Parameters

    • defaultValue: U

      The value to return if maybe is a Nothing.

    Returns (maybe: Maybe<T>) => T | U

    The content of maybe if it is a Just, otherwise defaultValue.