Skip to content

Commit 7b32338

Browse files
authored
Merge pull request #243 from mrodrig/fix-240
Fix 240
2 parents d636bff + a428c6c commit 7b32338

File tree

8 files changed

+37
-12
lines changed

8 files changed

+37
-12
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "5.0.0",
8+
"version": "5.0.1",
99
"homepage": "https://mrodrig.github.io/json-2-csv",
1010
"repository": {
1111
"type": "git",

src/csv2json.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,21 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
154154
stateVariables.startIndex = index + eolDelimiterLength;
155155
stateVariables.parsingValue = true;
156156
stateVariables.insideWrapDelimiter = charAfter === options.delimiter.wrap;
157-
} else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
157+
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
158+
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
159+
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter
160+
161+
stateVariables.startIndex = index;
162+
stateVariables.insideWrapDelimiter = true;
163+
stateVariables.parsingValue = true;
164+
165+
// If the next character(s) are an EOL delimiter, then skip them so we don't parse what we've seen as another value
166+
if (utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
167+
index += options.delimiter.eol.length + 1; // Skip past EOL
168+
}
169+
}
170+
171+
else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
158172
character === options.delimiter.wrap && utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
159173
// If we reach a wrap which is not preceded by a wrap delim and the next character is an EOL delim (ie. *"\n)
160174

@@ -174,13 +188,6 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
174188
stateVariables.startIndex = index + 2; // next value starts after the field delimiter
175189
stateVariables.insideWrapDelimiter = false;
176190
stateVariables.parsingValue = false;
177-
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
178-
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
179-
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter
180-
181-
stateVariables.startIndex = index;
182-
stateVariables.insideWrapDelimiter = true;
183-
stateVariables.parsingValue = true;
184191
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
185192
!stateVariables.insideWrapDelimiter && stateVariables.parsingValue) {
186193
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const csvFileConfig = [
4949
{key: 'quotedFieldWithNewline', file: '../data/csv/quotedFieldWithNewline.csv'},
5050
{key: 'falsyValues', file: '../data/csv/falsyValues.csv'},
5151
{key: 'nestedNotUnwoundObjects', file: '../data/csv/nestedNotUnwoundObjects.csv'},
52+
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
5253
];
5354

5455
function readCsvFile(filePath: string) {

test/config/testJsonFilesList.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ export default {
4141
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json'),
4242
emptyColumns: require('../data/json/emptyColumns.json'),
4343
quotedFieldWithNewline: require('../data/json/quotedFieldWithNewline.json'),
44-
falsyValues: require('../data/json/falsyValues.json')
44+
falsyValues: require('../data/json/falsyValues.json'),
45+
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
4546
};

test/csv2json.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,12 @@ export function runTests() {
376376
});
377377
assert.deepEqual(json, expectedJson);
378378
});
379+
380+
// Test case for issue #240
381+
it('should handle newline characters properly when inside of a field delimiter', () => {
382+
const json = csv2json(csvTestData.newlineWithWrapDelimiters);
383+
assert.deepEqual(json, jsonTestData.newlineWithWrapDelimiters);
384+
});
379385
});
380386
});
381387

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Rating","Feedback","External Id"
2+
"5","
3+
Layout","id"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"Rating": "5",
4+
"Feedback": "\nLayout",
5+
"External Id": "id"
6+
}
7+
]

0 commit comments

Comments
 (0)