fast-csv-delims

0.0.211 • Public • Published

build status

Fast-csv

Doug Martin is the original author. This is merely a temporary fork to support alternate cell delimiters. Please support Mr Martin and his awesome work by using his module.

This is a library is aimed at providing fast CSV parsing. It accomplishes this by not handling some of the more complex edge cases such as multi line rows. However it does support escaped values, embedded commas, double and single quotes.

Installation

npm install fast-csv

Usage

To parse a file.

var csv = require("fast-csv");
 
csv("my.csv")
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();

You may also parse a stream.

var stream = fs.createReadStream("my.csv");
 
csv(stream)
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

You may also designate a delimiter. Defaults to comma.

var stream = fs.createReadStream("my.csv");
 
csv(stream, {delimiter: '\t'})
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

If you expect the first line your csv to headers you may pass a headers option in. Setting the headers option will cause change each row to an object rather than an array.

var stream = fs.createReadStream("my.csv");
 
csv(stream, {headers : true})
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

You may alternatively pass an array of header names which must match the order of each column in the csv, otherwise the data columns will not match.

var stream = fs.createReadStream("my.csv");
 
csv(stream, {headers : ["firstName", "lastName", "address"]})
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

If your data may include empty rows, the sort Excel might include at the end of the file for instance, you can ignore these by including the ignoreEmpty option.

Any rows consisting of nothing but empty strings and/or commas will be skipped, without emitting a 'data' or 'error' event.

var stream = fs.createReadStream("my.csv");
 
csv(stream, {ignoreEmpty: true})
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

Validating

You can validate each row in the csv by providing a validate handler. If a row is invalid then a data-invalid event will be emitted with the row and the index.

var stream = fs.createReadStream("my.csv");
 
csv(stream, {headers : true})
 .validate(function(data){
     return data.age < 50; //all persons must be under the age of 50
 })
 .on("data-invalid", function(data){
     //do something with invalid row
 })
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

Transforming

You can transform data by providing in a transform function. What is returned from the transform function will be provided to validate and emitted as a row.

var stream = fs.createReadStream("my.csv");
 
csv(stream)
 .transform(function(data){
     return data.reverse(); //reverse each row.
 })
 .on("data", function(data){
     console.log(data):
 })
 .on("end", function(){
     console.log("done");
 })
 .parse();
 

License

MIT https://github.com/C2FO/fast-csv/raw/master/LICENSE

Meta

Readme

Keywords

none

Package Sidebar

Install

npm i fast-csv-delims

Weekly Downloads

0

Version

0.0.211

License

MIT

Last publish

Collaborators

  • fluffybunnies