Files
30-seconds-of-code/snippets/coalesceFactory.md
Isabelle Viktoria Maciohsek 2b6f2b1740 Update snippet descriptions & tags
2020-10-18 23:04:45 +03:00

544 B

title, tags
title tags
coalesceFactory function,type,intermediate

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'