Skip to content

Commit 007fd9c

Browse files
committed
fixed a bug with line break; refactored line population
1 parent b6c2312 commit 007fd9c

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

libs/core/csvConverter.js

+28-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function csvAdv(params) {
3434
this.resultObject.disableConstruct();
3535
}
3636
this.headRow = [];
37-
this._buffer = "";
37+
this._buffer = ""; //line buffer
38+
this._recordBuffer=""; //record buffer
3839
this.rowIndex = 0;
3940
this._isStarted = false;
4041
var self = this;
@@ -58,6 +59,21 @@ csvAdv.prototype._isToogleQuote = function(segment) {
5859
return false;
5960
}
6061
}
62+
//on line poped
63+
csvAdv.prototype._line=function(line,lastLine){
64+
this._recordBuffer+=line;
65+
if (!this._isToogleQuote(this._recordBuffer)){ //if a complete record is in buffer. start the parse
66+
var data=this._recordBuffer;
67+
this._recordBuffer="";
68+
this._record(data,this.rowIndex++,lastLine) ;
69+
}else{ //if the record in buffer is not a complete record (quote does not match). wait next line
70+
this._recordBuffer+=this.eol;
71+
if (lastLine){
72+
throw ("Incomplete CSV file detected. Quotes does not match in pairs.");
73+
}
74+
return;
75+
}
76+
}
6177
csvAdv.prototype._transform = function(data, encoding, cb) {
6278
var self = this;
6379
if (encoding == "buffer") {
@@ -79,23 +95,26 @@ csvAdv.prototype._transform = function(data, encoding, cb) {
7995
}
8096
if (this.eol) {
8197
//console.log(this._buffer);
82-
if (this._buffer.indexOf(this.eol) > -1) {
98+
if (this._buffer.indexOf(this.eol) > -1) { //if current data contains 1..* line break
8399
var arr = this._buffer.split(this.eol);
84100
while (arr.length > 1) {
85101
var data = arr.shift();
86-
if (data.length > 0) {
87-
this.emit("record", data, this.rowIndex++);
88-
}
102+
this._line(data);
103+
//this.emit("record", data, this.rowIndex++);
89104
}
90-
this._buffer = arr[0];
105+
this._buffer = arr[0]; //whats left (maybe half line). push to buffer
106+
cb();
107+
}else{ //if there is no line break appeared. wait next loop until it appears.
108+
cb();
91109
}
92110

111+
}else{
112+
cb();
93113
}
94-
cb();
95114
};
96115
csvAdv.prototype._flush = function(cb) {
97-
if (this._buffer.length != 0) { //emit last line
98-
this.emit("record", this._buffer, this.rowIndex++, true);
116+
if (this._buffer.length != 0) { //finished but still has buffer data. emit last line
117+
this._line(this._buffer, true);
99118
}
100119
cb();
101120
};

libs/core/init_onrecord.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
var os = require("os");
55
module.exports = function() {
66
var self = this;
7-
self.on("record", function(rowStr, index, lastLine) {
7+
this._record=function(rowStr, index, lastLine) {
88
var quote = self.param.quote;
99
var delimiter = self.param.delimiter;
1010
var rowArr = rowStr.split(delimiter);
@@ -49,5 +49,5 @@ module.exports = function() {
4949
self.emit("record_parsed", resultRow, row, index - 1);
5050
self.push(JSON.stringify(resultRow),"utf8");
5151
}
52-
});
52+
};
5353
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"name": "Keyang Xiang",
1313
"email": "[email protected]"
1414
}],
15-
"version": "0.3.16",
15+
"version": "0.3.17",
1616
"keywords": [
1717
"csv",
1818
"json",

test/data/lineBreak

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
F_Year,Event ID,Event Date,Time,Location,Rotue,TOC,Person type,Injury degree,FWI,Precursor,Gender,Under 16,Apparent age,Fatal,Alcohol/Drugs,Impairment,Encumbrances & Group Travel,Risk-taking Behaviour,Sub-standard conditions,Design,Operational error,Crowd management,Non-standard operation,Rushing,3rd Party Behaviour,Narrative Full,Period
2+
2013/2014,2914930,3/2/14,23:02:00,Pembrey and Burry Port,Wales,Arriva Trains Wales,Passenger,Minor non-reportable,0.001,Passenger falls from platform onto track (no electric shock nor struck by train) under the influence,Male,,Unknown,,x,,,,,,,,,,,"A report was initially received from Dyfed Powys Police that a person had been struck by a train at Pembrey and Burry Port station and that an ambulance was in attendance (the ambulance was the source of the police report). It later transpired that the person was inebriated and had fallen off of Pembrey platform after attempting to board the train after the doors were closed and the train was ready to depart. It has been confirmed (by Pembrey signaller) that the person has not been struck by the train and has NOT fallen under the train. One police officer on site.
3+
4+
Later advice from the train crew working 2B21 2135 Milford Haven to Cardiff Central stated the person had attempted to board 2B21 as it was ready to depart from the platform, bounced off of the train falling backwards, had then gotten back up and then fallen off of the platform and onto the track behind the train. The persons injuries (if any) have not been disclosed.
5+
6+
The person was been removed from the track and normal working resumed from 2326. The on call MOM was called to attend and act a RIO if required, but was stood down after the incident was resolved.
7+
8+
The initial call came from Dyfed Powys Police under reference 333 when it was advised that their report had come from the Ambulance Service (reference 1523673) and the BTP were also made aware by Dyfed Powys Police under their reference 470.",13

test/testCSVConverter.js

+12
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,16 @@ describe("CSV Converter", function() {
240240
});
241241
rs.pipe(csvConverter);
242242
});
243+
it ("should process column with linebreaks",function(done){
244+
var testData=__dirname+"/data/lineBreak";
245+
var rs=fs.createReadStream(testData);
246+
var csvConverter=new CSVAdv({
247+
constructResult:false
248+
});
249+
csvConverter.on("record_parsed",function(d){
250+
assert(d.Period===13);
251+
done();
252+
});
253+
rs.pipe(csvConverter);
254+
});
243255
});

0 commit comments

Comments
 (0)