Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Last active May 30, 2020 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWells/67976a2e59958672d135e2f8c41f3868 to your computer and use it in GitHub Desktop.
Save DavidWells/67976a2e59958672d135e2f8c41f3868 to your computer and use it in GitHub Desktop.
getFunctionArgsByName will return the argument key names. Careful using with minification
function getFunctionArgsByName(func) {
return (func + '')
.replace(/[/][/].*$/mg,'') // strip single-line comments
.replace(/\s+/g, '') // strip white space
.replace(/[/][*][^/*]*[*][/]/g, '') // strip multi-line comments
.split('){', 1)[0].replace(/^[^(]*[(]/, '') // extract the parameters
.replace(/=[^,]+/g, '') // strip any ES6 defaults
.split(',').filter(Boolean); // split & filter [""]
}
function xyz(lol, hi, cool) {
// stuff
}
const theArgs = getFunctionArgsByName(xyz)
// ['lol', 'hi', 'cool']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment