split coalesce into 2 functions

This commit is contained in:
xaveyaguarez
2017-12-17 11:39:24 -08:00
parent d511579fbe
commit ae61f9950a
2 changed files with 10 additions and 4 deletions

View File

@ -0,0 +1,9 @@
### Coalesce factory
Returns a function which provides a customized coalesce function
```js
const coalesceFactory = (excludes = [null, undefined]) => (...args) => args.find(_ => !excludes.includes(_))
// const customCoalesce = coalesceFactory([null, undefined, "", NaN])
// customCoalesce(undefined, null, NaN, "", "Waldo") -> "Waldo"
```

View File

@ -1,11 +1,8 @@
### Coalesce a set of arguments
Use `find()` to return the first non excluded argument.
Use `find()` to return the first non null/undefined 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"
```