Skip to content

Commit fa1deea

Browse files
authored
Merge pull request #260 from BEE-CODED/main
Fix double wrapping of header containing newlines
2 parents 65829f7 + 7dfd212 commit fa1deea

File tree

8 files changed

+28
-2
lines changed

8 files changed

+28
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "5.5.4",
8+
"version": "5.5.5",
99
"homepage": "https://mrodrig.github.io/json-2-csv",
1010
"repository": {
1111
"type": "git",

src/json2csv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
155155
headerKey = headerKey.replace(/\\\./g, '.');
156156
}
157157

158-
return wrapFieldValueIfNecessary(headerKey);
158+
return headerKey;
159159
})
160160
.join(options.delimiter.field);
161161
return params;

test/config/testCsvFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const csvFileConfig = [
1515
{key: 'nestedMissingField', file: '../data/csv/nestedMissingField.csv'},
1616
{key: 'comma', file: '../data/csv/comma.csv'},
1717
{key: 'quotes', file: '../data/csv/quotes.csv'},
18+
{key: 'quotesHeader', file: '../data/csv/quotesHeader.csv'},
1819
{key: 'quotesAndCommas', file: '../data/csv/quotesAndCommas.csv'},
1920
{key: 'eol', file: '../data/csv/eol.csv'},
2021
{key: 'assortedValues', file: '../data/csv/assortedValues.csv'},

test/config/testJsonFilesList.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default {
1616
nestedMissingField: require('../data/json/nestedMissingField'),
1717
comma: require('../data/json/comma'),
1818
quotes: require('../data/json/quotes'),
19+
quotesHeader: require('../data/json/quotesHeader'),
1920
quotesAndCommas: require('../data/json/quotesAndCommas'),
2021
eol: require('../data/json/eol'),
2122
assortedValues: require('../data/json/assortedValues'),

test/csv2json.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ export function runTests() {
7676
assert.deepEqual(json, jsonTestData.quotes);
7777
});
7878

79+
it('should convert csv containing a header with quotes to json', () => {
80+
const json = csv2json(csvTestData.quotesHeader);
81+
assert.deepEqual(json, jsonTestData.quotesHeader);
82+
});
83+
7984
it('should convert csv containing quotes and commas to json', () => {
8085
const json = csv2json(csvTestData.quotesAndCommas);
8186
assert.deepEqual(json, jsonTestData.quotesAndCommas);

test/data/csv/quotesHeader.csv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"author
2+
(original)",quote
3+
Carol Burnett,"""Only I can change my life. No one can do it for me."""
4+
Professor,"Please submit all assignment names inside ""quotation"" marks (""), like ""Homework 7"""

test/data/json/quotesHeader.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"author\n(original)": "Carol Burnett",
4+
"quote": "\"Only I can change my life. No one can do it for me.\""
5+
},
6+
{
7+
"author\n(original)": "Professor",
8+
"quote": "Please submit all assignment names inside \"quotation\" marks (\"), like \"Homework 7\""
9+
}
10+
]

test/json2csv.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ export function runTests() {
7777
assert.equal(csv, csvTestData.quotes);
7878
});
7979

80+
it('should convert a document with header containing quotes to csv', () => {
81+
const csv = json2csv(jsonTestData.quotesHeader);
82+
assert.equal(csv, csvTestData.quotesHeader);
83+
});
84+
8085
it('should convert a document with fields containing quotes and commas to csv', () => {
8186
const csv = json2csv(jsonTestData.quotesAndCommas);
8287
assert.equal(csv, csvTestData.quotesAndCommas);

0 commit comments

Comments
 (0)