Skip to content

Instantly share code, notes, and snippets.

@alizdavoodi
Last active December 11, 2021 11:21
Show Gist options
  • Save alizdavoodi/41209bd0f8c8c3f6a791 to your computer and use it in GitHub Desktop.
Save alizdavoodi/41209bd0f8c8c3f6a791 to your computer and use it in GitHub Desktop.
It takes a function as an argument, along with an object containing default values for that function's arguments, and returns another function which defaults to the right values.
/* Some example which works for below func! */
function add(a,b) { return a+b;};
var add_ = defaultArguments(add,{b:9});
add_(10); // returns 19
add_(10,7); // returns 17
add_(); // returns NaN
add_ = defaultArguments(add_,{b:3, a:2});
add_(10); // returns 13 now
add_(); // returns 5
add_ = defaultArguments(add_,{c:3}); // doesn't do anything, since c isn't an argument
add_(10); // returns NaN
add_(10,10); // returns 20
//Main func defaultArguments
function defaultArguments(func, opt) {
if (func == undefined) {
return;
}
//Remove comment from arguments
function removeComments(str) {
var uid = '_' + +new Date(),
primatives = [],
primIndex = 0;
return (
str
/* Remove strings */
.replace(/(['"])(\\\1|.)+?\1/g, function(match) {
primatives[primIndex] = match;
return (uid + '') + primIndex++;
})
/* Remove Regexes */
.replace(/([^\/])(\/(?!\*|\/)(\\\/|.)+?\/[gim]{0,3})/g, function(match, $1, $2) {
primatives[primIndex] = $2;
return $1 + (uid + '') + primIndex++;
})
.replace(/\/\/.*?\/?\*.+?(?=\n|\r|$)|\/\*[\s\S]*?\/\/[\s\S]*?\*\//g, '')
/*
Remove single and multi-line comments,
no consideration of inner-contents
*/
.replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g, '')
.replace(RegExp('\\/\\*[\\s\\S]+' + uid + '\\d+', 'g'), '')
/* Bring back strings & regexes */
.replace(RegExp(uid + '(\\d+)', 'g'), function(match, n) {
return primatives[n];
})
);
}
var strf = func.toString();
var regFunc = /\(([^)]+)\)/;
var comment = /(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/[\w \']*)|(\<![\-\-\s\w\>\/]*\>)/g;
if (regFunc.exec(strf) == null) {
return func;
}
var nestedFunc = [];
if (strf.match(comment) == null) {
nestedFunc = regFunc.exec(strf)[1].split(',');
} else {
var remove_comment = removeComments(strf);
nestedFunc = regFunc.exec(remove_comment)[1].split(',');
}
var opt = opt || {};
var result = result || {};
nestedFunc.forEach(function(value, index) {
value = value.trim();
if (opt.hasOwnProperty(value)) {
result[value] = opt[value];
} else if (value == 'arg') {
for (i in opt) {
if (opt.hasOwnProperty(i)) {
result[i] = opt[i];
}
}
}
});
return function(arg) {
var argsArray = Array.prototype.slice.call(arguments);
var final = [];
if (nestedFunc == 'arg') {
argsArray.forEach(function(value, index) {
if (isNaN(value)) {
return NaN;
}
result[index] = value
});
}
argsArray.forEach(function(value, index) {
result[nestedFunc[index]] = value;
});
for (i in result) {
if (result.hasOwnProperty(i)) {
delete final;
final.push(result[i]);
}
}
if (arg == undefined && Object.keys(result).length == 0) {
return NaN;
}
return func.apply(this, final);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment