-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
33 lines (32 loc) · 988 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const defaults = require('lodash.defaults');
const each = require('apr-engine-each');
/**
* <a id="reduce"></a>
* Reduces `coll` into a single value using an async `iteratee` to return each successive step.
*
* [![](https://img.shields.io/npm/v/apr-reduce.svg?style=flat-square)](https://www.npmjs.com/package/apr-reduce) [![](https://img.shields.io/npm/l/apr-reduce.svg?style=flat-square)](https://www.npmjs.com/package/apr-reduce)
*
* @kind function
* @name reduce
* @param {Array|Object|Iterable} input
* @param {Function} iteratee
* @returns {Promise}
*
* @example
* import reduce from 'apr-reduce';
*
* const sum = await reduce([1, 2, 3], async (sum, item) =>
* new Promise((resolve) => resolve(sum + item))
* );
*/
module.exports = (input, fn, sum, opts) =>
each({
input,
opts: defaults(opts, {
limit: 1
}),
after: (value, item) => {
sum = value;
},
call: item => fn(sum, item.value, item.key, input)
}).then(() => sum);