Files: cb323a7b5f1f6750d7e9bc16d375f9c48b688dab / sinks / reduce.js
609 bytesRaw
1 | |
2 | |
3 | var drain = require('./drain') |
4 | |
5 | module.exports = function reduce (/* reducer, acc, cb */) { |
6 | var reducer = arguments[0] |
7 | var acc |
8 | var cb |
9 | if (arguments.length === 3) { |
10 | acc = arguments[1] |
11 | cb = arguments[2] |
12 | return drain(function (data) { |
13 | acc = reducer(acc, data) |
14 | }, function (err) { |
15 | cb(err, acc) |
16 | }) |
17 | } else { |
18 | cb = arguments[1] |
19 | var first = true |
20 | return drain(function (data) { |
21 | if (first) { |
22 | acc = data |
23 | first = false |
24 | } else { |
25 | acc = reducer(acc, data) |
26 | } |
27 | }, function (err) { |
28 | cb(err, acc) |
29 | }) |
30 | } |
31 | } |
32 | |
33 |
Built with git-ssb-web