From df31042d689ba51e383209b2836ad971c11dee07 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 9 Mar 2016 13:06:28 -0800 Subject: remove ES6 idioms --- README.md | 6 +----- lib/autoInject.js | 13 +++++++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fab0a74..b872bc8 100644 --- a/README.md +++ b/README.md @@ -1470,19 +1470,15 @@ async.autoInject({ }); ``` -If you are using a minifier that mangles parameter names, `autoInject` will not work with plain functions. To work around this, you can explicitly specify the names of the parameters in an array, similar to Angular.js dependency injection. +If you are using a JS minifier that mangles parameter names, `autoInject` will not work with plain functions, since the parameter names will be collapsed to a single letter identifier. To work around this, you can explicitly specify the names of the parameters your task function needs in an array, similar to Angular.js dependency injection. ```js async.autoInject({ //... write_file: ['get_data', 'make_folder', function(get_data, make_folder, callback){ - // once there is some data and the directory exists, - // write the data to a file in the directory callback(null, 'filename'); }], email_link: ['write_file', function(write_file, callback){ - // once the file is written let's email a link to it... - // write_file contains the filename returned by write_file. callback(null, {'file':write_file, 'email':'user@example.com'}); }] //... diff --git a/lib/autoInject.js b/lib/autoInject.js index 1b6fea4..f84a4d9 100644 --- a/lib/autoInject.js +++ b/lib/autoInject.js @@ -1,6 +1,7 @@ import auto from './auto'; import forOwn from 'lodash/forOwn'; import arrayMap from 'lodash/_arrayMap'; +import clone from 'lodash/_baseClone'; import isArray from 'lodash/isArray'; var argsRegex = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; @@ -16,28 +17,28 @@ export default function autoInject(tasks, callback) { var params; if (isArray(taskFn)) { - params = [...taskFn]; + params = clone(taskFn); taskFn = params.pop(); - newTasks[key] = [...params].concat(newTask); + newTasks[key] = clone(params).concat(newTask); } else if (taskFn.length === 0) { throw new Error("autoInject task functions require explicit parameters."); } else if (taskFn.length === 1) { - // no dependencies + // no dependencies, use the function as-is newTasks[key] = taskFn; } else { params = parseParams(taskFn); params.pop(); - newTasks[key] = [...params].concat(newTask); - + newTasks[key] = clone(params).concat(newTask); } function newTask(results, taskCb) { var newArgs = arrayMap(params, function (name) { return results[name]; }); - taskFn(...newArgs.concat(taskCb)); + newArgs.push(taskCb); + taskFn.apply(null, newArgs); } }); -- cgit v1.2.1