Skip to content

Instantly share code, notes, and snippets.

@veltman
Created March 25, 2015 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save veltman/380548852bade95761df to your computer and use it in GitHub Desktop.
Save veltman/380548852bade95761df to your computer and use it in GitHub Desktop.
var parseCSV = require("./parse-csv.js");
// Call the function, pass a callback as the second arg
parseCSV("data.csv",function(err,rows){
// Did something go wrong?
if (err) {
throw new Error(err);
}
//rows is an array of rows
console.log(rows);
});
// Make an existing module do the hard part
var fs = require("fs"),
csv = require("dsv")(",");
// Export a function that expects two args: the filename and a callback
module.exports = function(filename,callback) {
// Read the file, asynchronously
fs.readFile(filename,"utf8",function(err,rawText){
// If something went wrong, call the callback with an error
if (err) {
return callback(err,null);
}
// Call the callback with the parsed data
return callback(err,csv.parse(rawText));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment