Files: 9624740f4ee88cb0bc96ed4cc1a0c295f6cef895 / sources.js
1586 bytesRaw
1 | |
2 | var keys = exports.keys = |
3 | function (object) { |
4 | return values(Object.keys(object)) |
5 | } |
6 | |
7 | function abortCb(cb, abort, onAbort) { |
8 | cb(abort) |
9 | onAbort && onAbort(abort === true ? null: abort) |
10 | return |
11 | } |
12 | |
13 | var once = exports.once = |
14 | function (value, onAbort) { |
15 | return function (abort, cb) { |
16 | if(abort) |
17 | return abortCb(cb, abort, onAbort) |
18 | if(value != null) { |
19 | var _value = value; value = null |
20 | cb(null, _value) |
21 | } else |
22 | cb(true) |
23 | } |
24 | } |
25 | |
26 | var values = exports.values = exports.readArray = |
27 | function (array, onAbort) { |
28 | if(!array) |
29 | return function (abort, cb) { |
30 | if(abort) return abortCb(cb, abort, onAbort) |
31 | return cb(true) |
32 | } |
33 | if(!Array.isArray(array)) |
34 | array = Object.keys(array).map(function (k) { |
35 | return array[k] |
36 | }) |
37 | var i = 0 |
38 | return function (abort, cb) { |
39 | if(abort) |
40 | return abortCb(cb, abort, onAbort) |
41 | cb(i >= array.length || null, array[i++]) |
42 | } |
43 | } |
44 | |
45 | |
46 | var count = exports.count = |
47 | function (max) { |
48 | var i = 0; max = max || Infinity |
49 | return function (end, cb) { |
50 | if(end) return cb && cb(end) |
51 | if(i > max) |
52 | return cb(true) |
53 | cb(null, i++) |
54 | } |
55 | } |
56 | |
57 | var infinite = exports.infinite = |
58 | function (generate) { |
59 | generate = generate || Math.random |
60 | return function (end, cb) { |
61 | if(end) return cb && cb(end) |
62 | return cb(null, generate()) |
63 | } |
64 | } |
65 | |
66 | //a stream that ends immediately. |
67 | var empty = exports.empty = function () { |
68 | return function (abort, cb) { |
69 | cb(true) |
70 | } |
71 | } |
72 | |
73 | //a stream that errors immediately. |
74 | var error = exports.error = function (err) { |
75 | return function (abort, cb) { |
76 | cb(err) |
77 | } |
78 | } |
79 | |
80 |
Built with git-ssb-web