diff options
author | Ivan Seidel <ivanseidel@gmail.com> | 2016-06-18 03:04:49 -0300 |
---|---|---|
committer | Ivan Seidel <ivanseidel@gmail.com> | 2016-06-18 03:04:49 -0300 |
commit | beae0198ffe85d1a57ccc4660d8bb957d775f2f9 (patch) | |
tree | 257921e23706b412198721a2398cec606c79e97a | |
parent | 1706048929a1882c0ee7391065616b15dd9fd621 (diff) | |
download | async-beae0198ffe85d1a57ccc4660d8bb957d775f2f9.tar.gz |
Allow use es6 object method shorthand
-rw-r--r-- | lib/autoInject.js | 15 | ||||
-rw-r--r-- | mocha_test/autoInject.js | 20 |
2 files changed, 31 insertions, 4 deletions
diff --git a/lib/autoInject.js b/lib/autoInject.js index 40a250d..5e9bda6 100644 --- a/lib/autoInject.js +++ b/lib/autoInject.js @@ -5,10 +5,19 @@ import clone from 'lodash/_copyArray'; import isArray from 'lodash/isArray'; import trim from 'lodash/trim'; -var argsRegex = /^(function[^\(]*)?\(?\s*([^\)=]*)/m; +var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m; +var FN_ARG_SPLIT = /,/; +var FN_ARG = /(=.+)?(\s*)$/; +var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; function parseParams(func) { - return trim(func.toString().match(argsRegex)[2]).split(/\s*\,\s*/); + func = func.toString().replace(STRIP_COMMENTS, ''); + func = func.match(FN_ARGS)[2].replace(' ', ''); + func = func.split(FN_ARG_SPLIT); + func = func.map(function (arg){ + return trim(arg.replace(FN_ARG, '')); + }); + return func; } /** @@ -108,8 +117,6 @@ export default function autoInject(tasks, callback) { taskFn = params.pop(); newTasks[key] = params.concat(params.length > 0 ? newTask : taskFn); - } else if (taskFn.length === 0) { - throw new Error("autoInject task functions require explicit parameters."); } else if (taskFn.length === 1) { // no dependencies, use the function as-is newTasks[key] = taskFn; diff --git a/mocha_test/autoInject.js b/mocha_test/autoInject.js index d699453..2ba6d55 100644 --- a/mocha_test/autoInject.js +++ b/mocha_test/autoInject.js @@ -110,5 +110,25 @@ describe('autoInject', function () { " }); " + "}) " )(); + + /* eslint {no-eval: 0}*/ + eval("(function() { " + + " it('should work with es6 obj method syntax', function (done) { " + + " async.autoInject({ " + + " task1 (cb){ cb(null, 1) }, " + + " task2 ( task3 , cb ) { cb(null, 2) }, " + + " task3 (cb) { cb(null, 3) }, " + + " task4 ( task2 , cb ) { cb(null) }, " + + " task5 ( task4 = 4 , cb ) { cb(null, task4 + 1) } " + + " }, (err, results) => { " + + " expect(results.task1).to.equal(1); " + + " expect(results.task3).to.equal(3); " + + " expect(results.task4).to.equal(undefined); " + + " expect(results.task5).to.equal(5); " + + " done(); " + + " }); " + + " }); " + + "}) " + )(); } }); |