coalesce snippet

This commit is contained in:
xaveyaguarez
2017-12-17 00:08:55 -08:00
parent 2a30131669
commit 48f11812bd

11
snippets/coalesce.md Normal file
View File

@ -0,0 +1,11 @@
### Coalesce a set of arguments
Use `find()` to return the first non excluded argument.
```js
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_))
// coalesce(null,undefined,"",NaN, "Waldo") -> ""
const coalesceFactory = (excludes = [null, undefined]) => (...args) => args.find(_ => !excludes.includes(_))
// coalesceFactory([null, undefined, "", NaN])(undefined, null, NaN, "", "Waldo") -> "Waldo"
```