Skip to content

Instantly share code, notes, and snippets.

@julien-f
Created January 29, 2019 16:46
Show Gist options
  • Save julien-f/54452ccbc1b7f8a95a2d4baf1e70ac28 to your computer and use it in GitHub Desktop.
Save julien-f/54452ccbc1b7f8a95a2d4baf1e70ac28 to your computer and use it in GitHub Desktop.
combine predicates
import { isFunction } from 'lodash'
export default function combinePredicates(...predicates) {
predicates = predicates.filter(isFunction)
const n = predicates.length
return n === 0
? undefined
: n === 1
? predicates[0]
: function() {
for (let i = 0; i < n; ++i) {
if (!predicates[i].apply(this, arguments)) {
return false
}
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment