Skip to content

Instantly share code, notes, and snippets.

@Xanir Xanir/Mod Function
Created Mar 24, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.