Files: 15c8b380dff5fefb550257e46b87876bda2db1e4 / util.js
1113 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 | exports.decorate = decorate |
21 | |
22 | exports.next = function (createStream, opts, range, property) { |
23 | |
24 | range = range || opts.reverse ? 'lt' : 'gt' |
25 | property = property || 'timestamp' |
26 | |
27 | var last = null |
28 | return Next(function () { |
29 | if(last) { |
30 | opts[range] = last[property] |
31 | } |
32 | return pull( |
33 | createStream(opts), |
34 | pull.through(function (msg) { |
35 | if(!msg.sync) last = msg |
36 | }) |
37 | ) |
38 | }) |
39 | } |
40 | |
41 | exports.firstPlug = function (plugs) { |
42 | if(!Array.isArray(plugs)) throw new Error('plugs must be an array') |
43 | var args = [].slice.call(arguments) |
44 | var plugs = args.shift() |
45 | return exports.first(plugs, function (fn) { |
46 | return fn.apply(null, args) |
47 | }) |
48 | } |
49 | |
50 |
Built with git-ssb-web