Skip to content

Instantly share code, notes, and snippets.

@Xanir
Created March 24, 2015 17:22
Show Gist options
  • Save Xanir/b7419e31922ac2960eb3 to your computer and use it in GitHub Desktop.
Save Xanir/b7419e31922ac2960eb3 to your computer and use it in GitHub Desktop.
Mod Function
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