Skip to content

Commit

Permalink
fix: properly escape quotes when using excel mode
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jan 27, 2021
1 parent e1ba2c1 commit 77c1fd3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ class JSON2CSVBase {
value = value.replace(new RegExp(this.opts.quote, 'g'), this.opts.escapedQuote);
}

value = `${this.opts.quote}${value}${this.opts.quote}`;

if (this.opts.excelStrings) {
value = `"="${value}""`;
value = `"=""${value.replace(new RegExp(this.opts.quote, 'g'), this.opts.escapedQuote)}"""`;
} else {
value = `${this.opts.quote}${value}${this.opts.quote}`;
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,17 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should format strings to force excel to view the values as strings with escaped quotes', (t) => {
const opts = '--excel-strings';

exec(`${cli} -i "${getFixturePath('/json/quotes.json')}" ${opts}`, (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.excelStringsWithEscapedQuoted);
t.end();
});
});

// Escaping and preserving values

testRunner.add('should parse JSON values with trailing backslashes', (t) => {
Expand Down
16 changes: 16 additions & 0 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,22 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
t.end();
});

testRunner.add('should format strings to force excel to view the values as strings with escaped quotes', async (t) => {
const opts = {
excelStrings:true
};
const parser = new AsyncParser(opts);

try {
const csv = await parser.fromInput(jsonFixtures.quotes()).promise();
t.equal(csv, csvFixtures.excelStringsWithEscapedQuoted);
} catch(err) {
t.fail(err.message);
}

t.end();
});

// Escaping and preserving values

testRunner.add('should parse JSON values with trailing backslashes', async (t) => {
Expand Down
12 changes: 12 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should format strings to force excel to view the values as strings with escaped quotes', (t) => {
const opts = {
excelStrings:true
};

const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.quotes);

t.equal(csv, csvFixtures.excelStringsWithEscapedQuoted);
t.end();
});

// Escaping and preserving values

testRunner.add('should parse JSON values with trailing backslashes', (t) => {
Expand Down
21 changes: 21 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,27 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
});
});

testRunner.add('should format strings to force excel to view the values as strings with escaped quotes', (t) => {
const opts = {
excelStrings:true
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.quotes().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.excelStringsWithEscapedQuoted);
t.end();
})
.on('error', err => {
t.fail(err.message);
t.end();
});
});

// Escaping and preserving values

testRunner.add('should parse JSON values with trailing backslashes', (t) => {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/csv/excelStringsWithEscapedQuoted.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"=""a string"""
"=""with a description"""
"=""with a description and """"quotes"""""""

0 comments on commit 77c1fd3

Please sign in to comment.