Files
30-seconds-of-code/snippets/coalesceFactory.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

649 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Argument coalescing factory function,type intermediate 2017-12-18T12:15:36+02:00 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'