Commit 4030fd72088498cc22acca950c4a1bb121937cc0
tidy the many files away into directories
Dominic Tarr committed on 5/19/2016, 8:19:37 PMParent: 7aa21412f6c0bd6b84022d67ff8e51711eddb510
Files changed
util/abortCb.js | ||
---|---|---|
@@ -1,6 +1,0 @@ | ||
1 | -module.exports = function abortCb(cb, abort, onAbort) { | |
2 | - cb(abort) | |
3 | - onAbort && onAbort(abort === true ? null: abort) | |
4 | - return | |
5 | -} | |
6 | - |
util/abort-cb.js | ||
---|---|---|
@@ -1,0 +1,6 @@ | ||
1 | +module.exports = function abortCb(cb, abort, onAbort) { | |
2 | + cb(abort) | |
3 | + onAbort && onAbort(abort === true ? null: abort) | |
4 | + return | |
5 | +} | |
6 | + |
asyncMap.js | ||
---|---|---|
@@ -1,42 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var id = require('./util/id') | |
4 | -var prop = require('./util/prop') | |
5 | - | |
6 | -module.exports = function asyncMap (map) { | |
7 | - if(!map) return id | |
8 | - map = prop(map) | |
9 | - var busy = false, abortCb, aborted | |
10 | - return function (read) { | |
11 | - return function next (abort, cb) { | |
12 | - if(aborted) return cb(aborted) | |
13 | - if(abort) { | |
14 | - aborted = abort | |
15 | - if(!busy) read(abort, cb) | |
16 | - else read(abort, function () { | |
17 | - //if we are still busy, wait for the mapper to complete. | |
18 | - if(busy) abortCb = cb | |
19 | - else cb(abort) | |
20 | - }) | |
21 | - } | |
22 | - else | |
23 | - read(null, function (end, data) { | |
24 | - if(end) cb(end) | |
25 | - else if(aborted) cb(aborted) | |
26 | - else { | |
27 | - busy = true | |
28 | - map(data, function (err, data) { | |
29 | - busy = false | |
30 | - if(aborted) { | |
31 | - cb(aborted) | |
32 | - abortCb(aborted) | |
33 | - } | |
34 | - else if(err) next (err, cb) | |
35 | - else cb(null, data) | |
36 | - }) | |
37 | - } | |
38 | - }) | |
39 | - } | |
40 | - } | |
41 | -} | |
42 | - |
collect.js | ||
---|---|---|
@@ -1,10 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var reduce = require('./reduce') | |
4 | - | |
5 | -module.exports = function collect (cb) { | |
6 | - return reduce(function (arr, item) { | |
7 | - arr.push(item) | |
8 | - return arr | |
9 | - }, [], cb) | |
10 | -} |
concat.js | ||
---|---|---|
@@ -1,9 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var reduce = require('./reduce') | |
4 | - | |
5 | -module.exports = function concat (cb) { | |
6 | - return reduce(function (a, b) { | |
7 | - return a + b | |
8 | - }, '', cb) | |
9 | -} |
count.js | ||
---|---|---|
@@ -1,11 +1,0 @@ | ||
1 | -module.exports = function count (max) { | |
2 | - var i = 0; max = max || Infinity | |
3 | - return function (end, cb) { | |
4 | - if(end) return cb && cb(end) | |
5 | - if(i > max) | |
6 | - return cb(true) | |
7 | - cb(null, i++) | |
8 | - } | |
9 | -} | |
10 | - | |
11 | - |
drain.js | ||
---|---|---|
@@ -1,48 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -module.exports = function drain (op, done) { | |
4 | - var read, abort | |
5 | - | |
6 | - function sink (_read) { | |
7 | - read = _read | |
8 | - if(abort) return sink.abort() | |
9 | - //this function is much simpler to write if you | |
10 | - //just use recursion, but by using a while loop | |
11 | - //we do not blow the stack if the stream happens to be sync. | |
12 | - ;(function next() { | |
13 | - var loop = true, cbed = false | |
14 | - while(loop) { | |
15 | - cbed = false | |
16 | - read(null, function (end, data) { | |
17 | - cbed = true | |
18 | - if(end = end || abort) { | |
19 | - loop = false | |
20 | - if(done) done(end === true ? null : end) | |
21 | - else if(end && end !== true) | |
22 | - throw end | |
23 | - } | |
24 | - else if(op && false === op(data) || abort) { | |
25 | - loop = false | |
26 | - read(abort || true, done || function () {}) | |
27 | - } | |
28 | - else if(!loop){ | |
29 | - next() | |
30 | - } | |
31 | - }) | |
32 | - if(!cbed) { | |
33 | - loop = false | |
34 | - return | |
35 | - } | |
36 | - } | |
37 | - })() | |
38 | - } | |
39 | - | |
40 | - sink.abort = function (err, cb) { | |
41 | - if('function' == typeof err) | |
42 | - cb = err, err = true | |
43 | - abort = err || true | |
44 | - if(read) return read(abort, cb || function () {}) | |
45 | - } | |
46 | - | |
47 | - return sink | |
48 | -} |
sinks/collect.js | ||
---|---|---|
@@ -1,0 +1,10 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var reduce = require('./reduce') | |
4 | + | |
5 | +module.exports = function collect (cb) { | |
6 | + return reduce(function (arr, item) { | |
7 | + arr.push(item) | |
8 | + return arr | |
9 | + }, [], cb) | |
10 | +} |
sinks/concat.js | ||
---|---|---|
@@ -1,0 +1,9 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var reduce = require('./reduce') | |
4 | + | |
5 | +module.exports = function concat (cb) { | |
6 | + return reduce(function (a, b) { | |
7 | + return a + b | |
8 | + }, '', cb) | |
9 | +} |
sinks/drain.js | ||
---|---|---|
@@ -1,0 +1,48 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +module.exports = function drain (op, done) { | |
4 | + var read, abort | |
5 | + | |
6 | + function sink (_read) { | |
7 | + read = _read | |
8 | + if(abort) return sink.abort() | |
9 | + //this function is much simpler to write if you | |
10 | + //just use recursion, but by using a while loop | |
11 | + //we do not blow the stack if the stream happens to be sync. | |
12 | + ;(function next() { | |
13 | + var loop = true, cbed = false | |
14 | + while(loop) { | |
15 | + cbed = false | |
16 | + read(null, function (end, data) { | |
17 | + cbed = true | |
18 | + if(end = end || abort) { | |
19 | + loop = false | |
20 | + if(done) done(end === true ? null : end) | |
21 | + else if(end && end !== true) | |
22 | + throw end | |
23 | + } | |
24 | + else if(op && false === op(data) || abort) { | |
25 | + loop = false | |
26 | + read(abort || true, done || function () {}) | |
27 | + } | |
28 | + else if(!loop){ | |
29 | + next() | |
30 | + } | |
31 | + }) | |
32 | + if(!cbed) { | |
33 | + loop = false | |
34 | + return | |
35 | + } | |
36 | + } | |
37 | + })() | |
38 | + } | |
39 | + | |
40 | + sink.abort = function (err, cb) { | |
41 | + if('function' == typeof err) | |
42 | + cb = err, err = true | |
43 | + abort = err || true | |
44 | + if(read) return read(abort, cb || function () {}) | |
45 | + } | |
46 | + | |
47 | + return sink | |
48 | +} |
sinks/find.js | ||
---|---|---|
@@ -1,0 +1,27 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var id = require('../util/id') | |
4 | +var prop = require('../util/prop') | |
5 | +var drain = require('./drain') | |
6 | + | |
7 | +module.exports = function find (test, cb) { | |
8 | + var ended = false | |
9 | + if(!cb) | |
10 | + cb = test, test = id | |
11 | + else | |
12 | + test = prop(test) || id | |
13 | + | |
14 | + return drain(function (data) { | |
15 | + if(test(data)) { | |
16 | + ended = true | |
17 | + cb(null, data) | |
18 | + return false | |
19 | + } | |
20 | + }, function (err) { | |
21 | + if(ended) return //already called back | |
22 | + cb(err === true ? null : err, null) | |
23 | + }) | |
24 | +} | |
25 | + | |
26 | + | |
27 | + |
sinks/index.js | ||
---|---|---|
@@ -1,0 +1,12 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +module.exports = { | |
4 | + drain: require('./drain'), | |
5 | + onEnd: require('./on-end'), | |
6 | + log: require('./log'), | |
7 | + find: require('./find'), | |
8 | + reduce: require('./reduce'), | |
9 | + collect: require('./collect'), | |
10 | + concat: require('./concat') | |
11 | +} | |
12 | + |
sinks/log.js | ||
---|---|---|
@@ -1,0 +1,9 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var drain = require('./drain') | |
4 | + | |
5 | +module.exports = function log (done) { | |
6 | + return drain(function (data) { | |
7 | + console.log(data) | |
8 | + }, done) | |
9 | +} |
sinks/on-end.js | ||
---|---|---|
@@ -1,0 +1,7 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var drain = require('./drain') | |
4 | + | |
5 | +module.exports = function onEnd (done) { | |
6 | + return drain(null, done) | |
7 | +} |
sinks/reduce.js | ||
---|---|---|
@@ -1,0 +1,12 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var drain = require('./drain') | |
4 | + | |
5 | +module.exports = function reduce (reducer, acc, cb) { | |
6 | + return drain(function (data) { | |
7 | + acc = reducer(acc, data) | |
8 | + }, function (err) { | |
9 | + cb(err, acc) | |
10 | + }) | |
11 | +} | |
12 | + |
empty.js | ||
---|---|---|
@@ -1,6 +1,0 @@ | ||
1 | -//a stream that ends immediately. | |
2 | -module.exports = function empty () { | |
3 | - return function (abort, cb) { | |
4 | - cb(true) | |
5 | - } | |
6 | -} |
sources/count.js | ||
---|---|---|
@@ -1,0 +1,11 @@ | ||
1 | +module.exports = function count (max) { | |
2 | + var i = 0; max = max || Infinity | |
3 | + return function (end, cb) { | |
4 | + if(end) return cb && cb(end) | |
5 | + if(i > max) | |
6 | + return cb(true) | |
7 | + cb(null, i++) | |
8 | + } | |
9 | +} | |
10 | + | |
11 | + |
sources/empty.js | ||
---|---|---|
@@ -1,0 +1,6 @@ | ||
1 | +//a stream that ends immediately. | |
2 | +module.exports = function empty () { | |
3 | + return function (abort, cb) { | |
4 | + cb(true) | |
5 | + } | |
6 | +} |
sources/error.js | ||
---|---|---|
@@ -1,0 +1,7 @@ | ||
1 | +//a stream that errors immediately. | |
2 | +module.exports = function error (err) { | |
3 | + return function (abort, cb) { | |
4 | + cb(err) | |
5 | + } | |
6 | +} | |
7 | + |
sources/index.js | ||
---|---|---|
@@ -1,0 +1,9 @@ | ||
1 | +module.exports = { | |
2 | + keys: require('./keys'), | |
3 | + once: require('./once'), | |
4 | + values: require('./values'), | |
5 | + count: require('./count'), | |
6 | + infinite: require('./infinite'), | |
7 | + empty: require('./empty'), | |
8 | + error: require('./error') | |
9 | +} |
sources/infinite.js | ||
---|---|---|
@@ -1,0 +1,9 @@ | ||
1 | +module.exports = function infinite (generate) { | |
2 | + generate = generate || Math.random | |
3 | + return function (end, cb) { | |
4 | + if(end) return cb && cb(end) | |
5 | + return cb(null, generate()) | |
6 | + } | |
7 | +} | |
8 | + | |
9 | + |
sources/keys.js | ||
---|---|---|
@@ -1,0 +1,6 @@ | ||
1 | +var values = require('./values') | |
2 | +module.exports = function (object) { | |
3 | + return values(Object.keys(object)) | |
4 | +} | |
5 | + | |
6 | + |
sources/once.js | ||
---|---|---|
@@ -1,0 +1,15 @@ | ||
1 | +var abortCb = require('../util/abort-cb') | |
2 | + | |
3 | +module.exports = function once (value, onAbort) { | |
4 | + return function (abort, cb) { | |
5 | + if(abort) | |
6 | + return abortCb(cb, abort, onAbort) | |
7 | + if(value != null) { | |
8 | + var _value = value; value = null | |
9 | + cb(null, _value) | |
10 | + } else | |
11 | + cb(true) | |
12 | + } | |
13 | +} | |
14 | + | |
15 | + |
sources/values.js | ||
---|---|---|
@@ -1,0 +1,20 @@ | ||
1 | +var abortCb = require('../util/abort-cb') | |
2 | + | |
3 | +module.exports = function values (array, onAbort) { | |
4 | + if(!array) | |
5 | + return function (abort, cb) { | |
6 | + if(abort) return abortCb(cb, abort, onAbort) | |
7 | + return cb(true) | |
8 | + } | |
9 | + if(!Array.isArray(array)) | |
10 | + array = Object.keys(array).map(function (k) { | |
11 | + return array[k] | |
12 | + }) | |
13 | + var i = 0 | |
14 | + return function (abort, cb) { | |
15 | + if(abort) | |
16 | + return abortCb(cb, abort, onAbort) | |
17 | + cb(i >= array.length || null, array[i++]) | |
18 | + } | |
19 | +} | |
20 | + |
error.js | ||
---|---|---|
@@ -1,7 +1,0 @@ | ||
1 | -//a stream that errors immediately. | |
2 | -module.exports = function error (err) { | |
3 | - return function (abort, cb) { | |
4 | - cb(err) | |
5 | - } | |
6 | -} | |
7 | - |
filter.js | ||
---|---|---|
@@ -1,24 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var tester = require('./util/tester') | |
4 | - | |
5 | -module.exports = function filter (test) { | |
6 | - //regexp | |
7 | - test = tester(test) | |
8 | - return function (read) { | |
9 | - return function next (end, cb) { | |
10 | - var sync, loop = true | |
11 | - while(loop) { | |
12 | - loop = false | |
13 | - sync = true | |
14 | - read(end, function (end, data) { | |
15 | - if(!end && !test(data)) | |
16 | - return sync ? loop = true : next(end, cb) | |
17 | - cb(end, data) | |
18 | - }) | |
19 | - sync = false | |
20 | - } | |
21 | - } | |
22 | - } | |
23 | -} | |
24 | - |
throughs/async-map.js | ||
---|---|---|
@@ -1,0 +1,42 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var id = require('../util/id') | |
4 | +var prop = require('../util/prop') | |
5 | + | |
6 | +module.exports = function asyncMap (map) { | |
7 | + if(!map) return id | |
8 | + map = prop(map) | |
9 | + var busy = false, abortCb, aborted | |
10 | + return function (read) { | |
11 | + return function next (abort, cb) { | |
12 | + if(aborted) return cb(aborted) | |
13 | + if(abort) { | |
14 | + aborted = abort | |
15 | + if(!busy) read(abort, cb) | |
16 | + else read(abort, function () { | |
17 | + //if we are still busy, wait for the mapper to complete. | |
18 | + if(busy) abortCb = cb | |
19 | + else cb(abort) | |
20 | + }) | |
21 | + } | |
22 | + else | |
23 | + read(null, function (end, data) { | |
24 | + if(end) cb(end) | |
25 | + else if(aborted) cb(aborted) | |
26 | + else { | |
27 | + busy = true | |
28 | + map(data, function (err, data) { | |
29 | + busy = false | |
30 | + if(aborted) { | |
31 | + cb(aborted) | |
32 | + abortCb(aborted) | |
33 | + } | |
34 | + else if(err) next (err, cb) | |
35 | + else cb(null, data) | |
36 | + }) | |
37 | + } | |
38 | + }) | |
39 | + } | |
40 | + } | |
41 | +} | |
42 | + |
throughs/filter-not.js | ||
---|---|---|
@@ -1,0 +1,9 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var tester = require('../util/tester') | |
4 | +var filter = require('./filter') | |
5 | + | |
6 | +module.exports = function filterNot (test) { | |
7 | + test = tester(test) | |
8 | + return filter(function (data) { return !test(data) }) | |
9 | +} |
throughs/filter.js | ||
---|---|---|
@@ -1,0 +1,24 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var tester = require('../util/tester') | |
4 | + | |
5 | +module.exports = function filter (test) { | |
6 | + //regexp | |
7 | + test = tester(test) | |
8 | + return function (read) { | |
9 | + return function next (end, cb) { | |
10 | + var sync, loop = true | |
11 | + while(loop) { | |
12 | + loop = false | |
13 | + sync = true | |
14 | + read(end, function (end, data) { | |
15 | + if(!end && !test(data)) | |
16 | + return sync ? loop = true : next(end, cb) | |
17 | + cb(end, data) | |
18 | + }) | |
19 | + sync = false | |
20 | + } | |
21 | + } | |
22 | + } | |
23 | +} | |
24 | + |
throughs/flatten.js | ||
---|---|---|
@@ -1,0 +1,47 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var values = require('../sources/values') | |
4 | +var once = require('../sources/once') | |
5 | + | |
6 | +//convert a stream of arrays or streams into just a stream. | |
7 | +module.exports = function flatten () { | |
8 | + return function (read) { | |
9 | + var _read | |
10 | + return function (abort, cb) { | |
11 | + if (abort) { //abort the current stream, and then stream of streams. | |
12 | + _read ? _read(abort, function(err) { | |
13 | + read(err || abort, cb) | |
14 | + }) : read(abort, cb) | |
15 | + } | |
16 | + else if(_read) nextChunk() | |
17 | + else nextStream() | |
18 | + | |
19 | + function nextChunk () { | |
20 | + _read(null, function (err, data) { | |
21 | + if (err === true) nextStream() | |
22 | + else if (err) { | |
23 | + read(true, function(abortErr) { | |
24 | + // TODO: what do we do with the abortErr? | |
25 | + cb(err) | |
26 | + }) | |
27 | + } | |
28 | + else cb(null, data) | |
29 | + }) | |
30 | + } | |
31 | + function nextStream () { | |
32 | + _read = null | |
33 | + read(null, function (end, stream) { | |
34 | + if(end) | |
35 | + return cb(end) | |
36 | + if(Array.isArray(stream) || stream && 'object' === typeof stream) | |
37 | + stream = values(stream) | |
38 | + else if('function' != typeof stream) | |
39 | + stream = once(stream) | |
40 | + _read = stream | |
41 | + nextChunk() | |
42 | + }) | |
43 | + } | |
44 | + } | |
45 | + } | |
46 | +} | |
47 | + |
throughs/index.js | ||
---|---|---|
@@ -1,0 +1,16 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +module.exports = { | |
4 | + map: require('./map'), | |
5 | + asyncMap: require('./async-map'), | |
6 | + filter: require('./filter'), | |
7 | + filterNot: require('./filter-not'), | |
8 | + through: require('./through'), | |
9 | + take: require('./take'), | |
10 | + unique: require('./unique'), | |
11 | + nonUnique: require('./non-unique'), | |
12 | + flatten: require('./flatten') | |
13 | +} | |
14 | + | |
15 | + | |
16 | + |
throughs/map.js | ||
---|---|---|
@@ -1,0 +1,22 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var prop = require('../util/prop') | |
4 | + | |
5 | +module.exports = function map (mapper) { | |
6 | + if(!mapper) return id | |
7 | + mapper = prop(mapper) | |
8 | + return function (read) { | |
9 | + return function (abort, cb) { | |
10 | + read(abort, function (end, data) { | |
11 | + try { | |
12 | + data = !end ? mapper(data) : null | |
13 | + } catch (err) { | |
14 | + return read(err, function () { | |
15 | + return cb(err) | |
16 | + }) | |
17 | + } | |
18 | + cb(end, data) | |
19 | + }) | |
20 | + } | |
21 | + } | |
22 | +} |
throughs/non-unique.js | ||
---|---|---|
@@ -1,0 +1,8 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var unique = require('./unique') | |
4 | + | |
5 | +//passes an item through when you see it for the second time. | |
6 | +module.exports = function nonUnique (field) { | |
7 | + return unique(field, true) | |
8 | +} |
throughs/take.js | ||
---|---|---|
@@ -1,0 +1,41 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +//read a number of items and then stop. | |
4 | +module.exports = function take (test, opts) { | |
5 | + opts = opts || {} | |
6 | + var last = opts.last || false // whether the first item for which !test(item) should still pass | |
7 | + var ended = false | |
8 | + if('number' === typeof test) { | |
9 | + last = true | |
10 | + var n = test; test = function () { | |
11 | + return --n | |
12 | + } | |
13 | + } | |
14 | + | |
15 | + return function (read) { | |
16 | + | |
17 | + function terminate (cb) { | |
18 | + read(true, function (err) { | |
19 | + last = false; cb(err || true) | |
20 | + }) | |
21 | + } | |
22 | + | |
23 | + return function (end, cb) { | |
24 | + if(ended) last ? terminate(cb) : cb(ended) | |
25 | + else if(ended = end) read(ended, cb) | |
26 | + else | |
27 | + read(null, function (end, data) { | |
28 | + if(ended = ended || end) { | |
29 | + //last ? terminate(cb) : | |
30 | + cb(ended) | |
31 | + } | |
32 | + else if(!test(data)) { | |
33 | + ended = true | |
34 | + last ? cb(null, data) : terminate(cb) | |
35 | + } | |
36 | + else | |
37 | + cb(null, data) | |
38 | + }) | |
39 | + } | |
40 | + } | |
41 | +} |
throughs/through.js | ||
---|---|---|
@@ -1,0 +1,23 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +//a pass through stream that doesn't change the value. | |
4 | +module.exports = function through (op, onEnd) { | |
5 | + var a = false | |
6 | + | |
7 | + function once (abort) { | |
8 | + if(a || !onEnd) return | |
9 | + a = true | |
10 | + onEnd(abort === true ? null : abort) | |
11 | + } | |
12 | + | |
13 | + return function (read) { | |
14 | + return function (end, cb) { | |
15 | + if(end) once(end) | |
16 | + return read(end, function (end, data) { | |
17 | + if(!end) op && op(data) | |
18 | + else once(end) | |
19 | + cb(end, data) | |
20 | + }) | |
21 | + } | |
22 | + } | |
23 | +} |
throughs/unique.js | ||
---|---|---|
@@ -1,0 +1,17 @@ | ||
1 | +'use strict' | |
2 | + | |
3 | +var id = require('../util/id') | |
4 | +var prop = require('../util/prop') | |
5 | +var filter = require('./filter') | |
6 | + | |
7 | +//drop items you have already seen. | |
8 | +module.exports = function unique (field, invert) { | |
9 | + field = prop(field) || id | |
10 | + var seen = {} | |
11 | + return filter(function (data) { | |
12 | + var key = field(data) | |
13 | + if(seen[key]) return !!invert //false, by default | |
14 | + else seen[key] = true | |
15 | + return !invert //true by default | |
16 | + }) | |
17 | +} |
filterNot.js | ||
---|---|---|
@@ -1,9 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var tester = require('./util/tester') | |
4 | -var filter = require('./filter') | |
5 | - | |
6 | -module.exports = function filterNot (test) { | |
7 | - test = tester(test) | |
8 | - return filter(function (data) { return !test(data) }) | |
9 | -} |
find.js | ||
---|---|---|
@@ -1,25 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var id = require('./util/id') | |
4 | -var prop = require('./util/prop') | |
5 | -var drain = require('./drain') | |
6 | - | |
7 | -module.exports = function find (test, cb) { | |
8 | - var ended = false | |
9 | - if(!cb) | |
10 | - cb = test, test = id | |
11 | - else | |
12 | - test = prop(test) || id | |
13 | - | |
14 | - return drain(function (data) { | |
15 | - if(test(data)) { | |
16 | - ended = true | |
17 | - cb(null, data) | |
18 | - return false | |
19 | - } | |
20 | - }, function (err) { | |
21 | - if(ended) return //already called back | |
22 | - cb(err === true ? null : err, null) | |
23 | - }) | |
24 | -} | |
25 | - |
flatten.js | ||
---|---|---|
@@ -1,47 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var values = require('./values') | |
4 | -var once = require('./once') | |
5 | - | |
6 | -//convert a stream of arrays or streams into just a stream. | |
7 | -module.exports = function flatten () { | |
8 | - return function (read) { | |
9 | - var _read | |
10 | - return function (abort, cb) { | |
11 | - if (abort) { //abort the current stream, and then stream of streams. | |
12 | - _read ? _read(abort, function(err) { | |
13 | - read(err || abort, cb) | |
14 | - }) : read(abort, cb) | |
15 | - } | |
16 | - else if(_read) nextChunk() | |
17 | - else nextStream() | |
18 | - | |
19 | - function nextChunk () { | |
20 | - _read(null, function (err, data) { | |
21 | - if (err === true) nextStream() | |
22 | - else if (err) { | |
23 | - read(true, function(abortErr) { | |
24 | - // TODO: what do we do with the abortErr? | |
25 | - cb(err) | |
26 | - }) | |
27 | - } | |
28 | - else cb(null, data) | |
29 | - }) | |
30 | - } | |
31 | - function nextStream () { | |
32 | - _read = null | |
33 | - read(null, function (end, stream) { | |
34 | - if(end) | |
35 | - return cb(end) | |
36 | - if(Array.isArray(stream) || stream && 'object' === typeof stream) | |
37 | - stream = values(stream) | |
38 | - else if('function' != typeof stream) | |
39 | - stream = once(stream) | |
40 | - _read = stream | |
41 | - nextChunk() | |
42 | - }) | |
43 | - } | |
44 | - } | |
45 | - } | |
46 | -} | |
47 | - |
infinite.js | ||
---|---|---|
@@ -1,9 +1,0 @@ | ||
1 | -module.exports = function infinite (generate) { | |
2 | - generate = generate || Math.random | |
3 | - return function (end, cb) { | |
4 | - if(end) return cb && cb(end) | |
5 | - return cb(null, generate()) | |
6 | - } | |
7 | -} | |
8 | - | |
9 | - |
keys.js |
---|
log.js | ||
---|---|---|
@@ -1,9 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var drain = require('./drain') | |
4 | - | |
5 | -module.exports = function log (done) { | |
6 | - return drain(function (data) { | |
7 | - console.log(data) | |
8 | - }, done) | |
9 | -} |
map.js | ||
---|---|---|
@@ -1,22 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var prop = require('./util/prop') | |
4 | - | |
5 | -module.exports = function map (mapper) { | |
6 | - if(!mapper) return id | |
7 | - mapper = prop(mapper) | |
8 | - return function (read) { | |
9 | - return function (abort, cb) { | |
10 | - read(abort, function (end, data) { | |
11 | - try { | |
12 | - data = !end ? mapper(data) : null | |
13 | - } catch (err) { | |
14 | - return read(err, function () { | |
15 | - return cb(err) | |
16 | - }) | |
17 | - } | |
18 | - cb(end, data) | |
19 | - }) | |
20 | - } | |
21 | - } | |
22 | -} |
nonUnique.js | ||
---|---|---|
@@ -1,8 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var unique = require('./unique') | |
4 | - | |
5 | -//passes an item through when you see it for the second time. | |
6 | -module.exports = function nonUnique (field) { | |
7 | - return unique(field, true) | |
8 | -} |
onEnd.js | ||
---|---|---|
@@ -1,7 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var drain = require('./drain') | |
4 | - | |
5 | -module.exports = function onEnd (done) { | |
6 | - return drain(null, done) | |
7 | -} |
once.js | ||
---|---|---|
@@ -1,15 +1,0 @@ | ||
1 | -var abortCb = require('./util/abortCb') | |
2 | - | |
3 | -module.exports = function once (value, onAbort) { | |
4 | - return function (abort, cb) { | |
5 | - if(abort) | |
6 | - return abortCb(cb, abort, onAbort) | |
7 | - if(value != null) { | |
8 | - var _value = value; value = null | |
9 | - cb(null, _value) | |
10 | - } else | |
11 | - cb(true) | |
12 | - } | |
13 | -} | |
14 | - | |
15 | - |
reduce.js | ||
---|---|---|
@@ -1,12 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var drain = require('./drain') | |
4 | - | |
5 | -module.exports = function reduce (reducer, acc, cb) { | |
6 | - return drain(function (data) { | |
7 | - acc = reducer(acc, data) | |
8 | - }, function (err) { | |
9 | - cb(err, acc) | |
10 | - }) | |
11 | -} | |
12 | - |
sinks.js | ||
---|---|---|
@@ -1,12 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -module.exports = { | |
4 | - drain: require('./drain'), | |
5 | - onEnd: require('./onEnd'), | |
6 | - log: require('./log'), | |
7 | - find: require('./find'), | |
8 | - reduce: require('./reduce'), | |
9 | - collect: require('./collect'), | |
10 | - concat: require('./concat') | |
11 | -} | |
12 | - |
sources.js | ||
---|---|---|
@@ -1,9 +1,0 @@ | ||
1 | -module.exports = { | |
2 | - keys: require('./keys'), | |
3 | - once: require('./once'), | |
4 | - values: require('./values'), | |
5 | - count: require('./count'), | |
6 | - infinite: require('./infinite'), | |
7 | - empty: require('./empty'), | |
8 | - error: require('./error') | |
9 | -} |
take.js | ||
---|---|---|
@@ -1,41 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -//read a number of items and then stop. | |
4 | -module.exports = function take (test, opts) { | |
5 | - opts = opts || {} | |
6 | - var last = opts.last || false // whether the first item for which !test(item) should still pass | |
7 | - var ended = false | |
8 | - if('number' === typeof test) { | |
9 | - last = true | |
10 | - var n = test; test = function () { | |
11 | - return --n | |
12 | - } | |
13 | - } | |
14 | - | |
15 | - return function (read) { | |
16 | - | |
17 | - function terminate (cb) { | |
18 | - read(true, function (err) { | |
19 | - last = false; cb(err || true) | |
20 | - }) | |
21 | - } | |
22 | - | |
23 | - return function (end, cb) { | |
24 | - if(ended) last ? terminate(cb) : cb(ended) | |
25 | - else if(ended = end) read(ended, cb) | |
26 | - else | |
27 | - read(null, function (end, data) { | |
28 | - if(ended = ended || end) { | |
29 | - //last ? terminate(cb) : | |
30 | - cb(ended) | |
31 | - } | |
32 | - else if(!test(data)) { | |
33 | - ended = true | |
34 | - last ? cb(null, data) : terminate(cb) | |
35 | - } | |
36 | - else | |
37 | - cb(null, data) | |
38 | - }) | |
39 | - } | |
40 | - } | |
41 | -} |
through.js | ||
---|---|---|
@@ -1,23 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -//a pass through stream that doesn't change the value. | |
4 | -module.exports = function through (op, onEnd) { | |
5 | - var a = false | |
6 | - | |
7 | - function once (abort) { | |
8 | - if(a || !onEnd) return | |
9 | - a = true | |
10 | - onEnd(abort === true ? null : abort) | |
11 | - } | |
12 | - | |
13 | - return function (read) { | |
14 | - return function (end, cb) { | |
15 | - if(end) once(end) | |
16 | - return read(end, function (end, data) { | |
17 | - if(!end) op && op(data) | |
18 | - else once(end) | |
19 | - cb(end, data) | |
20 | - }) | |
21 | - } | |
22 | - } | |
23 | -} |
throughs.js | ||
---|---|---|
@@ -1,19 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var id = require('./util/id') | |
4 | -var prop = require('./util/prop') | |
5 | - | |
6 | -var values = require('./values') | |
7 | -var once = require('./once') | |
8 | - | |
9 | -module.exports = { | |
10 | - map: require('./map'), | |
11 | - asyncMap: require('./asyncMap'), | |
12 | - filter: require('./filter'), | |
13 | - filterNot: require('./filterNot'), | |
14 | - through: require('./through'), | |
15 | - take: require('./take'), | |
16 | - unique: require('./unique'), | |
17 | - nonUnique: require('./nonUnique'), | |
18 | - flatten: require('./flatten') | |
19 | -} |
unique.js | ||
---|---|---|
@@ -1,17 +1,0 @@ | ||
1 | -'use strict' | |
2 | - | |
3 | -var id = require('./util/id') | |
4 | -var prop = require('./util/prop') | |
5 | -var filter = require('./filter') | |
6 | - | |
7 | -//drop items you have already seen. | |
8 | -module.exports = function unique (field, invert) { | |
9 | - field = prop(field) || id | |
10 | - var seen = {} | |
11 | - return filter(function (data) { | |
12 | - var key = field(data) | |
13 | - if(seen[key]) return !!invert //false, by default | |
14 | - else seen[key] = true | |
15 | - return !invert //true by default | |
16 | - }) | |
17 | -} |
values.js | ||
---|---|---|
@@ -1,20 +1,0 @@ | ||
1 | -var abortCb = require('./util/abortCb') | |
2 | - | |
3 | -module.exports = function values (array, onAbort) { | |
4 | - if(!array) | |
5 | - return function (abort, cb) { | |
6 | - if(abort) return abortCb(cb, abort, onAbort) | |
7 | - return cb(true) | |
8 | - } | |
9 | - if(!Array.isArray(array)) | |
10 | - array = Object.keys(array).map(function (k) { | |
11 | - return array[k] | |
12 | - }) | |
13 | - var i = 0 | |
14 | - return function (abort, cb) { | |
15 | - if(abort) | |
16 | - return abortCb(cb, abort, onAbort) | |
17 | - cb(i >= array.length || null, array[i++]) | |
18 | - } | |
19 | -} | |
20 | - |
Built with git-ssb-web