Argument coalescing factory

JavaScript, Function, Type · Oct 22, 2020

Customizes a coalesce function that returns the first argument which is true based on the given validator.

  • Use Array.prototype.find() to return the first argument that returns true from the provided argument validation function, valid.
const coalesceFactory = valid => (...args) => args.find(valid);

const customCoalesce = coalesceFactory(
  v => ![null, undefined, '', NaN].includes(v)
);
customCoalesce(undefined, null, NaN, '', 'Waldo'); // 'Waldo'

More like this

  • JavaScript Types

    A snippet collection of type helpers and explanations of JavaScript types.

    Collection · 49 snippets

  • Logical or for functions

    Checks if at least one function returns true for a given set of arguments.

    JavaScript, Function · Oct 19, 2020

  • Flip function arguments

    Takes a function as an argument, then makes the first argument the last.

    JavaScript, Function · Jun 13, 2021

  • Call functions with context

    Given a key and a set of arguments, call them when given a context.

    JavaScript, Function · Jun 13, 2021