Skip to content

Commit 0267ad8

Browse files
committed
Merge pull request #31 from doug-martin/master
v0.2.4
2 parents ddec4b2 + 9ea316d commit 0267ad8

File tree

7 files changed

+80
-21
lines changed

7 files changed

+80
-21
lines changed

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
#0.2.4
2+
3+
* Added more fine grained control to `.pause` and `.resume`
4+
* You can now pause resume between chunks
5+
16
# 0.2.3
27

38
* Add new `createWriteStream` for creating a streaming csv writer

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
DOC_COMMAND=coddoc -f multi-html -d ./lib --dir ./docs
2-
MD_COMMAND=coddoc -f markdown -d ./lib > README.md
32

43
test:
54
export NODE_PATH=$NODE_PATH:lib && ./node_modules/it/bin/it -r dotmatrix
65

76
docs: docclean
8-
$(DOC_COMMAND) && $(MD_COMMAND)
7+
$(DOC_COMMAND)
98

109
docclean :
1110
rm -rf docs/*

benchmark/benchmark.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function camelize(str) {
1313

1414
function benchmarkFastCsv(done) {
1515
var count = 0;
16-
fastCsv
16+
var stream = fastCsv
1717
.fromPath(TEST_FILE, {headers: true})
1818
.transform(function (data) {
1919
var ret = {};
@@ -23,12 +23,12 @@ function benchmarkFastCsv(done) {
2323
ret.address = data.address;
2424
return ret;
2525
})
26-
.on("record", function () {
27-
count++;
26+
.on("record", function (data, i) {
27+
count = i + 1;
2828
})
2929
.on("end", function () {
3030
if (count !== COUNT) {
31-
done(new Error("Error expected %d got %d", COUNT, count));
31+
done(new Error("Error expected " + COUNT + " got " + count));
3232
} else {
3333
done();
3434
}
@@ -51,7 +51,7 @@ function benchmarkCsv(done) {
5151
ret.address = data[3];
5252
return ret;
5353
})
54-
.on('record', function () {
54+
.on('record', function (data) {
5555
count++;
5656
})
5757
.on('end', function () {

docs/History.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@
176176

177177

178178

179+
<h1>0.2.4</h1>
180+
<ul>
181+
<li>Added more fine grained control to <code>.pause</code> and <code>.resume</code><ul>
182+
<li>You can now pause resume between chunks</li>
183+
</ul>
184+
</li>
185+
</ul>
179186
<h1>0.2.3</h1>
180187
<ul>
181188
<li>Add new <code>createWriteStream</code> for creating a streaming csv writer</li>

lib/parser_stream.js

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function ParserStream(options) {
1414
stream.Transform.call(this, options);
1515
this.lines = "";
1616
this._parsedHeaders = false;
17-
this._rowCount = 0;
17+
this._rowCount = -1;
1818
this._emitData = false;
1919
options = options || {};
2020
var delimiter;
@@ -31,6 +31,7 @@ function ParserStream(options) {
3131
this.parser = createParser(options);
3232
this._headers = options.headers;
3333
this._ignoreEmpty = options.ignoreEmpty;
34+
this.__buffered = [];
3435
return this;
3536
}
3637

@@ -61,7 +62,7 @@ extended(ParserStream).extend({
6162

6263
__handleLine: function __parseLineData(line, index, ignore) {
6364
var ignoreEmpty = this._ignoreEmpty;
64-
if (extended.isBoolean(ignoreEmpty) && ignoreEmpty && (!line || EMPTY.test(line.join("")))) {
65+
if (extended.isBoolean(ignoreEmpty) && ignoreEmpty && (!line || EMPTY.test(line.join("")))) {
6566
return null;
6667
}
6768
if (!ignore) {
@@ -77,9 +78,7 @@ extended(ParserStream).extend({
7778
},
7879

7980
_parse: function _parseLine(data, hasMoreData) {
80-
var row,
81-
emitData = this._emitData,
82-
count = 0, ret, rows, self = this;
81+
var row, count, ret, rows, self = this;
8382
try {
8483
data = this.parser(data, hasMoreData);
8584
ret = data.line;
@@ -109,12 +108,15 @@ extended(ParserStream).extend({
109108
for (var i = 0, l = rows.length; i < l; i++) {
110109
row = rows[i];
111110
if (row) {
112-
var dataRow = this.__handleLine(row, count);
111+
var dataRow = this.__handleLine(row, (count = ++this._rowCount));
113112
if (dataRow) {
114-
this.emit("record", dataRow, (count = this._rowCount++));
115-
if (emitData) {
116-
this.push(JSON.stringify(dataRow));
113+
if (!this.paused) {
114+
this.__emitRecord(dataRow, count);
115+
} else {
116+
this.__buffered.push([dataRow, count]);
117117
}
118+
} else {
119+
count = --this._rowCount;
118120
}
119121
}
120122
}
@@ -124,6 +126,13 @@ extended(ParserStream).extend({
124126
return ret;
125127
},
126128

129+
__emitRecord: function (dataRow, count) {
130+
this.emit("record", dataRow, count);
131+
if (this._emitData) {
132+
this.push(JSON.stringify(dataRow));
133+
}
134+
},
135+
127136
_transform: function (data, encoding, done) {
128137
var lines = this.lines;
129138
var lineData = (lines + data);
@@ -142,7 +151,8 @@ extended(ParserStream).extend({
142151
if (this.lines) {
143152
this._parse(this.lines, false);
144153
}
145-
this.emit("end", this._rowCount);
154+
//increment row count so we aren't 0 based
155+
this.emit("end", ++this._rowCount);
146156
callback();
147157
},
148158

@@ -163,6 +173,19 @@ extended(ParserStream).extend({
163173
resume: function () {
164174
if (this.paused) {
165175
this.paused = false;
176+
var buffered = this.__buffered, l = buffered.length;
177+
if (l) {
178+
var i = -1, entry;
179+
while (++i < buffered.length) {
180+
entry = buffered.shift();
181+
this.__emitRecord(entry[0], entry[1]);
182+
//handle case where paused is called while emitting data
183+
if (this.paused) {
184+
return;
185+
}
186+
}
187+
buffered.length = 0;
188+
}
166189
if (this.__pausedDone) {
167190
var done = this.__pausedDone;
168191
this.__pausedDone = null;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fast-csv",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "CSV parser and writer",
55
"main": "index.js",
66
"scripts": {

test/fast-csv.test.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,33 @@ it.describe("fast-csv", function (it) {
535535
});
536536
});
537537

538+
it.describe("pause/resume", function () {
539+
540+
it.should("support pausing a stream", function (next) {
541+
var actual = [], paused = false;
542+
var stream = csv
543+
.fromPath(path.resolve(__dirname, "./assets/test4.csv"), {headers: true})
544+
.on("record", function (data, index) {
545+
assert.isFalse(paused);
546+
actual[index] = data;
547+
paused = true;
548+
stream.pause();
549+
setTimeout(function () {
550+
assert.isTrue(paused);
551+
paused = false;
552+
stream.resume();
553+
}, 100);
554+
})
555+
.on("error", next)
556+
.on("end", function (count) {
557+
assert.deepEqual(actual, expected4);
558+
assert.equal(count, actual.length);
559+
next();
560+
});
561+
});
562+
563+
});
564+
538565

539566
it.should("throw an error if an invalid path or stream is passed in", function () {
540567
assert.throws(function () {
@@ -708,6 +735,4 @@ it.describe("fast-csv", function (it) {
708735
stream.write(null);
709736
});
710737
});
711-
});
712-
713-
it.run().both(process.exit);
738+
});

0 commit comments

Comments
 (0)