Last active
February 9, 2022 03:08
-
-
Save westc/b9c62d86efc015d1a444 to your computer and use it in GitHub Desktop.
Takes a function and returns the names of the parameters for that 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
/** | |
* Takes a function and returns the names of its parameters. | |
* @see https://gist.github.com/westc/b9c62d86efc015d1a444#file-getparamnames-js | |
* @param {Function} fn | |
* The function for which to get the parameter names. | |
* @returns {string[] | undefined} | |
* If `fn` is really a function and when using its `toString()` the parameters | |
* could be seen as a string then this will be an array of all of the | |
* parameters in this function. Otherwise `undefined` will be returned. | |
*/ | |
function getParamNames(fn) { | |
var params = (fn+'').replace(/\/\*[\s\S]*?\*\/|\/\/.*?[\r\n]/g, '').match(/\(([^]*?)\)/); | |
return ('function' === typeof fn && params) ? params[1].trim().split(/\s*,\s*/) : undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment