Skip to content

Instantly share code, notes, and snippets.

@nathggns
Last active December 25, 2015 06:29
Show Gist options
  • Save nathggns/6932782 to your computer and use it in GitHub Desktop.
Save nathggns/6932782 to your computer and use it in GitHub Desktop.
/**
* Protect a callback based on a condition
* @param {Function} protector The condition to protect with
* @param {Function} protectee The callback to protect
* @param {Boolean} not Should the protector protect using
* true or false results
* @param {mixed} context Optional context to call protector
* and protectee with
* @return {Function} The protected callback
*/
function protect(protector, protectee, not, context) {
if (typeof not === 'undefined') {
not = false;
}
return function() {
var result = protector.apply(context || this, arguments);
if (typeof not === 'function') {
not = not.apply(context || this, arguments);
}
if (not) {
result = !result;
}
if (result) {
return;
}
return protectee.apply(context || this, arguments);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment