Skip to content

Commit bc4a389

Browse files
committed
fix bugs with quotes
1 parent ede0cb0 commit bc4a389

6 files changed

Lines changed: 31 additions & 12 deletions

File tree

dist/csvtojson.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ function rowSplit(rowStr, param) {
10591059
len-=1;
10601060
if (e[0]!==quote){ // open quote
10611061
if (e[len-1] === quote){ // possible close
1062-
if (e[len-2]!==quote){ // close quote
1062+
if (e[len-2]!==quote || e[len-2]===quote && e[len-3] === quote){ // close quote
10631063
e=e.substring(0,len-1);
10641064
e=twoDoubleQuote(e,quote);
10651065
row.push(e);
@@ -1081,11 +1081,11 @@ function rowSplit(rowStr, param) {
10811081
continue;
10821082
}
10831083
}else{ //previous quote not closed
1084-
if (e[len-1] === quote && e[len-2] !==quote){ //close double quote
1084+
if (e[len-1] === quote && (e[len-2] !==quote || e[len-2] === quote && e[len-3] === quote)){ //close double quote
10851085
inquote=false;
10861086
e=e.substr(0,len-1);
10871087
quoteBuff+=delimiter+e;
1088-
quoteBuf=twoDoubleQuote(quoteBuff,quote);
1088+
quoteBuff=twoDoubleQuote(quoteBuff,quote);
10891089
if (trim){
10901090
quoteBuff=quoteBuff.trimRight();
10911091
}
@@ -10144,7 +10144,7 @@ module.exports={
1014410144
"email": "t3dodson@gmail.com"
1014510145
}
1014610146
],
10147-
"version": "0.5.8",
10147+
"version": "0.5.9",
1014810148
"keywords": [
1014910149
"csv",
1015010150
"csvtojson",

dist/csvtojson.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/core/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function rowSplit(rowStr, param) {
5252
len-=1;
5353
if (e[0]!==quote){ // open quote
5454
if (e[len-1] === quote){ // possible close
55-
if (e[len-2]!==quote){ // close quote
55+
if (e[len-2]!==quote || e[len-2]===quote && e[len-3] === quote){ // close quote
5656
e=e.substring(0,len-1);
5757
e=twoDoubleQuote(e,quote);
5858
row.push(e);
@@ -74,11 +74,11 @@ function rowSplit(rowStr, param) {
7474
continue;
7575
}
7676
}else{ //previous quote not closed
77-
if (e[len-1] === quote && e[len-2] !==quote){ //close double quote
77+
if (e[len-1] === quote && (e[len-2] !==quote || e[len-2] === quote && e[len-3] === quote)){ //close double quote
7878
inquote=false;
7979
e=e.substr(0,len-1);
8080
quoteBuff+=delimiter+e;
81-
quoteBuf=twoDoubleQuote(quoteBuff,quote);
81+
quoteBuff=twoDoubleQuote(quoteBuff,quote);
8282
if (trim){
8383
quoteBuff=quoteBuff.trimRight();
8484
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"email": "t3dodson@gmail.com"
1919
}
2020
],
21-
"version": "0.5.8",
21+
"version": "0.5.9",
2222
"keywords": [
2323
"csv",
2424
"csvtojson",

test/data/dataWithTripleQoutes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Year,Make,Model,Description,Price
2+
1997,Ford,E350,"ac, abs, moon",3000.00
3+
1999,Chevy,"Venture ""Extended Edition""","",4900.00
4+
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
5+
1996,Jeep,Grand Cherokee,"MUST SELL!air, moon roof, loaded",4799.00

test/testCSVConverter2.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,21 @@ describe("CSV Converter", function() {
152152
});
153153
rs.pipe(conv);
154154

155-
})
155+
});
156+
it ("should convert triple quotes correctly",function(done){
157+
var testData = __dirname + "/data/dataWithTripleQoutes";
158+
var rs = fs.createReadStream(testData);
159+
var conv=new Converter({trim:true});
160+
conv.on("end_parsed",function(res){
161+
console.log(res);
162+
assert.equal(res[0].Description,"ac, abs, moon");
163+
assert.equal(res[1].Model,"Venture \"Extended Edition\"");
164+
assert.equal(res[2].Model,"Venture \"Extended Edition, Very Large\"");
165+
done();
166+
});
167+
rs.pipe(conv);
168+
169+
});
156170
// it ("should convert big csv",function(done){
157171
// // var rs=fs.createReadStream(__dirname+"/data/large-csv-sample.csv");
158172
// var rs=fs.createReadStream("/Users/kxiang/tmp/csvdata");

0 commit comments

Comments
 (0)