diff --git a/index.js b/index.js index c41a8aa..235bb9e 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/test.js b/test.js index 7f3ede9..8e77be2 100644 --- a/test.js +++ b/test.js @@ -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() +})