This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (119 loc) · 3.59 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Generated by CoffeeScript 1.3.3
(function() {
var LazyLines;
LazyLines = (function() {
var nCode, onChunkReceived, onLineEndFound, onStreamEnd, rCode, _ref;
_ref = ['\r'.charCodeAt(0), '\n'.charCodeAt(0)], rCode = _ref[0], nCode = _ref[1];
function LazyLines(stream) {
var _this = this;
this.stream = stream;
if (!(this instanceof LazyLines)) {
return new LazyLines(this.stream);
}
this.bufferedChunks = [];
this.bufferedLength = 0;
this.actions = [];
this.encoding = 'utf-8';
this.stream.on('data', function() {
return onChunkReceived.apply(_this, arguments);
});
this.stream.on('end', function() {
return onStreamEnd.apply(_this, arguments);
});
}
onChunkReceived = function(chunk) {
var chr, firstUnhandledIndex, i, _i, _len;
firstUnhandledIndex = 0;
for (i = _i = 0, _len = chunk.length; _i < _len; i = ++_i) {
chr = chunk[i];
if (chr === rCode || chr === nCode) {
this.bufferedChunks.push(chunk.slice(firstUnhandledIndex, i));
this.bufferedLength += i - firstUnhandledIndex;
onLineEndFound.call(this);
if (chr === rCode && chunk[i + 1] === nCode) {
i += 1;
}
firstUnhandledIndex = i + 1;
}
}
if (firstUnhandledIndex !== chunk.length) {
this.bufferedChunks.push(chunk.slice(firstUnhandledIndex));
return this.bufferedLength += chunk.length - firstUnhandledIndex;
}
};
onStreamEnd = function() {
if (this.bufferedLength > 0) {
return onLineEndFound.call(this);
}
};
onLineEndFound = function() {
var action, bufferedChunk, line, targetLength, _i, _j, _len, _len1, _ref1, _ref2;
if (this.encoding != null) {
line = ((function() {
var _i, _len, _ref1, _results;
_ref1 = this.bufferedChunks;
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
bufferedChunk = _ref1[_i];
_results.push(bufferedChunk.toString(this.encoding));
}
return _results;
}).call(this)).join('');
} else {
line = new Buffer(this.bufferedLength);
targetLength = 0;
_ref1 = this.bufferedChunks;
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
bufferedChunk = _ref1[_i];
bufferedChunk.copy(line, targetLength);
targetLength += bufferedChunk.length;
}
}
this.bufferedChunks = [];
this.bufferedLength = 0;
_ref2 = this.actions;
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
action = _ref2[_j];
switch (action.type) {
case 'forEach':
action.f(line);
break;
case 'map':
line = action.f(line);
break;
case 'filter':
if (action.f(line) === false) {
return;
}
}
}
};
LazyLines.prototype.setEncoding = function(encoding) {
this.encoding = encoding;
return this;
};
LazyLines.prototype.forEach = function(f) {
this.actions.push({
type: 'forEach',
f: f
});
return this;
};
LazyLines.prototype.map = function(f) {
this.actions.push({
type: 'map',
f: f
});
return this;
};
LazyLines.prototype.filter = function(f) {
this.actions.push({
type: 'filter',
f: f
});
return this;
};
return LazyLines;
})();
module.exports = LazyLines;
}).call(this);