Created
March 24, 2015 17:22
-
-
Save Xanir/b7419e31922ac2960eb3 to your computer and use it in GitHub Desktop.
Mod Function
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
var escapeRegexpSpecialChars = function(str) { | |
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); | |
} | |
var funcModder = function(func, replacements) { | |
var funcStr = func.toString(); | |
Object.keys(replacements).forEach(function(replaceKey) { | |
var escappedReplacement = escapeRegexpSpecialChars(replaceKey); | |
funcStr = funcStr.replace(new RegExp(escappedReplacement, 'g'), replacements[replaceKey]); | |
}); | |
return (function() {eval('var fn = ' + funcStr); return fn;})().toString(); // jshint ignore:line | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment