Skip to content

Commit

Permalink
preserve order, make variadic and handle falsy values in concat [fixes
Browse files Browse the repository at this point in the history
…#1437] (#1436)

* preserve order in concat

* remove comment

* fix concatLimit linting

* handle variadic and falsy results in concatLimit [fixes #1437]

* fix tests (typos)

* PR fixes
  • Loading branch information
hargasinski committed Jun 23, 2017
1 parent 2f14cac commit e9b2855
Show file tree
Hide file tree
Showing 10 changed files with 486 additions and 102 deletions.
6 changes: 3 additions & 3 deletions lib/concat.js
@@ -1,5 +1,5 @@
import concat from './internal/concat';
import doParallel from './internal/doParallel';
import doLimit from './internal/doLimit';
import concatLimit from './concatLimit';

/**
* Applies `iteratee` to each item in `coll`, concatenating the results. Returns
Expand All @@ -26,4 +26,4 @@ import doParallel from './internal/doParallel';
* // files is now a list of filenames that exist in the 3 directories
* });
*/
export default doParallel(concat);
export default doLimit(concatLimit, Infinity);
30 changes: 26 additions & 4 deletions lib/concatLimit.js
@@ -1,5 +1,9 @@
import doParallelLimit from './internal/doParallelLimit';
import concat from './internal/concat';
import noop from 'lodash/noop';
import wrapAsync from './internal/wrapAsync';
import slice from './internal/slice';
import mapLimit from './mapLimit';

var _concat = Array.prototype.concat;

/**
* The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time.
Expand All @@ -14,9 +18,27 @@ import concat from './internal/concat';
* @param {number} limit - The maximum number of async operations at a time.
* @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
* which should use an array as its result. Invoked with (item, callback).
* @param {Function} [callback(err)] - A callback which is called after all the
* @param {Function} [callback] - A callback which is called after all the
* `iteratee` functions have finished, or an error occurs. Results is an array
* containing the concatenated results of the `iteratee` function. Invoked with
* (err, results).
*/
export default doParallelLimit(concat);
export default function(coll, limit, iteratee, callback) {
callback = callback || noop;
var _iteratee = wrapAsync(iteratee);
mapLimit(coll, limit, function(val, callback) {
_iteratee(val, function(err /*, ...args*/) {
if (err) return callback(err);
return callback(null, slice(arguments, 1));
});
}, function(err, mapResults) {
var result = [];
for (var i = 0; i < mapResults.length; i++) {
if (mapResults[i]) {
result = _concat.apply(result, mapResults[i]);
}
}

return callback(err, result);
});
}
6 changes: 3 additions & 3 deletions lib/concatSeries.js
@@ -1,5 +1,5 @@
import concat from './internal/concat';
import doSeries from './internal/doSeries';
import doLimit from './internal/doLimit';
import concatLimit from './concatLimit';

/**
* The same as [`concat`]{@link module:Collections.concat} but runs only a single async operation at a time.
Expand All @@ -19,4 +19,4 @@ import doSeries from './internal/doSeries';
* containing the concatenated results of the `iteratee` function. Invoked with
* (err, results).
*/
export default doSeries(concat);
export default doLimit(concatLimit, 1);
11 changes: 0 additions & 11 deletions lib/internal/concat.js

This file was deleted.

8 changes: 0 additions & 8 deletions lib/internal/doSeries.js

This file was deleted.

0 comments on commit e9b2855

Please sign in to comment.