Skip to content

Instantly share code, notes, and snippets.

@omnidan
Forked from Bear-Foot/combineFilters.js
Last active April 2, 2016 00:03
Show Gist options
  • Save omnidan/3432a5dc51e876e2803cc59998562c09 to your computer and use it in GitHub Desktop.
Save omnidan/3432a5dc51e876e2803cc59998562c09 to your computer and use it in GitHub Desktop.
const arg1 = (arg) => arg !== 1
const arg2 = (arg) => arg !== 2
function combineFilters (filters) {
return filters.reduce((prev, curr) =>
(arg) => prev(arg) && curr(arg)
, () => true)
}
console.log(combineFilters([arg2, arg1, arg1])(1)); // false
console.log(combineFilters([arg2, arg1, arg1])(2)); // false
console.log(combineFilters([arg2, arg1, arg1])(3)); // true
@peteruithoven
Copy link

You could also skip having people adding the array, by using the spread operator to parse the arguments.

const arg1 = (arg) => arg !== 1
const arg2 = (arg) => arg !== 2

function combineFilters (...filters) {
  return filters.reduce((prev, curr) =>
    (arg) => prev(arg) && curr(arg)
  , () => true)
}

console.log(combineFilters(arg2, arg1, arg1)(1)); // false
console.log(combineFilters(arg2, arg1, arg1)(2)); // false
console.log(combineFilters(arg2, arg1, arg1)(3)); // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment