Skip to content

Commit

Permalink
bugfixes, drop max option - not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Mar 18, 2016
1 parent 60c6239 commit fb8e734
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
30 changes: 13 additions & 17 deletions index.js
Expand Up @@ -26,12 +26,11 @@
* ```
*
* @param {Function} `fn` Function from which to get arguments names.
* @param {Number} `max` How many characters to cut from `fn`s toString.
* @return {Array}
* @api public
*/

module.exports = function functionArguments (fn, max) {
module.exports = function functionArguments (fn) {
if (typeof fn !== 'function') {
throw new TypeError('function-arguments expect a function')
}
Expand All @@ -42,23 +41,20 @@ module.exports = function functionArguments (fn, max) {
// from https://github.com/jrburke/requirejs
var reComments = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg
var fnToStr = Function.prototype.toString
var fnStr = fnToStr.call(fn).replace(reComments, '')
var arrow = fnStr.indexOf('=>')
var isNumber = require('is-number')
var fnStr = fnToStr.call(fn)
fnStr = fnStr.replace(reComments, '') || fnStr
fnStr = fnStr.slice(0, fnStr.indexOf('{'))

if (fnStr[0] !== '(' && arrow) {
fnStr = 'function (' + fnStr.slice(0, arrow) + ')' + fnStr.slice(arrow)
}
if (isNumber(max)) {
max = Number(max)
} else {
max = false
}
if (max && max > fnStr.length) {
max = fnStr.length
}
var open = fnStr.indexOf('(')
var close = fnStr.indexOf(')')

open = open >= 0 ? open + 1 : 0
close = close > 0 ? close : fnStr.indexOf('=')

fnStr = fnStr.slice(open, close)
fnStr = '(' + fnStr + ')'

var match = fnStr.slice(0, max || 100).match(/.*\(([^\)]*)\)/)
var match = fnStr.match(/\(([\s\S]*)\)/)
return match ? require('arr-map')(match[1].split(','), function (param) {
return param.trim()
}) : []
Expand Down
19 changes: 2 additions & 17 deletions test.js
Expand Up @@ -41,27 +41,12 @@ test('should work when using comments', function (done) {
test('should get array with arguments names from regular function', function () {
test.deepEqual(fnArgs(function (a, b, c) {}), ['a', 'b', 'c'])
test.deepEqual(fnArgs(function named (a, b, c) {}), ['a', 'b', 'c'])
})

// those works on modern enviroment
test('should get arguments of an arrow and generator functions', function (done) {
test.deepEqual(fnArgs(a => {}), ['a']) // eslint-disable-line arrow-parens
test.deepEqual(fnArgs((a, b) => {}), ['a', 'b'])
test.deepEqual(fnArgs(function * (a, b, c) {}), ['a', 'b', 'c'])
test.deepEqual(fnArgs(function * named (a, b, c) {}), ['a', 'b', 'c'])
})

test('should fail if `max` is bigger than fn.toString().length', function (done) {
function fixture (a, b) {
return [a, b]
}
test.deepEqual(fnArgs(fixture, 5555), ['a', 'b'])
done()
})

test('should `max` be number otherwise defaults to 100', function (done) {
function fixture (foo) {
return foo
}
test.deepEqual(fnArgs(fixture, 'str'), ['foo'])
test.deepEqual(fnArgs(fixture, '250'), ['foo'])
done()
})

0 comments on commit fb8e734

Please sign in to comment.