magic-csv
v7.0.2
Published
Robust CSV parser with format detection
Downloads
102
Readme
magic-csv
magic-csv is an automagic CSV parser designed to handle whatever you throw at it Usage
MagicCSV = require("magic-csv");
csv = new MagicCSV({trim: true});
// file example
csv.readFile("example.csv", function(err, stats) {
csv.getCols(); // ['First', 'Last', 'Pain']
csv.getRow(0); // ['Brian', 'Regan', '8']
csv.getObject(0); // {First: 'Brian', Last: 'Regan', Pain: '8'}
csv.getStats(); // stats object, detailing how the file was parsed
csv.getRowCount(); // same as stats.row_count
});
// objects example
var ob1 = {Name: 'Jimmy', Phone: 5552497, Gender: 'M'};
var ob2 = {Name: 'Justin', Phone: 5553546, Phone2: 5557951, Gender: 'M'};
csv.readObjects([ob1, ob2], function(err, stats) {
csv.getCols(); // ['Name', 'Phone', 'Phone2', 'Gender']
csv.getRow(0); // ['Jimmy', '5552497', '', 'M']
csv.getRow(1); // ['Justin', '5553546', '5557951', 'M']
csv.getCol('Phone'); // ['5552497', '5553546']
});
// raw example
csv.parse(str, function(err, stats) {
csv.getRow(-1); // last row
csv.getRows(); // all rows
csv.getObjects(); // all objects
csv.getColCount(); // same as stats.col_count
});
// write methods
csv.writeToStream(stream);
csv.writeToRes(res, 'out.csv'); // express response
csv.writeToFile('out.csv');