Skip to content

Commit f9c66c0

Browse files
authored
Merge pull request #87 from eric-thelin/master
Escape nested quote with an additional quote
2 parents f76048b + ca712b4 commit f9c66c0

File tree

7 files changed

+16
-14
lines changed

7 files changed

+16
-14
lines changed

lib/csv-2-json.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,8 @@ var splitLine = function (line) {
175175
stateVariables.parsingValue = true;
176176
stateVariables.startIndex = index + 1;
177177
}
178-
else if (character === "\\" && charAfter === options.DELIMITER.WRAP && stateVariables.insideWrapDelimiter) {
178+
else if (character === options.DELIMITER.WRAP && charAfter === options.DELIMITER.WRAP && stateVariables.insideWrapDelimiter) {
179179
line = line.slice(0, index) + line.slice(index+1); // Remove the current character from the line
180-
index--; // Move to position before to prevent moving ahead and skipping a character
181180
lastCharacterIndex--; // Update the value since we removed a character
182181
}
183182
// Otherwise increment to the next character

lib/json-2-csv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var convertField = function (value) {
133133
} else if (_.isBoolean(value)) { // If we have a boolean (avoids false being converted to '')
134134
return options.DELIMITER.WRAP + convertValue(value) + options.DELIMITER.WRAP;
135135
}
136-
value = options.DELIMITER.WRAP && value ? value.replace(new RegExp(options.DELIMITER.WRAP, 'g'), "\\"+options.DELIMITER.WRAP) : value;
136+
value = options.DELIMITER.WRAP && value ? value.replace(new RegExp(options.DELIMITER.WRAP, 'g'), options.DELIMITER.WRAP + options.DELIMITER.WRAP) : value;
137137
return options.DELIMITER.WRAP + convertValue(value) + options.DELIMITER.WRAP; // Otherwise push the current value
138138
};
139139

test/CSV/quoted/nestedQuotes.csv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
"a string"
22
"with a description"
3-
"with a description and \"quotes\""
3+
"with a description and ""quotes"""
4+
"with a description and multiple """"""quotes"""""""

test/CSV/unQuoted/nestedQuotes.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
a string
22
with a description
33
with a description and "quotes"
4+
with a description and multiple """quotes"""

test/JSON/nestedQuotes.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
22
{"a string": "with a description"},
3-
{"a string": "with a description and \"quotes\""}
3+
{"a string": "with a description and \"quotes\""},
4+
{"a string": "with a description and multiple \"\"\"quotes\"\"\""}
45
]

test/testCsv2Json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ var csv2jsonTests = function () {
328328
if (err) { throw err; }
329329
true.should.equal(_.isEqual(err, null));
330330
csv.should.equal(csvTestData.unQuoted.nestedQuotes.replace(/,/g, options.DELIMITER.FIELD));
331-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
331+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
332332
done();
333333
}, options);
334334
});

test/testJson2Csv.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var json2csvTests = function () {
5151
if (err) { throw err; }
5252
true.should.equal(_.isEqual(err, null));
5353
csv.should.equal(csvTestData.unQuoted.nestedQuotes);
54-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
54+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
5555
done();
5656
});
5757
});
@@ -210,7 +210,7 @@ var json2csvTests = function () {
210210
if (err) { throw err; }
211211
true.should.equal(_.isEqual(err, null));
212212
csv.should.equal(csvTestData.unQuoted.nestedQuotes);
213-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
213+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
214214
done();
215215
}, options);
216216
});
@@ -385,7 +385,7 @@ var json2csvTests = function () {
385385
if (err) { throw err; }
386386
true.should.equal(_.isEqual(err, null));
387387
csv.should.equal(csvTestData.unQuoted.nestedQuotes.replace(new RegExp(defaultOptions.DELIMITER.FIELD, 'g'), options.DELIMITER.FIELD));
388-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
388+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
389389
done();
390390
}, options);
391391
});
@@ -562,7 +562,7 @@ var json2csvTests = function () {
562562
if (err) { throw err; }
563563
true.should.equal(_.isEqual(err, null));
564564
csv.should.equal(csvTestData.quoted.nestedQuotes.replace(/,/g, options.DELIMITER.FIELD));
565-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
565+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
566566
done();
567567
}, options);
568568
});
@@ -882,7 +882,7 @@ var json2csvTests = function () {
882882
converter.json2csvPromisified(jsonTestData.nestedQuotes)
883883
.then(function(csv) {
884884
csv.should.equal(csvTestData.unQuoted.nestedQuotes);
885-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
885+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
886886
done();
887887
})
888888
.catch(function (err) {
@@ -1024,7 +1024,7 @@ var json2csvTests = function () {
10241024
converter.json2csvPromisified(jsonTestData.nestedQuotes, options)
10251025
.then(function(csv) {
10261026
csv.should.equal(csvTestData.unQuoted.nestedQuotes);
1027-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
1027+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
10281028
done();
10291029
})
10301030
.catch(function (err) {
@@ -1195,7 +1195,7 @@ var json2csvTests = function () {
11951195
converter.json2csvPromisified(jsonTestData.nestedQuotes, options)
11961196
.then(function(csv) {
11971197
csv.should.equal(csvTestData.unQuoted.nestedQuotes.replace(new RegExp(defaultOptions.DELIMITER.FIELD, 'g'), options.DELIMITER.FIELD));
1198-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
1198+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
11991199
done();
12001200
})
12011201
.catch(function (err) {
@@ -1368,7 +1368,7 @@ var json2csvTests = function () {
13681368
converter.json2csvPromisified(jsonTestData.nestedQuotes, options)
13691369
.then(function(csv) {
13701370
csv.should.equal(csvTestData.quoted.nestedQuotes.replace(/,/g, options.DELIMITER.FIELD));
1371-
csv.split(options.DELIMITER.EOL).length.should.equal(4);
1371+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
13721372
done();
13731373
})
13741374
.catch(function (err) {

0 commit comments

Comments
 (0)