1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _eachOfSeries = require('./eachOfSeries.js');
|
8 |
|
9 | var _eachOfSeries2 = _interopRequireDefault(_eachOfSeries);
|
10 |
|
11 | var _once = require('./internal/once.js');
|
12 |
|
13 | var _once2 = _interopRequireDefault(_once);
|
14 |
|
15 | var _wrapAsync = require('./internal/wrapAsync.js');
|
16 |
|
17 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
18 |
|
19 | var _awaitify = require('./internal/awaitify.js');
|
20 |
|
21 | var _awaitify2 = _interopRequireDefault(_awaitify);
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
24 |
|
25 | /**
|
26 | * Reduces `coll` into a single value using an async `iteratee` to return each
|
27 | * successive step. `memo` is the initial state of the reduction. This function
|
28 | * only operates in series.
|
29 | *
|
30 | * For performance reasons, it may make sense to split a call to this function
|
31 | * into a parallel map, and then use the normal `Array.prototype.reduce` on the
|
32 | * results. This function is for situations where each step in the reduction
|
33 | * needs to be async; if you can get the data before reducing it, then it's
|
34 | * probably a good idea to do so.
|
35 | *
|
36 | * @name reduce
|
37 | * @static
|
38 | * @memberOf module:Collections
|
39 | * @method
|
40 | * @alias inject
|
41 | * @alias foldl
|
42 | * @category Collection
|
43 | * @param {Array|Iterable|AsyncIterable|Object} coll - A collection to iterate over.
|
44 | * @param {*} memo - The initial state of the reduction.
|
45 | * @param {AsyncFunction} iteratee - A function applied to each item in the
|
46 | * array to produce the next step in the reduction.
|
47 | * The `iteratee` should complete with the next state of the reduction.
|
48 | * If the iteratee completes with an error, the reduction is stopped and the
|
49 | * main `callback` is immediately called with the error.
|
50 | * Invoked with (memo, item, callback).
|
51 | * @param {Function} [callback] - A callback which is called after all the
|
52 | * `iteratee` functions have finished. Result is the reduced value. Invoked with
|
53 | * (err, result).
|
54 | * @returns {Promise} a promise, if no callback is passed
|
55 | * @example
|
56 | *
|
57 | * // file1.txt is a file that is 1000 bytes in size
|
58 | * // file2.txt is a file that is 2000 bytes in size
|
59 | * // file3.txt is a file that is 3000 bytes in size
|
60 | * // file4.txt does not exist
|
61 | *
|
62 | * const fileList = ['file1.txt','file2.txt','file3.txt'];
|
63 | * const withMissingFileList = ['file1.txt','file2.txt','file3.txt', 'file4.txt'];
|
64 | *
|
65 | * // asynchronous function that computes the file size in bytes
|
66 | * // file size is added to the memoized value, then returned
|
67 | * function getFileSizeInBytes(memo, file, callback) {
|
68 | * fs.stat(file, function(err, stat) {
|
69 | * if (err) {
|
70 | * return callback(err);
|
71 | * }
|
72 | * callback(null, memo + stat.size);
|
73 | * });
|
74 | * }
|
75 | *
|
76 | * // Using callbacks
|
77 | * async.reduce(fileList, 0, getFileSizeInBytes, function(err, result) {
|
78 | * if (err) {
|
79 | * console.log(err);
|
80 | * } else {
|
81 | * console.log(result);
|
82 | * // 6000
|
83 | * // which is the sum of the file sizes of the three files
|
84 | * }
|
85 | * });
|
86 | *
|
87 | * // Error Handling
|
88 | * async.reduce(withMissingFileList, 0, getFileSizeInBytes, function(err, result) {
|
89 | * if (err) {
|
90 | * console.log(err);
|
91 | * // [ Error: ENOENT: no such file or directory ]
|
92 | * } else {
|
93 | * console.log(result);
|
94 | * }
|
95 | * });
|
96 | *
|
97 | * // Using Promises
|
98 | * async.reduce(fileList, 0, getFileSizeInBytes)
|
99 | * .then( result => {
|
100 | * console.log(result);
|
101 | * // 6000
|
102 | * // which is the sum of the file sizes of the three files
|
103 | * }).catch( err => {
|
104 | * console.log(err);
|
105 | * });
|
106 | *
|
107 | * // Error Handling
|
108 | * async.reduce(withMissingFileList, 0, getFileSizeInBytes)
|
109 | * .then( result => {
|
110 | * console.log(result);
|
111 | * }).catch( err => {
|
112 | * console.log(err);
|
113 | * // [ Error: ENOENT: no such file or directory ]
|
114 | * });
|
115 | *
|
116 | * // Using async/await
|
117 | * async () => {
|
118 | * try {
|
119 | * let result = await async.reduce(fileList, 0, getFileSizeInBytes);
|
120 | * console.log(result);
|
121 | * // 6000
|
122 | * // which is the sum of the file sizes of the three files
|
123 | * }
|
124 | * catch (err) {
|
125 | * console.log(err);
|
126 | * }
|
127 | * }
|
128 | *
|
129 | * // Error Handling
|
130 | * async () => {
|
131 | * try {
|
132 | * let result = await async.reduce(withMissingFileList, 0, getFileSizeInBytes);
|
133 | * console.log(result);
|
134 | * }
|
135 | * catch (err) {
|
136 | * console.log(err);
|
137 | * // [ Error: ENOENT: no such file or directory ]
|
138 | * }
|
139 | * }
|
140 | *
|
141 | */
|
142 | function reduce(coll, memo, iteratee, callback) {
|
143 | callback = (0, _once2.default)(callback);
|
144 | var _iteratee = (0, _wrapAsync2.default)(iteratee);
|
145 | return (0, _eachOfSeries2.default)(coll, (x, i, iterCb) => {
|
146 | _iteratee(memo, x, (err, v) => {
|
147 | memo = v;
|
148 | iterCb(err);
|
149 | });
|
150 | }, err => callback(err, memo));
|
151 | }
|
152 | exports.default = (0, _awaitify2.default)(reduce, 4);
|
153 | module.exports = exports.default; |
\ | No newline at end of file |