Skip to content

Instantly share code, notes, and snippets.

@casperin
Last active August 29, 2015 14:08
Show Gist options
  • Save casperin/8173950c2d986b46696b to your computer and use it in GitHub Desktop.
Save casperin/8173950c2d986b46696b to your computer and use it in GitHub Desktop.
Get parameter names from a function
//+ fn -> [a]
var getParameterNames = (function () {
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
ARGUMENT_NAMES = /([^\s,]+)/g;
return function (fn) {
var fnStr = fn.toString().replace(STRIP_COMMENTS, ''),
names = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
return names || [];
};
})();
function add (x, y) {
return x + y;
}
console.log(getParameterNames(add)); // => ['x', 'y']
@casperin
Copy link
Author

To anyone who finds this and find it useful; be aware that uglifying the code will completely wreck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment