Reputation: 39
I have used fast-csv module and it only has the option to ignore the empty rows but I need to count the empty rows.
csv
.fromPath(importFile.path, {
headers: true,
ignoreEmpty: true
})
.on("data", function(data) {
console.log(data)
})
Upvotes: 0
Views: 1186
Reputation: 3830
From the code I am seeing you are already half way there. Just initialize a count
variable at the top and in the callback of "data" event split the lines in array and check if the row is empty, based on your requirement.
function getEmptyRows(csv){
return new Promise((resolve, reject) => {
let emptyRows = 0;
csv
.fromPath(importFile.path, {
headers: true,
ignoreEmpty: true
})
.on("data", (data) => {
const lines = data.trim().split(/\s*[\r\n]+\s*/g);
lines.forEach((line) => {
if(line.match(/([^\s,])/)){
count++;
}
});
})
.on('end', () => {
return resolve(count);
});
});
}
And you can use the function above in the following ways-
getEmptyRows(csv).then((count) => {
// do your operation...
});
// or as an async/await
async () => {
const count = await getEmptyRows(csv);
// do your operation...
}
Upvotes: 1