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

18 lines
544 B
Markdown

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