From 3a8ce35ae4fd9faa323699eb423f5299100e214a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9verin=20Gouge?= Date: Fri, 24 Jan 2025 16:59:57 +0100 Subject: [PATCH] fix: comma starting a wrapped value broke the line --- src/csv2json.ts | 2 +- test/config/testCsvFilesList.ts | 1 + test/config/testJsonFilesList.ts | 1 + test/csv2json.ts | 5 +++++ test/data/csv/commaAfterOpeningWrap.csv | 3 +++ test/data/json/commaAfterOpeningWrap.json | 10 ++++++++++ 6 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/data/csv/commaAfterOpeningWrap.csv create mode 100644 test/data/json/commaAfterOpeningWrap.json diff --git a/src/csv2json.ts b/src/csv2json.ts index c1c9268..99976cc 100644 --- a/src/csv2json.ts +++ b/src/csv2json.ts @@ -190,7 +190,7 @@ export const Csv2Json = function (options: FullCsv2JsonOptions) { stateVariables.insideWrapDelimiter = true; stateVariables.parsingValue = true; stateVariables.startIndex = index; - } else if (character === options.delimiter.wrap && charAfter === options.delimiter.field) { + } else if (character === options.delimiter.wrap && charAfter === options.delimiter.field && stateVariables.insideWrapDelimiter) { // If we reached a wrap delimiter with a field delimiter after it (ie. *",) splitLine.push(csv.substring(stateVariables.startIndex, index + 1)); diff --git a/test/config/testCsvFilesList.ts b/test/config/testCsvFilesList.ts index 0b5f226..cadbc23 100644 --- a/test/config/testCsvFilesList.ts +++ b/test/config/testCsvFilesList.ts @@ -14,6 +14,7 @@ const csvFileConfig = [ { key: 'nested', file: '../data/csv/nested.csv' }, { key: 'nestedMissingField', file: '../data/csv/nestedMissingField.csv' }, { key: 'comma', file: '../data/csv/comma.csv' }, + { key: 'commaAfterOpeningWrap', file: '../data/csv/commaAfterOpeningWrap.csv' }, { key: 'quotes', file: '../data/csv/quotes.csv' }, { key: 'quotesHeader', file: '../data/csv/quotesHeader.csv' }, { key: 'quotesAndCommas', file: '../data/csv/quotesAndCommas.csv' }, diff --git a/test/config/testJsonFilesList.ts b/test/config/testJsonFilesList.ts index 42fd5cb..0f3c712 100644 --- a/test/config/testJsonFilesList.ts +++ b/test/config/testJsonFilesList.ts @@ -15,6 +15,7 @@ export default { nested: require('../data/json/nested'), nestedMissingField: require('../data/json/nestedMissingField'), comma: require('../data/json/comma'), + commaAfterOpeningWrap: require('../data/json/commaAfterOpeningWrap'), quotes: require('../data/json/quotes'), quotesHeader: require('../data/json/quotesHeader'), quotesAndCommas: require('../data/json/quotesAndCommas'), diff --git a/test/csv2json.ts b/test/csv2json.ts index 91c3558..b2280b8 100644 --- a/test/csv2json.ts +++ b/test/csv2json.ts @@ -71,6 +71,11 @@ export function runTests() { assert.deepEqual(json, jsonTestData.comma); }); + it('should convert csv containing a field with a comma after an opening wrap to json', () => { + const json = csv2json(csvTestData.commaAfterOpeningWrap); + assert.deepEqual(json, jsonTestData.commaAfterOpeningWrap); + }); + it('should convert csv containing a field with quotes to json', () => { const json = csv2json(csvTestData.quotes); assert.deepEqual(json, jsonTestData.quotes); diff --git a/test/data/csv/commaAfterOpeningWrap.csv b/test/data/csv/commaAfterOpeningWrap.csv new file mode 100644 index 0000000..7de5cd3 --- /dev/null +++ b/test/data/csv/commaAfterOpeningWrap.csv @@ -0,0 +1,3 @@ +username,comment +mrodrig,",In other words, this is cool!" +jsmith,"This module is really, really helpful!" diff --git a/test/data/json/commaAfterOpeningWrap.json b/test/data/json/commaAfterOpeningWrap.json new file mode 100644 index 0000000..69e5631 --- /dev/null +++ b/test/data/json/commaAfterOpeningWrap.json @@ -0,0 +1,10 @@ +[ + { + "username": "mrodrig", + "comment": ",In other words, this is cool!" + }, + { + "username": "jsmith", + "comment": "This module is really, really helpful!" + } +] \ No newline at end of file