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

  • Logical or for functions

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

    JavaScript, Function · Oct 19, 2020

  • Bind object method

    Creates a function that invokes the method at a given key of an object, optionally prepending any additional supplied parameters to the arguments.

    JavaScript, Function · Oct 18, 2020

  • Bind function context

    Creates a function that invokes fn with a given context, optionally prepending any additional supplied parameters to the arguments.

    JavaScript, Function · Oct 18, 2020