Skip to content

Commit aa346e3

Browse files
authored
Merge pull request #74 from mrodrig/fix-escaping
Fixing #73, bad test, and associated csv-2-json functionality to behave correctly
2 parents ff4ba9f + e0da806 commit aa346e3

File tree

5 files changed

+13
-7
lines changed

5 files changed

+13
-7
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ $ npm run coverage
109109

110110
Current Coverage is:
111111
```
112-
Statements : 96.69% ( 175/181 )
113-
Branches : 93.48% ( 129/138 )
114-
Functions : 100% ( 33/33 )
115-
Lines : 97.63% ( 165/169 )
112+
Statements : 94.84% ( 202/213 )
113+
Branches : 93.33% ( 154/165 )
114+
Functions : 100% ( 37/37 )
115+
Lines : 95.52% ( 192/201 )
116116
```
117117

118118
## Frequently Asked Questions (FAQ)

lib/csv-2-json.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ var splitLine = function (line) {
175175
stateVariables.parsingValue = true;
176176
stateVariables.startIndex = index + 1;
177177
}
178+
else if (character === "\\" && charAfter === options.DELIMITER.WRAP && stateVariables.insideWrapDelimiter) {
179+
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
181+
lastCharacterIndex--; // Update the value since we removed a character
182+
}
178183
// Otherwise increment to the next character
179184
index++;
180185
}

lib/json-2-csv.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +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;
136137
return options.DELIMITER.WRAP + convertValue(value) + options.DELIMITER.WRAP; // Otherwise push the current value
137138
};
138139

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "mrodrig",
33
"name": "json-2-csv",
44
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
5-
"version": "2.0.23",
5+
"version": "2.1.0",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/mrodrig/json-2-csv.git"
@@ -25,7 +25,7 @@
2525
],
2626
"dependencies": {
2727
"underscore": "1.8.3",
28-
"doc-path": "1.0.7",
28+
"doc-path": "1.0.8",
2929
"bluebird": "3.4.6"
3030
},
3131
"devDependencies": {

test/CSV/quoted/nestedQuotes.csv

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

0 commit comments

Comments
 (0)