Files
30-seconds-of-code/snippets/js/s/coalesce-factory.md
2023-05-07 16:07:29 +03:00

651 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Argument coalescing factory snippet javascript
function
type
coffee-phone-tray 2020-10-22T20:23:47+03:00

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'