Files: 59ec50547ac8f97c3f287fc4854692b515223939 / util.js
853 bytesRaw
1 | var pull = require('pull-stream') |
2 | var Next = require('pull-next') |
3 | |
4 | function first (list, test) { |
5 | for(var i in list) { |
6 | var value = test(list[i], i, list) |
7 | if(value) return value |
8 | } |
9 | } |
10 | |
11 | function decorate (list, value, caller) { |
12 | caller = caller || function (d,e,v) { return d(e, v) } |
13 | |
14 | return list.reduce(function (element, decorator) { |
15 | return caller(decorator, element, value) || element |
16 | }, null) |
17 | } |
18 | |
19 | exports.first = first |
20 | |
21 | exports.decorate = decorate |
22 | |
23 | exports.next = function (createStream, opts, range, property) { |
24 | |
25 | range = range || opts.reverse ? 'lt' : 'gt' |
26 | property = property || 'timestamp' |
27 | |
28 | var last = null |
29 | return Next(function () { |
30 | if(last) { |
31 | opts[range] = last[property] |
32 | } |
33 | return pull( |
34 | createStream(opts), |
35 | pull.through(function (msg) { |
36 | if(!msg.sync) last = msg |
37 | }) |
38 | ) |
39 | }) |
40 | } |
41 |
Built with git-ssb-web