split coalesce into 2 functions
This commit is contained in:
9
snippets/coalesce-factory.md
Normal file
9
snippets/coalesce-factory.md
Normal 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"
|
||||
```
|
||||
@ -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"
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user