Skip to content

Instantly share code, notes, and snippets.

@ttepasse
Created June 29, 2011 22:12
Show Gist options
  • Save ttepasse/1055146 to your computer and use it in GitHub Desktop.
Save ttepasse/1055146 to your computer and use it in GitHub Desktop.
Function.prototype.and = function () {
var predicates = Array.prototype.slice.call(arguments);
predicates.unshift(this);
return (function (value) {
return predicates.every(function (predicate) {
return predicate(value);
})
});
}
Function.prototype.or = function () {
var predicates = Array.prototype.slice.call(arguments);
predicates.unshift(this);
return (function (value) {
return predicates.some(function (predicate) {
return predicate(value);
})
});
}
// Usage:
var isBoth = isFoo.and(isBar),
isOneof = isFoo.or(isBar);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment