Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80540 views
1
var pipeline = require('../');
2
var through = require('through2');
3
var split = require('split');
4
var concat = require('concat-stream');
5
var test = require('tape');
6
7
test('splice', function (t) {
8
var expected = {};
9
expected.replacer = [ '333', '444', '5000', '6000' ];
10
expected.d = [ 3, 4 ];
11
expected.thousander = [ 5, 6 ];
12
13
t.plan(4 + 2 + 2 + 1);
14
15
var a = split();
16
var b = through.obj(function (row, enc, next) {
17
this.push(JSON.parse(row));
18
next();
19
});
20
var c = through.obj(function (row, enc, next) {
21
this.push(row.x);
22
next();
23
});
24
var d = through.obj(function (x, enc, next) {
25
t.equal(x, expected.d.shift(), 'd');
26
this.push(String(x * 111));
27
next();
28
});
29
var thousander = through.obj(function (x, enc, next) {
30
t.equal(x, expected.thousander.shift(), 'thousander');
31
this.push(String(x * 1000));
32
next();
33
});
34
35
var replacer = through(function (buf, enc, next) {
36
var ex = expected.replacer.shift();
37
t.equal(buf.toString(), ex);
38
if (expected.replacer.length === 2) {
39
stream.splice(3, 1, thousander);
40
}
41
this.push(buf.toString('hex') + '\n');
42
next();
43
});
44
45
var stream = pipeline([ a, b, c, d, replacer ]);
46
stream.pipe(concat(function (body) {
47
t.deepEqual(
48
body.toString(),
49
'333333\n343434\n35303030\n36303030\n'
50
);
51
}));
52
53
stream.write('{"x":3}\n');
54
stream.write('{"x":4}\n');
55
stream.write('{"x":5}\n');
56
stream.write('{"x":6}');
57
stream.end();
58
});
59
60