Files: 6f0c7afd16d33393e0a972dcf17ba5d5a4d48df7 / util.js
1114 bytesRaw
1 | var Map = require('pull-stream/throughs/map') |
2 | |
3 | // opts standardized to work like levelup api |
4 | function stdopts (opts) { |
5 | opts = opts || {} |
6 | opts.keys = opts.keys !== false //default keys to true |
7 | opts.values = opts.values !== false //default values to true |
8 | return opts |
9 | } |
10 | |
11 | function msgFmt (keys, values, obj) { |
12 | if (keys && values) |
13 | return obj |
14 | if (keys) |
15 | return obj.key |
16 | if (values) |
17 | return obj.value |
18 | return null // i guess? |
19 | } |
20 | |
21 | exports.options = stdopts |
22 | exports.format = msgFmt |
23 | |
24 | exports.lo = null |
25 | exports.hi = undefined |
26 | |
27 | exports.wait = function () { |
28 | var waiting = [], value |
29 | return { |
30 | get: function () { return value }, |
31 | set: function (_value) { |
32 | value = _value |
33 | while(waiting.length) |
34 | waiting.shift()(null, value) |
35 | }, |
36 | wait: function (cb) { |
37 | if(value !== undefined) cb(null, value) |
38 | else waiting.push(cb) |
39 | } |
40 | } |
41 | } |
42 | exports.formatStream = function (keys, values) { |
43 | return Map(function (data) { |
44 | if(data.sync) return data |
45 | return keys && values ? data.value : keys ? data.value.key : data.value.value |
46 | }) |
47 | } |
48 | |
49 |
Built with git-ssb-web