Skip to content

Handle nested EOL delimiter inside wrap delimiter properly #215

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 2 commits into from
Jan 9, 2022
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 lib/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const Csv2Json = function(options) {
stateVariables.insideWrapDelimiter = false;
stateVariables.parsingValue = false;
// Next iteration will substring, add the value to the line, and push the line onto the array of lines
} else if (character === options.delimiter.wrap && (index === 0 || utils.getNCharacters(csv, index - eolDelimiterLength, eolDelimiterLength) === options.delimiter.eol)) {
} else if (character === options.delimiter.wrap && (index === 0 || utils.getNCharacters(csv, index - eolDelimiterLength, eolDelimiterLength) === options.delimiter.eol && !stateVariables.insideWrapDelimiter)) {
// If the line starts with a wrap delimiter (ie. "*)

stateVariables.insideWrapDelimiter = true;
Expand Down Expand Up @@ -190,7 +190,7 @@ const Csv2Json = function(options) {
stateVariables.insideWrapDelimiter = true;
stateVariables.parsingValue = true;
stateVariables.startIndex = index;
} else if (character === options.delimiter.wrap && charAfter === options.delimiter.wrap) {
} else if (character === options.delimiter.wrap && charAfter === options.delimiter.wrap && index !== stateVariables.startIndex) {
// If we run into an escaped quote (ie. "") skip past the second quote

index += 2;
Expand Down
3 changes: 2 additions & 1 deletion test/config/testCsvFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ const fs = require('fs'),
{key: 'nestedDotKeys', file: '../data/csv/nestedDotKeys.csv'},
{key: 'nestedDotKeysWithArray', file: '../data/csv/nestedDotKeysWithArray.csv'},
{key: 'nestedDotKeysWithArrayExpandedUnwound', file: '../data/csv/nestedDotKeysWithArrayExpandedUnwound.csv'},
{key: 'emptyColumns', file: '../data/csv/emptyColumns.csv'}
{key: 'emptyColumns', file: '../data/csv/emptyColumns.csv'},
{key: 'quotedFieldWithNewline', file: '../data/csv/quotedFieldWithNewline.csv'}
];

function readCsvFile(filePath) {
Expand Down
3 changes: 2 additions & 1 deletion test/config/testJsonFilesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ module.exports = {
nestedDotKeys: require('../data/json/nestedDotKeys.json'),
nestedDotKeysWithArray: require('../data/json/nestedDotKeysWithArray.json'),
nestedDotKeysWithArrayExpandedUnwound: require('../data/json/nestedDotKeysWithArrayExpandedUnwound.json'),
emptyColumns: require('../data/json/emptyColumns.json')
emptyColumns: require('../data/json/emptyColumns.json'),
quotedFieldWithNewline: require('../data/json/quotedFieldWithNewline.json')
};
10 changes: 10 additions & 0 deletions test/csv2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,23 @@ function runTests(jsonTestData, csvTestData) {
});
});

// Test case for #204
it('should drop any values with empty column headers', (done) => {
converter.csv2json(csvTestData.emptyColumns, (err, json) => {
if (err) done(err);
json.should.deepEqual(jsonTestData.emptyColumns);
done();
});
});

// Test case for #214
it('should handle quoted fields with nested EOL characters', (done) => {
converter.csv2json(csvTestData.quotedFieldWithNewline, (err, json) => {
if (err) done(err);
json.should.deepEqual(jsonTestData.quotedFieldWithNewline);
done();
});
});
});

describe('Error Handling', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/data/csv/quotedFieldWithNewline.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
first,second,third
"hello
",world,test
first,"world

",test
world,test,"hello
"
5 changes: 5 additions & 0 deletions test/data/json/quotedFieldWithNewline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "first": "hello\n", "second": "world", "third": "test" },
{ "first": "first", "second": "world\n\n", "third": "test"},
{ "first": "world", "second": "test", "third": "hello\n"}
]