Last active
August 29, 2015 14:08
-
-
Save casperin/8173950c2d986b46696b to your computer and use it in GitHub Desktop.
Get parameter names from a 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
//+ 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'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To anyone who finds this and find it useful; be aware that uglifying the code will completely wreck it.