Giter Club home page Giter Club logo

fast-csv's Introduction

build status #Fast-csv

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();

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();

###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

fast-csv's People

Contributors

doug-martin avatar shane-walker avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.