Files
30-seconds-of-code/snippets/coalesceFactory.md
2020-09-15 21:52:00 +03:00

558 B

title, tags
title tags
coalesceFactory type,intermediate

Returns a customized coalesce function that returns the first argument that returns true from the provided argument validation function.

  • Use Array.prototype.find() to return the first argument that returns true from the provided argument validation function.
const coalesceFactory = valid => (...args) => args.find(valid);
const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));
customCoalesce(undefined, null, NaN, '', 'Waldo'); // "Waldo"