Skip to content

Commit

Permalink
Merge pull request #46 from bibhas2/pr-readme-work
Browse files Browse the repository at this point in the history
Add event information and enhance the basic example.
  • Loading branch information
mafintosh committed Feb 18, 2016
2 parents ee1f605 + 264a9da commit b736bdb
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,29 @@ Simply instantiate `csv` and pump a csv file to it and get the rows out as objec

You can use `csv-parser` in the browser with [browserify](http://browserify.org/)

Let's say that you have a CSV file ``some-csv-file.csv`` like this:

```
NAME, AGE
Daffy Duck, 24
Bugs Bunny, 22
```

You can parse it like this:

``` js
var csv = require('csv-parser')
var fs = require('fs')

fs.createReadStream('some-csv-file.csv')
.pipe(csv())
.on('data', function(data) {
console.log('row', data)
console.log("Name: %s Age: %s",
data["NAME"], data["AGE"])
})
```

The data emitted is a normalized JSON object
The data emitted is a normalized JSON object. Each header is used as the property name of the object.

The csv constructor accepts the following options as well

Expand Down Expand Up @@ -69,6 +80,37 @@ var stream = csv({

If you do not specify the headers, csv-parser will take the first line of the csv and treat it like the headers

## Events
The following events are emitted during parsing.

### data
For each row parsed (except the header), this event is emitted. This is already discussed above.

### headers
After the header row is parsed this event is emitted. An array of header names is supplied as the payload.

```
fs.createReadStream('some-csv-file.csv')
.pipe(csv())
.on('headers', function(headerList) {
console.log("First header: %s", headerList[0])
})
```

### Other Readable Stream Events
The usual [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable) events are also emitted. Use the ``close`` event to detect the end of parsing.

```
fs.createReadStream('some-csv-file.csv')
.pipe(csv())
.on('data', function(data) {
//Process row
})
.on('end', function() {
//We are done
})
```

## Command line tool

There is also a command line tool available. It will convert csv to line delimited JSON.
Expand Down

0 comments on commit b736bdb

Please sign in to comment.