Created
March 25, 2015 16:23
-
-
Save veltman/380548852bade95761df to your computer and use it in GitHub Desktop.
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 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); | |
}); |
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
// 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