Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix ignoreEmpty=false to keep empty header columns
Ensure missing trailing cells are treated as empty strings, and add a regression test for headers with empty column values.
  • Loading branch information
Rithick574 committed Jan 29, 2026
commit 73efab0644943b8e4b4cf68fc5c9cbb8550287c4
10 changes: 7 additions & 3 deletions src/lineToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ function processRow(row: string[], conv: Converter, index): JSONResult | null {
function convertRowToJson(row: string[], headRow: string[], conv: Converter): { [key: string]: any } | null {
let hasValue = false;
const resultRow = {};
let len = row.length;
if (!conv.parseParam.ignoreEmpty && len > 0 && headRow.length > len) {
len = headRow.length;
}

for (let i = 0, len = row.length; i < len; i++) {
let item = row[i];

for (let i = 0; i < len; i++) {
let item = row[i] ?? '';
if (conv.parseParam.ignoreEmpty && item === '') {
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions test/data/dataEmptyColumns
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
col1,col2,col3
a,b
c,d
14 changes: 14 additions & 0 deletions test/testCSVConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,20 @@ describe("CSV Converter", function () {
});
});

it("should keep empty columns when ignoreEmpty is false", function (done) {
var testData = __dirname + "/data/dataEmptyColumns";
var rs = fs.createReadStream(testData);
var st = rs.pipe(csv({ ignoreEmpty: false }));
st.then(function (res) {
assert(res.length === 2);
assert.equal(res[0].col1, "a");
assert.equal(res[0].col2, "b");
assert.equal(res[0].col3, "");
assert.equal(res[1].col3, "");
done();
});
});

it("should allow no header", function (done) {
var testData = __dirname + "/data/noheadercsv";
var rs = fs.createReadStream(testData);
Expand Down
10 changes: 7 additions & 3 deletions v2/lineToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ function processRow(row, conv, index) {
function convertRowToJson(row, headRow, conv) {
var hasValue = false;
var resultRow = {};
for (var i = 0, len = row.length; i < len; i++) {
var item = row[i];
var len = row.length;
if (!conv.parseParam.ignoreEmpty && len > 0 && headRow.length > len) {
len = headRow.length;
}
for (var i = 0; i < len; i++) {
var item = row[i] !== null && row[i] !== void 0 ? row[i] : '';
if (conv.parseParam.ignoreEmpty && item === '') {
continue;
}
}
hasValue = true;
var head = headRow[i];
if (!head || head === "") {
Expand Down