Files: cd76eff3ebf93df1912d899b6453a4b837d2eebb / maybe.js
1222 bytesRaw
1 | var u = require('pull-core') |
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 | |
37 | return maybeSink(function (cb) { |
38 | return drain(function (data) { |
39 | acc = reduce(acc, data) |
40 | }, function (err) { |
41 | cb(err, acc) |
42 | }) |
43 | |
44 | }, cb) |
45 | } |
46 | |
47 | var collect = exports.collect = exports.writeArray = |
48 | function (cb) { |
49 | return reduce(function (arr, item) { |
50 | arr.push(item) |
51 | return arr |
52 | }, [], cb) |
53 | } |
54 | |
55 | var concat = exports.concat = |
56 | function (cb) { |
57 | return reduce(function (a, b) { |
58 | return a + b |
59 | }, '', cb) |
60 | } |
61 | |
62 | return exports |
63 | } |
64 |
Built with git-ssb-web