Function mapOrElse

  • Map over a Result instance as in map and get out the value if result is Ok, or apply a function (orElseFn) to the value wrapped in the Err to get a default value.

    Like mapOr but using a function to transform the error into a usable value instead of simply using a default value.

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

    const summarize = (s: string) => `The response was: '${s}'`;
    const getReason = (err: { code: number, reason: string }) => err.reason;

    const okResponse = ok("Things are grand here.");
    const mappedOkAndUnwrapped = mapOrElse(getReason, summarize, okResponse);
    console.log(mappedOkAndUnwrapped); // The response was: 'Things are grand here.'

    const errResponse = err({ code: 500, reason: 'Nothing at this endpoint!' });
    const mappedErrAndUnwrapped = mapOrElse(getReason, summarize, errResponse);
    console.log(mappedErrAndUnwrapped); // Nothing at this endpoint!

    Type Parameters

    • T

      The type of the wrapped Ok value.

    • U

      The type of the resulting value from applying mapFn to the Ok value or orElseFn to the Err value.

    • E

      The type of the wrapped Err value.

    Parameters

    • orElseFn: (err: E) => U

      The function to apply to the wrapped Err value to get a usable value if result is an Err.

    • mapFn: (t: T) => U

      The function to apply to the wrapped Ok value 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 Ok, or apply a function (orElseFn) to the value wrapped in the Err to get a default value.

    Like mapOr but using a function to transform the error into a usable value instead of simply using a default value.

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

    const summarize = (s: string) => `The response was: '${s}'`;
    const getReason = (err: { code: number, reason: string }) => err.reason;

    const okResponse = ok("Things are grand here.");
    const mappedOkAndUnwrapped = mapOrElse(getReason, summarize, okResponse);
    console.log(mappedOkAndUnwrapped); // The response was: 'Things are grand here.'

    const errResponse = err({ code: 500, reason: 'Nothing at this endpoint!' });
    const mappedErrAndUnwrapped = mapOrElse(getReason, summarize, errResponse);
    console.log(mappedErrAndUnwrapped); // Nothing at this endpoint!

    Type Parameters

    • T

      The type of the wrapped Ok value.

    • U

      The type of the resulting value from applying mapFn to the Ok value or orElseFn to the Err value.

    • E

      The type of the wrapped Err value.

    Parameters

    • orElseFn: (err: E) => U

      The function to apply to the wrapped Err value to get a usable value if result is an Err.

    • mapFn: (t: T) => U

      The function to apply to the wrapped Ok value 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 Ok, or apply a function (orElseFn) to the value wrapped in the Err to get a default value.

    Like mapOr but using a function to transform the error into a usable value instead of simply using a default value.

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

    const summarize = (s: string) => `The response was: '${s}'`;
    const getReason = (err: { code: number, reason: string }) => err.reason;

    const okResponse = ok("Things are grand here.");
    const mappedOkAndUnwrapped = mapOrElse(getReason, summarize, okResponse);
    console.log(mappedOkAndUnwrapped); // The response was: 'Things are grand here.'

    const errResponse = err({ code: 500, reason: 'Nothing at this endpoint!' });
    const mappedErrAndUnwrapped = mapOrElse(getReason, summarize, errResponse);
    console.log(mappedErrAndUnwrapped); // Nothing at this endpoint!

    Type Parameters

    • T

      The type of the wrapped Ok value.

    • U

      The type of the resulting value from applying mapFn to the Ok value or orElseFn to the Err value.

    • E

      The type of the wrapped Err value.

    Parameters

    • orElseFn: (err: E) => U

      The function to apply to the wrapped Err value to get a usable value if result is an Err.

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