Merge pull request #205 from prodigic/master

coalesce snippet
This commit is contained in:
Angelos Chalaris
2017-12-18 12:16:26 +02:00
committed by GitHub
2 changed files with 21 additions and 0 deletions

10
snippets/coalesce.md Normal file
View File

@ -0,0 +1,10 @@
### coalesce
Returns the first non-null/undefined argument.
Use `Array.find()` to return the first non `null`/`undefined` argument.
```js
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_))
// coalesce(null,undefined,"",NaN, "Waldo") -> ""
```

View File

@ -0,0 +1,11 @@
### 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);
// const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_))
// customCoalesce(undefined, null, NaN, "", "Waldo") //-> "Waldo"
```