Last active
December 25, 2015 06:29
-
-
Save nathggns/6932782 to your computer and use it in GitHub Desktop.
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
/** | |
* 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