Files: d9ce522f3cc18dd0c8a78fd594afe12042a3a15b / maybe.js
1143 bytesRaw
1 | var u = require('./util') |
2 | var prop = u.prop |
3 | var id = u.id |
4 | var maybeSink = u.maybeSink |
5 | |
6 | module.exports = function (pull) { |
7 | |
8 | var exports = {} |
9 | var drain = pull.drain |
10 | |
11 | var find = |
12 | exports.find = function (test, cb) { |
13 | return maybeSink(function (cb) { |
14 | var ended = false |
15 | if(!cb) |
16 | cb = test, test = id |
17 | else |
18 | test = prop(test) || id |
19 | |
20 | return drain(function (data) { |
21 | if(test(data)) { |
22 | ended = true |
23 | cb(null, data) |
24 | return false |
25 | } |
26 | }, function (err) { |
27 | if(ended) return //already called back |
28 | cb(err === true ? null : err, null) |
29 | }) |
30 | |
31 | }, cb) |
32 | } |
33 | |
34 | var reduce = exports.reduce = |
35 | function (reduce, acc, cb) { |
36 | console.log('maybeSink---', reduce, acc, cb) |
37 | |
38 | return maybeSink(function (cb) { |
39 | return drain(function (data) { |
40 | acc = reduce(acc, data) |
41 | }, function (err) { |
42 | cb(err, acc) |
43 | }) |
44 | |
45 | }, cb) |
46 | } |
47 | |
48 | var collect = exports.collect = exports.writeArray = |
49 | function (cb) { |
50 | return reduce(function (arr, item) { |
51 | arr.push(item) |
52 | return arr |
53 | }, [], cb) |
54 | } |
55 | |
56 | return exports |
57 | } |
58 |
Built with git-ssb-web