Last active
August 29, 2015 14:25
-
-
Save letladi/80a3f93a279c6c4fa58d to your computer and use it in GitHub Desktop.
Simple Node.js CSV Parser module that returns a collection of objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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