/
index.js
57 lines (50 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*!
* iterator-streams <https://github.com/doowb/iterator-streams>
*
* Copyright (c) 2015, Brian Woodward.
* Licensed under the MIT License.
*/
'use strict';
/**
* Iterate over a stack of streams piping the results of
* each steram to the next stream in the stack.
*
* @param {Array} `stack` Array of streams to use.
* @return {Function} Returns a function that will iterator over the given stack of streams.
* @api public
* @name iterator
*/
var es = require('event-stream');
var through = require('through2');
module.exports = function iteratorStream(stack) {
stack = stack || [];
stack = Array.isArray(stack) ? stack : [stack];
if (!stack.length) {
stack.push(identity);
}
return function (/* arguments */) {
var self = this;
var args = [].slice.call(arguments);
var pipeline = [];
var len = stack.length, i = 0;
while (len--) {
var loader = stack[i++];
if (typeof loader === 'function') {
loader = loader.apply(self, args);
}
pipeline.push(loader);
}
return es.pipe.apply(es, pipeline);
};
};
function identity() {
var args = [].slice.call(arguments);
var stream = through.obj();
var pass = through.obj();
stream.pipe(pass);
process.nextTick(function () {
stream.write(args[0]);
stream.end();
});
return stream;
}