Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50659 views
1
2
var es = require('event-stream')
3
, it = require('it-is')
4
5
function writeArray(array, stream) {
6
7
array.forEach( function (j) {
8
stream.write(j)
9
})
10
stream.end()
11
12
}
13
14
function readStream(stream, done) {
15
16
var array = []
17
stream.on('data', function (data) {
18
array.push(data)
19
})
20
stream.on('error', done)
21
stream.on('end', function (data) {
22
done(null, array)
23
})
24
25
}
26
27
exports ['simple map applied to a stream'] = function (test) {
28
29
var input = [1,2,3,7,5,3,1,9,0,2,4,6]
30
//create event stream from
31
32
var doubler = es.map(function (data, cb) {
33
cb(null, data * 2)
34
})
35
36
readStream(doubler, function (err, output) {
37
it(output).deepEqual(input.map(function (j) {
38
return j * 2
39
}))
40
test.done()
41
})
42
43
writeArray(input, doubler)
44
45
}
46
47
exports['pipe two maps together'] = function (test) {
48
49
var input = [1,2,3,7,5,3,1,9,0,2,4,6]
50
//create event stream from
51
function dd (data, cb) {
52
cb(null, data * 2)
53
}
54
var doubler1 = es.map(dd), doubler2 = es.map(dd)
55
56
doubler1.pipe(doubler2)
57
58
readStream(doubler2, function (err, output) {
59
it(output).deepEqual(input.map(function (j) {
60
return j * 4
61
}))
62
test.done()
63
})
64
65
writeArray(input, doubler1)
66
67
}
68
69
//next:
70
//
71
// test pause, resume and drian.
72
//
73
74
// then make a pipe joiner:
75
//
76
// plumber (evStr1, evStr2, evStr3, evStr4, evStr5)
77
//
78
// will return a single stream that write goes to the first
79
80
exports ['map will not call end until the callback'] = function (test) {
81
82
var ticker = es.map(function (data, cb) {
83
process.nextTick(function () {
84
cb(null, data * 2)
85
})
86
})
87
ticker.write('x')
88
89
ticker.end()
90
ticker.end()
91
ticker.end()
92
93
ticker.on('end', function () {
94
test.done()
95
})
96
}
97