Function mapOr

  • Map over a Result instance as in map and get out the value if result is an Ok, or return a default value if result is an Err.

    import { ok, err, mapOr } from 'true-myth/result';

    const length = (s: string) => s.length;

    const anOkString = ok('a string');
    const theStringLength = mapOr(0, anOkString);
    console.log(theStringLength); // 8

    const anErr = err('uh oh');
    const anErrMapped = mapOr(0, anErr);
    console.log(anErrMapped); // 0

    Type Parameters

    • T
    • U
    • E

    Parameters

    • orU: U

      The default value to use if result is an Err.

    • mapFn: (t: T) => U

      The function to apply the value to if result is an Ok.

    • result: Result<T, E>

      The Result instance to map over.

    Returns U

  • Map over a Result instance as in map and get out the value if result is an Ok, or return a default value if result is an Err.

    import { ok, err, mapOr } from 'true-myth/result';

    const length = (s: string) => s.length;

    const anOkString = ok('a string');
    const theStringLength = mapOr(0, anOkString);
    console.log(theStringLength); // 8

    const anErr = err('uh oh');
    const anErrMapped = mapOr(0, anErr);
    console.log(anErrMapped); // 0

    Type Parameters

    • T
    • U
    • E

    Parameters

    • orU: U

      The default value to use if result is an Err.

    • mapFn: (t: T) => U

      The function to apply the value to if result is an Ok.

    Returns (result: Result<T, E>) => U

  • Map over a Result instance as in map and get out the value if result is an Ok, or return a default value if result is an Err.

    import { ok, err, mapOr } from 'true-myth/result';

    const length = (s: string) => s.length;

    const anOkString = ok('a string');
    const theStringLength = mapOr(0, anOkString);
    console.log(theStringLength); // 8

    const anErr = err('uh oh');
    const anErrMapped = mapOr(0, anErr);
    console.log(anErrMapped); // 0

    Type Parameters

    • T
    • U
    • E

    Parameters

    • orU: U

      The default value to use if result is an Err.

    Returns (mapFn: (t: T) => U) => (result: Result<T, E>) => U