Skip to content

Instantly share code, notes, and snippets.

@westc
Last active February 9, 2022 03:08
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 westc/b9c62d86efc015d1a444 to your computer and use it in GitHub Desktop.
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.
/**
* 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