1
|
'use strict';
|
2
|
|
3
|
Object.defineProperty(exports, "__esModule", {
|
4
|
value: true
|
5
|
});
|
6
|
exports.default = reduce;
|
7
|
|
8
|
var _eachOfSeries = require('./eachOfSeries');
|
9
|
|
10
|
var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);
|
11
|
|
12
|
var _noop = require('lodash/noop');
|
13
|
|
14
|
var _noop2 = _interopRequireDefault(_noop);
|
15
|
|
16
|
var _once = require('./internal/once');
|
17
|
|
18
|
var _once2 = _interopRequireDefault(_once);
|
19
|
|
20
|
var _wrapAsync = require('./internal/wrapAsync');
|
21
|
|
22
|
var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
23
|
|
24
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
25
|
|
26
|
/**
|
27
|
* Reduces `coll` into a single value using an async `iteratee` to return each
|
28
|
* successive step. `memo` is the initial state of the reduction. This function
|
29
|
* only operates in series.
|
30
|
*
|
31
|
* For performance reasons, it may make sense to split a call to this function
|
32
|
* into a parallel map, and then use the normal `Array.prototype.reduce` on the
|
33
|
* results. This function is for situations where each step in the reduction
|
34
|
* needs to be async; if you can get the data before reducing it, then it's
|
35
|
* probably a good idea to do so.
|
36
|
*
|
37
|
* @name reduce
|
38
|
* @static
|
39
|
* @memberOf module:Collections
|
40
|
* @method
|
41
|
* @alias inject
|
42
|
* @alias foldl
|
43
|
* @category Collection
|
44
|
* @param {Array|Iterable|Object} coll - A collection to iterate over.
|
45
|
* @param {*} memo - The initial state of the reduction.
|
46
|
* @param {AsyncFunction} iteratee - A function applied to each item in the
|
47
|
* array to produce the next step in the reduction.
|
48
|
* The `iteratee` should complete with the next state of the reduction.
|
49
|
* If the iteratee complete with an error, the reduction is stopped and the
|
50
|
* main `callback` is immediately called with the error.
|
51
|
* Invoked with (memo, item, callback).
|
52
|
* @param {Function} [callback] - A callback which is called after all the
|
53
|
* `iteratee` functions have finished. Result is the reduced value. Invoked with
|
54
|
* (err, result).
|
55
|
* @example
|
56
|
*
|
57
|
* async.reduce([1,2,3], 0, function(memo, item, callback) {
|
58
|
* // pointless async:
|
59
|
* process.nextTick(function() {
|
60
|
* callback(null, memo + item)
|
61
|
* });
|
62
|
* }, function(err, result) {
|
63
|
* // result is now equal to the last value of memo, which is 6
|
64
|
* });
|
65
|
*/
|
66
|
function reduce(coll, memo, iteratee, callback) {
|
67
|
callback = (0, _once2.default)(callback || _noop2.default);
|
68
|
var _iteratee = (0, _wrapAsync2.default)(iteratee);
|
69
|
(0, _eachOfSeries2.default)(coll, function (x, i, callback) {
|
70
|
_iteratee(memo, x, function (err, v) {
|
71
|
memo = v;
|
72
|
callback(err);
|
73
|
});
|
74
|
}, function (err) {
|
75
|
callback(err, memo);
|
76
|
});
|
77
|
}
|
78
|
module.exports = exports['default'];
|