Skip to content

Commit 8c00071

Browse files
authored
Merge pull request #162 from mrodrig/fix-161
Handle scenario where CSV has no EOL and last character is a field delimiter
2 parents dc48adb + ead00f4 commit 8c00071

File tree

8 files changed

+39
-18
lines changed

8 files changed

+39
-18
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
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.7.6",
8+
"version": "3.7.7",
99
"repository": {
1010
"type": "git",
1111
"url": "https://github.com/mrodrig/json-2-csv.git"
@@ -37,8 +37,8 @@
3737
"cli"
3838
],
3939
"dependencies": {
40-
"deeks": "2.2.6",
41-
"doc-path": "2.0.4"
40+
"deeks": "2.2.7",
41+
"doc-path": "2.1.0"
4242
},
4343
"devDependencies": {
4444
"babel-eslint": "10.1.0",

src/csv2json.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,16 @@ const Csv2Json = function(options) {
106106

107107
// If the start index is the current index (and since the previous character is a comma),
108108
// then the value being parsed is an empty value accordingly, add an empty string
109-
let parsedValue = nextNChar === options.delimiter.eol && stateVariables.startIndex === index
110-
? ''
109+
if (nextNChar === options.delimiter.eol && stateVariables.startIndex === index) {
110+
splitLine.push('');
111+
} else if (character === options.delimiter.field) {
112+
// If we reached the end of the CSV, there's no new line, and the current character is a comma
113+
// then add an empty string for the current value
114+
splitLine.push('');
115+
} else {
111116
// Otherwise, there's a valid value, and the start index isn't the current index, grab the whole value
112-
: csv.substr(stateVariables.startIndex);
113-
114-
// Push the value for the field that we were parsing
115-
splitLine.push(parsedValue);
117+
splitLine.push(csv.substr(stateVariables.startIndex));
118+
}
116119

117120
// Since the last character is a comma, there's still an additional implied field value trailing the comma.
118121
// Since this value is empty, we push an extra empty value

test/config/testCsvFilesList.js

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

4142
function readCsvFile(filePath) {

test/config/testJsonFilesList.js

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

test/csv2json.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ function runTests(jsonTestData, csvTestData) {
180180
done();
181181
});
182182
});
183+
184+
// Test case for #161
185+
it('should properly convert empty field values if they occur at the end of the csv with no EOL at the end', (done) => {
186+
converter.csv2json(csvTestData.emptyLastFieldValueNoEol, (err, json) => {
187+
if (err) done(err);
188+
json.should.deepEqual(jsonTestData.emptyLastFieldValueNoEol);
189+
done();
190+
});
191+
});
183192
});
184193

185194
describe('Error Handling', () => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
param,20/9/27,20/9/28
2+
c,2,2
3+
d,,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "param": "c", "20/9/27": 2, "20/9/28": 2 },
3+
{ "param": "d", "20/9/27": "", "20/9/28": "" }
4+
]

0 commit comments

Comments
 (0)