Skip to content

Commit

Permalink
fix: don't escape tabs (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Jun 23, 2018
1 parent c191adf commit a36c8e3
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/JSON2CSVBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ class JSON2CSVBase {
if (isValueString) {
value = value
.replace(/\n/g, '\u2028')
.replace(/\r/g, '\u2029');
.replace(/\r/g, '\u2029')
.replace(/\t/g, '\u21E5');
}

//JSON.stringify('\\') results in a string with two backslash
Expand All @@ -175,7 +176,8 @@ class JSON2CSVBase {
if (isValueString) {
stringifiedValue = stringifiedValue
.replace(/\u2028/g, '\n')
.replace(/\u2029/g, '\r');
.replace(/\u2029/g, '\r')
.replace(/\u21E5/g, '\t');
}


Expand All @@ -185,8 +187,8 @@ class JSON2CSVBase {
.replace(/(\\")(?!$)/g, this.opts.doubleQuote);
} else {
// Unescape automatically escaped double quote symbol
// Replace wrapping quotes
// Replace single quote with double quote
// Replace wrapping quotes
stringifiedValue = stringifiedValue
.replace(/(\\")(?!$)/g, '"')
.replace(new RegExp(this.opts.quote, 'g'), this.opts.doubleQuote)
Expand Down
9 changes: 9 additions & 0 deletions test/CLI.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,15 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
});
});

testRunner.add('should preserve tabs in values', (t) => {
child_process.exec(cli + '-i ' + getFixturePath('/json/escapeTab.json'), (err, stdout, stderr) => {
t.notOk(stderr);
const csv = stdout;
t.equal(csv, csvFixtures.escapeTab);
t.end();
});
});

// Header

testRunner.add('should parse json to csv without column title', (t) => {
Expand Down
8 changes: 8 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should preserve tabs in values', (t) => {
const parser = new Json2csvParser();
const csv = parser.parse(jsonFixtures.escapeTab);

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

// Header

testRunner.add('should parse json to csv without column title', (t) => {
Expand Down
14 changes: 14 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,20 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should preserve tabs in values', (t) => {
const transform = new Json2csvTransform();
const processor = jsonFixtures.escapeTab().pipe(transform);

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

// Header

testRunner.add('should parse json to csv without column title', (t) => {
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/csv/escapeTab.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"Id","Name","CreatedById","CreatedDate","IsDeleted","LastModifiedById","LastModifiedDate","LastReferencedDate","LastViewedDate","OwnerId","SBQQ__CustomSource__c","SBQQ__FontFamily__c","SBQQ__FontSize__c","SBQQ__Markup__c","SBQQ__RawMarkup__c","SBQQ__TableStyle__c","SBQQ__TextColor__c","SBQQ__Type__c","SystemModstamp"
"a13f4000001NrzWAAS","a13f4000001NrzW","005f4000002OzPRAA0","2018-06-20T15:49:06.000+0000","FALSE","005f4000002OzPRAA0","2018-06-20T15:51:36.000+0000","2018-06-20T15:51:36.000+0000","2018-06-20T15:51:36.000+0000","005f4000002OzPRAA0","","","","","<table width=""100%"">
<tbody>
<tr>
<td style=""text-align: left; width: 25%;"">{!companyLogo}</td>
<td style=""width: 10%;"">&#160;</td>
<td style=""text-align: left; width: 30%;"">
<p><span style=""font-size:10px;"">{!quote.Name__c}<br />
{!quote.Street__c}<br />
{!quote.City__c}, {!quote.State__c} {!quote.Postal_Code__c}<br />
Office: {!quote.Phone__c}<br />
Fax: {!quote.Fax__c}</span></p>
</td>
<td style=""width: 3%;"">&#160;</td>
<td style=""width: 30%;border: 1px solid black;"" valign=""middle"">&#160;
<p><span style=""font-size: 10px;"">&#160; Agreement&#160;#: {!quote.Name}-{!document.SBQQ__Version__c}</span></p>

<p><span style=""font-size: 10px;"">&#160; Start Date: {!quote.SBQQ__StartDate__c}<br />
&#160; Expiration Date: {!quote.SBQQ__ExpirationDate__c}<br />
&#160; Sales Contact: {!salesRep.Name}</span></p>
</td>
</tr>
</tbody>
</table>","Standard","","HTML","2018-06-20T15:51:36.000+0000"
23 changes: 23 additions & 0 deletions test/fixtures/json/escapeTab.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"Id": "a13f4000001NrzWAAS",
"Name": "a13f4000001NrzW",
"CreatedById": "005f4000002OzPRAA0",
"CreatedDate": "2018-06-20T15:49:06.000+0000",
"IsDeleted": "FALSE",
"LastModifiedById": "005f4000002OzPRAA0",
"LastModifiedDate": "2018-06-20T15:51:36.000+0000",
"LastReferencedDate": "2018-06-20T15:51:36.000+0000",
"LastViewedDate": "2018-06-20T15:51:36.000+0000",
"OwnerId": "005f4000002OzPRAA0",
"SBQQ__CustomSource__c": "",
"SBQQ__FontFamily__c": "",
"SBQQ__FontSize__c": "",
"SBQQ__Markup__c": "",
"SBQQ__RawMarkup__c": "<table width=\"100%\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td style=\"text-align: left; width: 25%;\">{!companyLogo}</td>\n\t\t\t<td style=\"width: 10%;\">&#160;</td>\n\t\t\t<td style=\"text-align: left; width: 30%;\">\n\t\t\t<p><span style=\"font-size:10px;\">{!quote.Name__c}<br />\n\t\t\t{!quote.Street__c}<br />\n\t\t\t{!quote.City__c}, {!quote.State__c} {!quote.Postal_Code__c}<br />\n\t\t\tOffice: {!quote.Phone__c}<br />\n\t\t\tFax: {!quote.Fax__c}</span></p>\n\t\t\t</td>\n\t\t\t<td style=\"width: 3%;\">&#160;</td>\n\t\t\t<td style=\"width: 30%;border: 1px solid black;\" valign=\"middle\">&#160;\n\t\t\t<p><span style=\"font-size: 10px;\">&#160; Agreement&#160;#: {!quote.Name}-{!document.SBQQ__Version__c}</span></p>\n\n\t\t\t<p><span style=\"font-size: 10px;\">&#160; Start Date: {!quote.SBQQ__StartDate__c}<br />\n\t\t\t&#160; Expiration Date: {!quote.SBQQ__ExpirationDate__c}<br />\n\t\t\t&#160; Sales Contact: {!salesRep.Name}</span></p>\n\t\t\t</td>\n\t\t</tr>\n\t</tbody>\n</table>",
"SBQQ__TableStyle__c": "Standard",
"SBQQ__TextColor__c": "",
"SBQQ__Type__c": "HTML",
"SystemModstamp": "2018-06-20T15:51:36.000+0000"
}
]

0 comments on commit a36c8e3

Please sign in to comment.