Skip to content

Commit ffcada9

Browse files
authored
Merge pull request #156 from mrodrig/fix-155
Fix to properly handle empty last field values in csv2json.
2 parents a352e18 + 22ab313 commit ffcada9

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

src/csv2json.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ const Csv2Json = function(options) {
124124
stateVariables.startIndex = index + eolDelimiterLength;
125125
stateVariables.parsingValue = true;
126126
stateVariables.insideWrapDelimiter = charAfter === options.delimiter.wrap;
127+
} else if (index === lastCharacterIndex && character === options.delimiter.field) {
128+
// If we reach the end of the CSV and the current character is a field delimiter
129+
130+
// Parse the previously seen value and add it to the line
131+
let parsedValue = csv.substring(stateVariables.startIndex, index);
132+
splitLine.push(parsedValue);
133+
134+
// Then add an empty string to the line since the last character being a field delimiter indicates an empty field
135+
splitLine.push('');
136+
lines.push(splitLine);
127137
} else if (index === lastCharacterIndex || nextNChar === options.delimiter.eol &&
128138
// if we aren't inside wrap delimiters or if we are but the character before was a wrap delimiter and we didn't just see two
129139
(!stateVariables.insideWrapDelimiter ||

test/config/testCsvFilesList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const fs = require('fs'),
3434
{key: 'unwindWithSpecifiedKeys', file: '../data/csv/unwindWithSpecifiedKeys.csv'},
3535
{key: 'localeFormat', file: '../data/csv/localeFormat.csv'},
3636
{key: 'invalidParsedValues', file: '../data/csv/invalidParsedValues.csv'},
37-
{key: 'firstColumnWrapCRLF', file: '../data/csv/firstColumnWrapCRLF.csv'}
37+
{key: 'firstColumnWrapCRLF', file: '../data/csv/firstColumnWrapCRLF.csv'},
38+
{key: 'emptyLastFieldValue', file: '../data/csv/emptyLastFieldValue.csv'}
3839
];
3940

4041
function readCsvFile(filePath) {

test/config/testJsonFilesList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ module.exports = {
2626
unwindEmptyArray: require('../data/json/unwindEmptyArray'),
2727
localeFormat: require('../data/json/localeFormat'),
2828
invalidParsedValues: require('../data/json/invalidParsedValues'),
29-
firstColumnWrapCRLF: require('../data/json/firstColumnWrapCRLF.json')
29+
firstColumnWrapCRLF: require('../data/json/firstColumnWrapCRLF.json'),
30+
emptyLastFieldValue: require('../data/json/emptyLastFieldValue.json')
3031
};

test/csv2json.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ function runTests(jsonTestData, csvTestData) {
171171
done();
172172
});
173173
});
174+
175+
// Test case for #155
176+
it('should properly convert empty field values if they occur at the end of the csv', (done) => {
177+
converter.csv2json(csvTestData.emptyLastFieldValue, (err, json) => {
178+
if (err) done(err);
179+
json.should.deepEqual(jsonTestData.emptyLastFieldValue);
180+
done();
181+
});
182+
});
174183
});
175184

176185
describe('Error Handling', () => {

test/data/csv/emptyLastFieldValue.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data.uid,data.name
2+
,
3+
,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[ { "data":{ "uid": "", "name": "" } },
2+
{ "data":{ "uid": "", "name": "" } }]

0 commit comments

Comments
 (0)