Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where closing quote was not removed #468

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/rowSplit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,37 @@ describe("RowSplit.parse function", function () {
assert.equal(res.cells[0],'"Weight" (kg)');
assert.equal(res.cells[1],'Error code');
assert.equal(res.cells[2],'"Height" (m)');

})
it("should remove closing quote when cell string is quoted ", () => {
// manually edited csv often are formatted to have commas like columns
// this trailing whitespace should be removed before the closing quote is removed
const data = ' " Text with ws " , "Text, with, commas" , " Text, with, both " , No quotes ';
const rowSplit = new RowSplit(new Converter({
noheader: true,
trim: false
}));
const res = rowSplit.parse(data);
assert.equal(res.cells.length, 4);
assert(res.closed);
assert.equal(res.cells[0],' Text with ws ');
assert.equal(res.cells[1],'Text, with, commas');
assert.equal(res.cells[2],' Text, with, both ');
assert.equal(res.cells[3],' No quotes ');
})
it("should remove trailing whitespace (and the closing quote) when cell string is quoted ", () => {
// manually edited csv often are formatted to have commas like columns
// this trailing whitespace should be removed before the closing quote is removed
const data = ' " Text with ws " , " Text, with, commas" , " Text, with, both " , No quotes ';
const rowSplit = new RowSplit(new Converter({
noheader: true,
trim: true
}));
const res = rowSplit.parse(data);
assert.equal(res.cells.length, 4);
assert(res.closed);
assert.equal(res.cells[0],'Text with ws');
assert.equal(res.cells[1],'Text, with, commas');
assert.equal(res.cells[2],'Text, with, both');
assert.equal(res.cells[3],'No quotes');
})
});
14 changes: 10 additions & 4 deletions src/rowSplit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@ export class RowSplit {
row.push("");
continue;
} else if (this.isQuoteOpen(e)) { //quote open
e = trimLeft(e);
e = e.substr(1);
if (trim) {
e = trimLeft(e);
}
if (this.isQuoteClose(e)) { //quote close
e = e.substring(0, e.lastIndexOf(quote));
if (trim) {
e = trimRight(e);
}
e = this.escapeQuote(e);
row.push(e);
continue;
Expand Down Expand Up @@ -113,7 +120,7 @@ export class RowSplit {
} else { //previous quote not closed
if (this.isQuoteClose(e)) { //close double quote
inquote = false;
e = e.substr(0, len - 1);
e = e.substring(0, e.lastIndexOf(quote));
quoteBuff += delimiter + e;
quoteBuff = this.escapeQuote(quoteBuff);
if (trim) {
Expand Down Expand Up @@ -156,16 +163,15 @@ export class RowSplit {
private isQuoteOpen(str: string): boolean {
const quote = this.quote;
const escape = this.escape;
str = trimLeft(str);
return str[0] === quote && (
str[1] !== quote ||
str[1] === escape && (str[2] === quote || str.length === 2));
}
private isQuoteClose(str: string): boolean {
const quote = this.quote;
const escape = this.escape;
if (this.conv.parseParam.trim) {
str = trimRight(str);
}
str = trimRight(str);
let count = 0;
let idx = str.length - 1;
while (str[idx] === quote || str[idx] === escape) {
Expand Down