diff --git a/lib/csv2json.js b/lib/csv2json.js index 4e3b266..c7d8935 100644 --- a/lib/csv2json.js +++ b/lib/csv2json.js @@ -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; @@ -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; diff --git a/test/config/testCsvFilesList.js b/test/config/testCsvFilesList.js index cb115ec..78edcb0 100644 --- a/test/config/testCsvFilesList.js +++ b/test/config/testCsvFilesList.js @@ -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) { diff --git a/test/config/testJsonFilesList.js b/test/config/testJsonFilesList.js index fb2f351..a2a9143 100644 --- a/test/config/testJsonFilesList.js +++ b/test/config/testJsonFilesList.js @@ -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') }; diff --git a/test/csv2json.js b/test/csv2json.js index f7e412b..8445da1 100644 --- a/test/csv2json.js +++ b/test/csv2json.js @@ -226,6 +226,7 @@ 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); @@ -233,6 +234,15 @@ function runTests(jsonTestData, csvTestData) { 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', () => { diff --git a/test/data/csv/quotedFieldWithNewline.csv b/test/data/csv/quotedFieldWithNewline.csv new file mode 100644 index 0000000..89816df --- /dev/null +++ b/test/data/csv/quotedFieldWithNewline.csv @@ -0,0 +1,8 @@ +first,second,third +"hello +",world,test +first,"world + +",test +world,test,"hello +" \ No newline at end of file diff --git a/test/data/json/quotedFieldWithNewline.json b/test/data/json/quotedFieldWithNewline.json new file mode 100644 index 0000000..b1eae1f --- /dev/null +++ b/test/data/json/quotedFieldWithNewline.json @@ -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"} + ] \ No newline at end of file