Skip to content

Commit

Permalink
ensure if max bigger than fn.toString().length
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Mar 18, 2016
1 parent 506b3dd commit e2cd9a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Expand Up @@ -48,6 +48,9 @@ module.exports = function functionArguments (fn, max) {
if (fnStr[0] !== '(' && arrow) {
fnStr = 'function (' + fnStr.slice(0, arrow) + ')' + fnStr.slice(arrow)
}
if (max > fnStr.length) {
max = fnStr.length
}

var match = fnStr.slice(0, Number(max) || 100).match(/.*\(([^\)]*)\)/)
return match ? require('arr-map')(match[1].split(','), function (param) {
Expand Down
7 changes: 7 additions & 0 deletions test.js
Expand Up @@ -49,3 +49,10 @@ test('should get array with arguments names from regular function', function ()
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()
})

0 comments on commit e2cd9a3

Please sign in to comment.