Files
30-seconds-of-code/snippets/coalesceFactory.md
2017-12-28 08:30:19 +00:00

15 lines
511 B
Markdown

### coalesceFactory
Returns a customized coalesce function that returns the first argument that returns `true` from the provided argument validation function.
Use `Array.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"
```