Last active
November 3, 2015 13:34
-
-
Save XOP/fd3541d031ff916df434 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
function getArgs(func) { | |
// First match everything inside the function argument parens. | |
var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1]; | |
// Split the arguments string into an array comma delimited. | |
return args.split(',').map(function(arg) { | |
// Ensure no inline comments are parsed and trim the whitespace. | |
return arg.replace(/\/\*.*\*\//, '').trim(); | |
}).filter(function(arg) { | |
// Ensure no undefined values are added. | |
return arg; | |
}); | |
} | |
/* | |
So given the function above and a sample function, here's how it would work: | |
*/ | |
function myCustomFn(arg1, arg2,arg3) { | |
} | |
console.log(getArgs(myCustomFn)); // ["arg1", "arg2", "arg3"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment