Skip to content

Instantly share code, notes, and snippets.

@letladi
Last active August 29, 2015 14:25
Show Gist options
  • Save letladi/80a3f93a279c6c4fa58d to your computer and use it in GitHub Desktop.
Save letladi/80a3f93a279c6c4fa58d to your computer and use it in GitHub Desktop.
Simple Node.js CSV Parser module that returns a collection of objects
var fs = require('fs');
function csvParser(filePath) {
fs.readFile(filePath, 'utf8', function(err, data) {
if (err) {
throw err;
} else {
var result = [];
var lines = data.split('\n');
var headings = lines[0].split(',');
lines.shift();
lines.forEach(function(line) {
var lineValues = line.split(',');
var obj = {};
headings.forEach(function(value, idx) {
obj[headings[idx]] = lineValues[idx] || "";
});
result.push(obj);
});
}
return result;
});
}
module.exports = csvParser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment