New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
preserve order, make variadic and handle falsy values in concat #1436
Changes from 5 commits
f37078f
e3a341b
ee70042
1382f30
39b20e6
a0ffde8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
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'; | ||
|
||
/** | ||
* The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time. | ||
|
@@ -14,9 +16,28 @@ 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 = []; | ||
var _concat = Array.prototype.concat; | ||
for (var i = 0; i < mapResults.length; i++) { | ||
if (mapResults[i]) { | ||
result = _concat.apply(result, mapResults[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
var result = {0: 'foo', 1: 'bar', 2: 'baz', length: 3};
// previously
[].concat(result); // [{0: 'foo', 1: 'bar', 2: 'baz', length: 3}]
// now
Array.prototype.concat.apply([], args); // ['foo', 'bar', 'baz'] Array.prototype.concat.apply([], [args]); // [{0: 'foo', 1: 'bar', 2: 'baz', length: 3}] keeping the previous behaviour. |
||
} | ||
} | ||
|
||
return callback(err, result); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this the last use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. From the history, it looks like all of the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's time to delete it, then! |
||
export default doLimit(concatLimit, 1); |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can move this var to the top level scope.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, thank you!