Files
30-seconds-of-code/snippets/coalesceFactory.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

17 lines
558 B
Markdown

---
title: coalesceFactory
tags: utility,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.
```js
const coalesceFactory = valid => (...args) => args.find(valid);
```
```js
const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));
customCoalesce(undefined, null, NaN, '', 'Waldo'); // "Waldo"
```