@metreeca/core - v0.10.0
    Preparing search index...

    Function opt

    Applies a mapper to an optional value, short-circuiting an undefined one.

    • Applies a mapper to an optional value.

      Extends a mapper with a tolerance for undefined values: a defined value is handed on to it stripped of undefined, while an undefined one short-circuits to undefined without calling it, so an optional value flows through a chain of total functions without a guard at every step.

      Unlike optional chaining, this covers an arbitrary mapper rather than property access alone, and short-circuits on undefined alone: definedness is about assignment rather than emptiness, so null is mapped like any other value.

      Type Parameters

      • V

        The type of the value to map, possibly including undefined

      • R

        The type of the value reported by mapper

      Parameters

      • value: V

        The value to hand on to mapper, if defined

      • mapper: (value: Defined<V>) => R

        The mapper to apply to value, if defined

      Returns Optional<R>

      undefined if value is undefined, and the value computed from value by mapper otherwise; errors raised by mapper propagate to the caller unchanged

      opt(ports.get(name), port => `localhost:${port}`); // string or undefined (the mapper is not called)
      
    • Applies a mapper to an optional value, falling back to a default.

      Closes the undefined case with a value of its own, so the result is defined whatever the input: a defined value is mapped as above, while an undefined one reports fallback without calling the mapper. This states a total computation in one call, where the two-argument form would leave the caller to close the gap with ??.

      The fallback may be deferred as a no-arg function, matching the short-circuiting ?? supplies: a deferred fallback is resolved only if the value is actually undefined, so a costly default is not computed on the mapped path.

      Important

      A fallback supplied as a function is always taken as a deferred one. Where R is itself a function type, wrap the default in a thunk reporting it (() => fallback), or it is called instead of being reported.

      Type Parameters

      • V

        The type of the value to map, possibly including undefined

      • R

        The type of the value reported by mapper and of the fallback

      Parameters

      • value: V

        The value to hand on to mapper, if defined

      • mapper: (value: Defined<V>) => R

        The mapper to apply to value, if defined

      • fallback: Lazy<R>

        The value reported if value is undefined, either outright or as a function computing it on demand

      Returns R

      The value computed from value by mapper if value is defined, and fallback otherwise; errors raised by mapper or by a deferred fallback propagate to the caller unchanged

      opt(ports.get(name), port => `localhost:${port}`, "localhost:80"); // string
      opt(ports.get(name), port => `localhost:${port}`, () => probe()); // string (probed only if the port is missing)