Created
December 10, 2019 14:32
-
-
Save AngeloR/bc9a64cb75841a74b150deb8b15a1c64 to your computer and use it in GitHub Desktop.
get all arg names passed to a 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
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; | |
}); | |
} | |
/* | |
usage: | |
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