diff --git a/snippets/coalesce.md b/snippets/coalesce.md new file mode 100644 index 000000000..22271dc68 --- /dev/null +++ b/snippets/coalesce.md @@ -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") -> "" +``` diff --git a/snippets/coalesceFactory.md b/snippets/coalesceFactory.md new file mode 100644 index 000000000..4b9a78924 --- /dev/null +++ b/snippets/coalesceFactory.md @@ -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" +```