Created
January 29, 2019 16:46
-
-
Save julien-f/54452ccbc1b7f8a95a2d4baf1e70ac28 to your computer and use it in GitHub Desktop.
combine predicates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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