git ssb

2+

Dominic / pull-stream



Tree: 0638fd964786330ea637fe10af73a9474d43493d

Files: 0638fd964786330ea637fe10af73a9474d43493d / sinks / reduce.js

620 bytesRaw
1'use strict'
2
3var drain = require('./drain')
4
5module.exports = function reduce (/* reducer, acc, cb */) {
6 var reducer = arguments[0]
7 var acc
8 var cb
9 var sink = drain(function (data) {
10 acc = reducer(acc, data)
11 }, function (err) {
12 cb(err, acc)
13 })
14 if (arguments.length === 2) {
15 cb = arguments[2]
16 return function (source) {
17 source(null, function (end, data) {
18 //if ended immediately, and no initial...
19 if(end) return cb(end === true ? null : end)
20 acc = data; sink(source)
21 })
22 }
23 } else {
24 acc = arguments[1]
25 cb = arguments[2]
26 return sink
27 }
28}
29

Built with git-ssb-web