Skip to content

Commit d29f767

Browse files
committed
fix: add support for wrap delimited header field values
* Removes wrap delimiters from header record values if so wrapped. Fixes #117
1 parent 56c19d0 commit d29f767

File tree

6 files changed

+23
-2
lines changed

6 files changed

+23
-2
lines changed

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)