From fd6ee99dfb34cd0503c905ab5baf31eb4db1611e Mon Sep 17 00:00:00 2001 From: Bogdan Duda Date: Fri, 2 Aug 2024 22:36:02 +0300 Subject: [PATCH 1/2] Avoid double wrapping on header --- src/json2csv.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/json2csv.ts b/src/json2csv.ts index f0055c4..50ccf24 100755 --- a/src/json2csv.ts +++ b/src/json2csv.ts @@ -155,7 +155,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) { headerKey = headerKey.replace(/\\\./g, '.'); } - return wrapFieldValueIfNecessary(headerKey); + return headerKey; }) .join(options.delimiter.field); return params; From 7dfd212d7554f6964f2e8f50f6a453879dafb857 Mon Sep 17 00:00:00 2001 From: Bogdan Duda Date: Fri, 2 Aug 2024 22:56:44 +0300 Subject: [PATCH 2/2] Add test cases for header with quotes --- package.json | 2 +- test/config/testCsvFilesList.ts | 1 + test/config/testJsonFilesList.ts | 1 + test/csv2json.ts | 5 +++++ test/data/csv/quotesHeader.csv | 4 ++++ test/data/json/quotesHeader.json | 10 ++++++++++ test/json2csv.ts | 5 +++++ 7 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/data/csv/quotesHeader.csv create mode 100644 test/data/json/quotesHeader.json diff --git a/package.json b/package.json index 62d8042..c67fb6c 100755 --- a/package.json +++ b/package.json @@ -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.5.4", + "version": "5.5.5", "homepage": "https://mrodrig.github.io/json-2-csv", "repository": { "type": "git", diff --git a/test/config/testCsvFilesList.ts b/test/config/testCsvFilesList.ts index d064ea9..2753c8e 100644 --- a/test/config/testCsvFilesList.ts +++ b/test/config/testCsvFilesList.ts @@ -15,6 +15,7 @@ const csvFileConfig = [ {key: 'nestedMissingField', file: '../data/csv/nestedMissingField.csv'}, {key: 'comma', file: '../data/csv/comma.csv'}, {key: 'quotes', file: '../data/csv/quotes.csv'}, + {key: 'quotesHeader', file: '../data/csv/quotesHeader.csv'}, {key: 'quotesAndCommas', file: '../data/csv/quotesAndCommas.csv'}, {key: 'eol', file: '../data/csv/eol.csv'}, {key: 'assortedValues', file: '../data/csv/assortedValues.csv'}, diff --git a/test/config/testJsonFilesList.ts b/test/config/testJsonFilesList.ts index e1aab49..4a1e40f 100644 --- a/test/config/testJsonFilesList.ts +++ b/test/config/testJsonFilesList.ts @@ -16,6 +16,7 @@ export default { nestedMissingField: require('../data/json/nestedMissingField'), comma: require('../data/json/comma'), quotes: require('../data/json/quotes'), + quotesHeader: require('../data/json/quotesHeader'), quotesAndCommas: require('../data/json/quotesAndCommas'), eol: require('../data/json/eol'), assortedValues: require('../data/json/assortedValues'), diff --git a/test/csv2json.ts b/test/csv2json.ts index a0f62e2..5c62016 100644 --- a/test/csv2json.ts +++ b/test/csv2json.ts @@ -76,6 +76,11 @@ export function runTests() { assert.deepEqual(json, jsonTestData.quotes); }); + it('should convert csv containing a header with quotes to json', () => { + const json = csv2json(csvTestData.quotesHeader); + assert.deepEqual(json, jsonTestData.quotesHeader); + }); + it('should convert csv containing quotes and commas to json', () => { const json = csv2json(csvTestData.quotesAndCommas); assert.deepEqual(json, jsonTestData.quotesAndCommas); diff --git a/test/data/csv/quotesHeader.csv b/test/data/csv/quotesHeader.csv new file mode 100644 index 0000000..b62c81d --- /dev/null +++ b/test/data/csv/quotesHeader.csv @@ -0,0 +1,4 @@ +"author +(original)",quote +Carol Burnett,"""Only I can change my life. No one can do it for me.""" +Professor,"Please submit all assignment names inside ""quotation"" marks (""), like ""Homework 7""" \ No newline at end of file diff --git a/test/data/json/quotesHeader.json b/test/data/json/quotesHeader.json new file mode 100644 index 0000000..58b52b1 --- /dev/null +++ b/test/data/json/quotesHeader.json @@ -0,0 +1,10 @@ +[ + { + "author\n(original)": "Carol Burnett", + "quote": "\"Only I can change my life. No one can do it for me.\"" + }, + { + "author\n(original)": "Professor", + "quote": "Please submit all assignment names inside \"quotation\" marks (\"), like \"Homework 7\"" + } +] \ No newline at end of file diff --git a/test/json2csv.ts b/test/json2csv.ts index 66be69a..f4fab8e 100644 --- a/test/json2csv.ts +++ b/test/json2csv.ts @@ -77,6 +77,11 @@ export function runTests() { assert.equal(csv, csvTestData.quotes); }); + it('should convert a document with header containing quotes to csv', () => { + const csv = json2csv(jsonTestData.quotesHeader); + assert.equal(csv, csvTestData.quotesHeader); + }); + it('should convert a document with fields containing quotes and commas to csv', () => { const csv = json2csv(jsonTestData.quotesAndCommas); assert.equal(csv, csvTestData.quotesAndCommas);