Skip to content

Commit

Permalink
Merge pull request #260 from BEE-CODED/main
Browse files Browse the repository at this point in the history
Fix double wrapping of header containing newlines
  • Loading branch information
mrodrig authored Aug 4, 2024
2 parents 65829f7 + 7dfd212 commit fa1deea
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 2 deletions.
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.5.4",
"version": "5.5.5",
"homepage": "https://mrodrig.github.io/json-2-csv",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
Expand Down
1 change: 1 addition & 0 deletions test/config/testJsonFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
5 changes: 5 additions & 0 deletions test/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions test/data/csv/quotesHeader.csv
Original file line number Diff line number Diff line change
@@ -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"""
10 changes: 10 additions & 0 deletions test/data/json/quotesHeader.json
Original file line number Diff line number Diff line change
@@ -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\""
}
]
5 changes: 5 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit fa1deea

Please sign in to comment.