lines.js
2.24 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var assert = require('assert');
var Lazy = require('..');
var EventEmitter = require('events').EventEmitter;
exports['buffered lines'] = function () {
var lazy = Lazy();
var joined = false;
lazy.lines
.forEach(function (line) {
assert.ok(line instanceof Buffer);
assert.ok(!line.toString().match(/\n/));
assert.ok(line.length > 0);
})
.join(function (lines) {
assert.deepEqual(
lines.map(function (x) { return x.toString() }),
'foo bar baz quux moo'.split(' ')
);
joined = true;
});
;
setTimeout(function () {
lazy.push(new Buffer('foo\nbar'));
lazy.push(new Buffer('\nbaz\nquux\nmoo'));
lazy.push(new Buffer(''));
lazy.push(new Buffer('\ndoom'));
lazy.end();
assert.ok(joined);
}, 50);
};
exports['string lines'] = function () {
var lazy = Lazy();
var joined = false;
lazy.lines
.forEach(function (line) {
assert.ok(line instanceof Buffer);
assert.ok(!line.toString().match(/\n/));
assert.ok(line.length > 0);
})
.join(function (lines) {
assert.deepEqual(
lines.map(function (x) { return x.toString() }),
'foo bar baz quux moo'.split(' ')
);
joined = true;
});
;
setTimeout(function () {
lazy.push('foo\nbar');
lazy.push('\nbaz\nquux\nmoo');
lazy.push('');
lazy.push('\ndoom');
lazy.end();
assert.ok(joined);
}, 50);
};
exports.endStream = function () {
var to = setTimeout(function () {
assert.fail('never finished');
}, 2500);
var em = new EventEmitter;
var i = 0;
var lines = [];
Lazy(em).lines.forEach(function (line) {
i ++;
lines.push(line);
if (i == 2) {
clearTimeout(to);
assert.eql(lines.map(String), [ 'foo', 'bar' ]);
}
});
setTimeout(function () {
em.emit('data', 'fo');
}, 100);
setTimeout(function () {
em.emit('data', 'o\nbar');
}, 150);
setTimeout(function () {
em.emit('end');
}, 200);
};