Skip to content

Instantly share code, notes, and snippets.

@rodneyrehm
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save rodneyrehm/1d8788358d67ea3734f2 to your computer and use it in GitHub Desktop.

Select an option

Save rodneyrehm/1d8788358d67ea3734f2 to your computer and use it in GitHub Desktop.
extract function arguments
var parameterCommentsPattern = /((\/\/.*$)|(\/\*[\s\S]*?\*\/)|\s+)/mg;
function getParameters(func) {
var text = Function.prototype.toString.call(func)
.replace(parameterCommentsPattern, '');
if (text === 'function(){[nativecode]}') {
throw new Error('Cannot extrapolate parameters from bound and native functions');
}
var params = text
.slice(text.indexOf('(') + 1, text.indexOf(')'))
.split(',')
.filter(Boolean);
if (params.length !== func.length) {
throw new Error('Failed to extrapolate parameters properly');
}
return params;
}
var someFunc = function(a,b,/*c*/d){};
console.log(getParameters(someFunc));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment