Function safe

  • Wraps a transform function to passes nullish values (null and undefined) through unchanged instead of applying the transform.

    Remarks

    This is intended to solve the same problem as the Maybe monad in other functional languages and libraries. It allows you to use nullish for error conditions, and to pass potentially nullish values safely through a pipeline even when the individual functions do not accept them as input.

    Example

    import { safe } from "flurp";
    import * as N from "flurp/number";

    const safeDouble = safe(N.multiply(2));
    safeDouble(5); // 10
    safeDouble(undefined); // undefined
    safeDouble(null); // null

    Type Parameters

    • T

    • U

    Parameters

    • transform: ((x: T) => U)
        • (x: T): U
        • Parameters

          • x: T

          Returns U

    Returns ((val: undefined | null | T) => undefined | null | U)

      • (val: undefined | null | T): undefined | null | U
      • Parameters

        • val: undefined | null | T

        Returns undefined | null | U