Skip to content

Commit

Permalink
Fix parsing when quote is escaped inside quoted column containing del…
Browse files Browse the repository at this point in the history
…imiter (#318) (#323)
  • Loading branch information
risseraka authored and Keyang committed Jun 20, 2019
1 parent f4136ca commit 98fe0f3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/rowSplit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,15 @@ export class RowSplit {
continue;
} else if (e.indexOf(quote) !== -1) {
let count = 0;
let prev = "";
for (const c of e) {
if (c === quote) {
// count quotes only if previous character is not escape char
if (c === quote && prev !== this.escape) {
count++;
prev = "";
} else {
// save previous char to temp variable
prev = c;
}
}
if (count % 2 === 1) {
Expand Down
2 changes: 2 additions & 0 deletions test/data/dataWithSlashEscapeAndDelimiterBetweenQuotes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,raw
0,"\"hello,\"world\""
17 changes: 16 additions & 1 deletion test/testCSVConverter2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,22 @@ describe("testCSVConverter2", function () {
rs.pipe(test_converter);
});

it("should output ndjson format", function (done) {
it("should process escape chars when delimiter is between escaped quotes", function(done) {
var test_converter = new Converter({
escape: "\\"
});

var testData =
__dirname + "/data/dataWithSlashEscapeAndDelimiterBetweenQuotes";
var rs = fs.createReadStream(testData);
test_converter.then(function(res) {
assert.equal(res[0].raw, '"hello,"world"');
done();
});
rs.pipe(test_converter);
});

it("should output ndjson format", function(done) {
var conv = new Converter();
conv.fromString("a,b,c\n1,2,3\n4,5,6")
.on("data", function (d) {
Expand Down

0 comments on commit 98fe0f3

Please sign in to comment.