Skip to content

Fix 240 #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"name": "json-2-csv",
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
"version": "5.0.0",
"version": "5.0.1",
"homepage": "https://mrodrig.github.io/json-2-csv",
"repository": {
"type": "git",
Expand Down
23 changes: 15 additions & 8 deletions src/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
stateVariables.startIndex = index + eolDelimiterLength;
stateVariables.parsingValue = true;
stateVariables.insideWrapDelimiter = charAfter === options.delimiter.wrap;
} else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter

stateVariables.startIndex = index;
stateVariables.insideWrapDelimiter = true;
stateVariables.parsingValue = true;

// If the next character(s) are an EOL delimiter, then skip them so we don't parse what we've seen as another value
if (utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
index += options.delimiter.eol.length + 1; // Skip past EOL
}
}

else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
character === options.delimiter.wrap && utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
// If we reach a wrap which is not preceded by a wrap delim and the next character is an EOL delim (ie. *"\n)

Expand All @@ -174,13 +188,6 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
stateVariables.startIndex = index + 2; // next value starts after the field delimiter
stateVariables.insideWrapDelimiter = false;
stateVariables.parsingValue = false;
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter

stateVariables.startIndex = index;
stateVariables.insideWrapDelimiter = true;
stateVariables.parsingValue = true;
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
!stateVariables.insideWrapDelimiter && stateVariables.parsingValue) {
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const csvFileConfig = [
{key: 'quotedFieldWithNewline', file: '../data/csv/quotedFieldWithNewline.csv'},
{key: 'falsyValues', file: '../data/csv/falsyValues.csv'},
{key: 'nestedNotUnwoundObjects', file: '../data/csv/nestedNotUnwoundObjects.csv'},
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
];

function readCsvFile(filePath: string) {
Expand Down
3 changes: 2 additions & 1 deletion test/config/testJsonFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ export default {
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json'),
emptyColumns: require('../data/json/emptyColumns.json'),
quotedFieldWithNewline: require('../data/json/quotedFieldWithNewline.json'),
falsyValues: require('../data/json/falsyValues.json')
falsyValues: require('../data/json/falsyValues.json'),
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
};
6 changes: 6 additions & 0 deletions test/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ export function runTests() {
});
assert.deepEqual(json, expectedJson);
});

// Test case for issue #240
it('should handle newline characters properly when inside of a field delimiter', () => {
const json = csv2json(csvTestData.newlineWithWrapDelimiters);
assert.deepEqual(json, jsonTestData.newlineWithWrapDelimiters);
});
});
});

Expand Down
3 changes: 3 additions & 0 deletions test/data/csv/newlineWithWrapDelimiters.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"Rating","Feedback","External Id"
"5","
Layout","id"
7 changes: 7 additions & 0 deletions test/data/json/newlineWithWrapDelimiters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"Rating": "5",
"Feedback": "\nLayout",
"External Id": "id"
}
]