git ssb

16+

Dominic / patchbay



Tree: 26afbb3703400ed773df3fdb62dd05be51b56dab

Files: 26afbb3703400ed773df3fdb62dd05be51b56dab / util.js

1113 bytesRaw
1var pull = require('pull-stream')
2var Next = require('pull-next')
3
4function 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
11function 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
19exports.first = first
20exports.decorate = decorate
21
22exports.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
41exports.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