Skip to content

Commit cf9821b

Browse files
authored
Merge pull request #118 from mrodrig/fix-117
Fix 117
2 parents 56c19d0 + effa5e9 commit cf9821b

File tree

8 files changed

+25
-4
lines changed

8 files changed

+25
-4
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "3.5.1",
8+
"version": "3.5.2",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/mrodrig/json-2-csv.git"

src/csv2json.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const Csv2Json = function(options) {
1414
* @param headerKey
1515
* @returns {*}
1616
*/
17-
function trimHeaderKey(headerKey) {
17+
function processHeaderKey(headerKey) {
18+
headerKey = removeWrapDelimitersFromValue(headerKey);
1819
if (options.trimHeaderFields) {
1920
return headerKey.split('.')
2021
.map((component) => component.trim())
@@ -33,7 +34,7 @@ const Csv2Json = function(options) {
3334
// Generate and return the heading keys
3435
headerRow = params.lines[0];
3536
params.headerFields = headerRow.map((headerKey, index) => ({
36-
value: trimHeaderKey(headerKey),
37+
value: processHeaderKey(headerKey),
3738
index: index
3839
}));
3940

test/config/testCsvFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const fs = require('fs'),
2727
{key: 'noHeader', file: '../data/csv/noHeader.csv'},
2828
{key: 'sortedHeader', file: '../data/csv/sortedHeader.csv'},
2929
{key: 'emptyFieldValues', file: '../data/csv/emptyFieldValues.csv'},
30+
{key: 'quotedEmptyFieldValue', file: '../data/csv/quotedEmptyFieldValue.csv'},
3031
{key: 'csvEmptyLastValue', file: '../data/csv/csvEmptyLastValue.csv'}
3132
];
3233

test/config/testJsonFilesList.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ module.exports = {
2020
trimmedHeader: require('../data/json/trimmedHeader'),
2121
specifiedKeys: require('../data/json/specifiedKeys'),
2222
emptyFieldValues: require('../data/json/emptyFieldValues'),
23+
quotedEmptyFieldValue: require('../data/json/quotedEmptyFieldValue'),
2324
csvEmptyLastValue: require('../data/json/csvEmptyLastValue')
2425
};

test/csv2json.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ function runTests(jsonTestData, csvTestData) {
153153
done();
154154
});
155155
});
156+
157+
// Test case for #115
158+
it('should properly handle quoted empty field values', (done) => {
159+
converter.csv2json(csvTestData.quotedEmptyFieldValue, (err, json) => {
160+
if (err) done(err);
161+
json.should.deepEqual(jsonTestData.quotedEmptyFieldValue);
162+
done();
163+
});
164+
});
156165
});
157166

158167
describe('Error Handling', () => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"a","b","c"
2+
"foo","","bar"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"a": "foo",
4+
"b": "",
5+
"c": "bar"
6+
}
7+
]

0 commit comments

Comments
 (0)