Skip to content

Commit

Permalink
Major updates. Breaking API.
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanb committed Aug 14, 2014
1 parent 5d186ce commit 2962140
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 233 deletions.
72 changes: 39 additions & 33 deletions README.md
Expand Up @@ -7,33 +7,37 @@ Simple CSV export module that can export a rich JSON array of objects to CSV.

Usage
-----

###Buffered###
```
var jsoncsv = require('json-csv')
jsoncsv.csvBuffered(data, options, callback)
```
var csv = require('json-csv')
csv.toCSV(args, callback)
- data : Array of JS objects
- callback : returns buffered result (see below)

```
var callback = function(err,csv) {
//csv contains string of converted data in CSV format.
}
```

Streaming
---------
###Streaming###
When using the streaming API, you'll need to also stream data into it.

```
var csv = require('json-csv')
var reader = csv.createReadStream(args)
reader.on('end', function() {
//all done
})
reader.pipe(something_writable)
var jsoncsv = require('json-csv')
var readable_source = <something readable that emits data row by row>
readable_source
.pipe(jsoncsv.csv(options))
.pipe(something_else_writable)
```


Arguments:
###Options###
```
{
//required: array of data
data : [],
//field definitions for CSV export
fields :
[
Expand All @@ -56,7 +60,7 @@ Example
Simple structure with basic CSV conversion.

```
var csv = require('../json-csv')
var jsoncsv = require('../json-csv')
var items = [
{
name : 'fred',
Expand All @@ -79,7 +83,7 @@ var items = [
amount : 1.02
}]
csv.toCSV({
jsoncsv.csvBuffered({
data : items,
fields : [
{
Expand All @@ -101,8 +105,7 @@ csv.toCSV({
});
//OR Streaming
csv.createReadStream({
data : items,
var options = {
fields : [
{
name : 'name',
Expand All @@ -117,11 +120,15 @@ csv.createReadStream({
name : 'amount',
label : 'Amount'
}
]}).pipe(process.stdout);
]}
var source = es.readArray(items)
source
.pipe(jsoncsv.csv(options))
.pipe(process.stdout)
```

Generates Output:
``` output
```
Name,Email,Amount
"fred",fred@somewhere,1.02
"jo",jo@somewhere,1.02
Expand All @@ -132,7 +139,7 @@ Name,Email,Amount
Here's a little more advanced sample that uses sub-structures and a filter for manipulating output for individual columns.

```
var csv = require('json-csv')
var jsoncsv = require('json-csv')
var items = [
{
contact : {
Expand All @@ -158,8 +165,7 @@ var items = [
}
];
csv.toCSV({
data : items,
jsoncsv.csvBuffered(items, {
fields : [
{
name : 'contact.company',
Expand Down Expand Up @@ -196,7 +202,7 @@ csv.toCSV({
```

Generates Output:
``` output
```
Company,Name,Email,Year,Level
"Widgets, LLC",John Doe,john@widgets.somewhere,2013,Unknown
"Sprockets, LLC",Jane Doe,jane@sprockets.somewhere,2013,Test 2
Expand All @@ -205,9 +211,9 @@ Company,Name,Email,Year,Level
Pipe to File (Using example above):
```
var fs = require("fs")
var out = fs.createWriteStream("output.csv", {encoding: 'utf8'})
var reader = csv.createReadStream({
data : items,
var es = require("event-stream")
var options = {
fields : [
{
name : 'contact.company',
Expand Down Expand Up @@ -236,10 +242,10 @@ var reader = csv.createReadStream({
}
}
}]
})
reader.on('end', function() {
console.log("done")
})
reader.pipe(out)
}
var out = fs.createWriteStream("output.csv", {encoding: 'utf8'})
var readable = es.readArray(items)
readable
.pipe(jsoncsv.csv(options))
.pipe(out)
```
108 changes: 108 additions & 0 deletions index.js
@@ -0,0 +1,108 @@
var es = require('event-stream');
var _ = require("lodash")
var concat = require('concat-stream')

var exporter = function(options) {
this.options = options || {}
}

exporter.prototype.csvBuffered = function(data, options, done) {
if (!data) throw new Error("No data provided.")

if(typeof options == 'function')
{
done = options
options = {}
}

es.readArray(data)
.pipe(this.csv(options))
.pipe(concat(function(buffer) {
done(null, buffer)
}))
}
exporter.prototype.csv = function(options) {
var writtenHeader = false
this.options = options || {}
var self = this;

return es.through(function write(data) {
if (!writtenHeader)
{
this.emit('data', self.getHeaderRow())
writtenHeader = true
}
this.emit('data', self.getBodyRow(data))
})
}

exporter.prototype.prepValue = function(arg, forceQuoted) {
var quoted = forceQuoted || arg.indexOf('"') >= 0 || arg.indexOf(',') >= 0
var result = arg.replace(/\"/g,'""')
if (quoted)
result = '"' + result + '"'
return result
}

exporter.prototype.getHeaderRow = function() {
var self = this
var header = _.reduce(this.options.fields, function(line, field) {
var label = field.label || field.field
if (line)
line += ','
line += self.prepValue(label)
return line
}, '', this)
header += '\r\n'
return header
}

exporter.prototype.getBodyRow = function(data) {
var self = this
var row = _.reduce(this.options.fields, function(line, field) {
var label = field.label || field.field
if (line)
line += ','

var val = self.getValue(data, field.name)
if (field.filter) {
val = field.filter(val)
}
if (typeof val !== 'undefined' && val !== null) {
var quoted = typeof field.quoted !== 'undefined' && field.quoted
line += self.prepValue(val.toString(), quoted)
}
return line
}, '', this)

row += '\r\n'
return row
}

exporter.prototype.getValue = function(data, arg) {
var args = arg.split('.')
if (args.length > 0)
return this.getValueIx(data, args, 0)
return data[args[0]];
}

exporter.prototype.getValueIx = function(data, args, ix) {
var val = data[args[ix]]
if (typeof val === 'undefined')
return ''

if ((args.length-1) > ix)
return this.getValueIx(val, args, ix+1);

return val;
}

module.exports = {
csv : function(options) {
return new exporter().csv(options)
}
,csvBuffered : function(data, options, done) {
return new exporter().csvBuffered(data,options, done)
}
,exporter : exporter
}
88 changes: 0 additions & 88 deletions json-csv.js

This file was deleted.

11 changes: 8 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "json-csv",
"version": "0.1.1",
"version": "1.0.0",
"description": "Export a richly structured, JSON array to CSV",
"homepage": "https://github.com/IWSLLC/json-csv",
"author": "Nathan Bridgewater <nathan@iws.io> (http://iws.io/)",
Expand All @@ -18,12 +18,13 @@
"type": "MIT"
}
],
"main": "./json-csv.js",
"main": "./index.js",
"engines": {
"node": "*"
},
"devDependencies": {
"coffee-script": "^1.7.1",
"concat-stream": "^1.4.6",
"mocha": "^1.20.1",
"should": "^4.0.4"
},
Expand All @@ -33,7 +34,11 @@
"directories": {
"test": "test"
},
"dependencies": {},
"dependencies": {
"concat-stream": "^1.4.6",
"event-stream": "^3.1.7",
"lodash": "^2.4.1"
},
"scripts": {
"test": "mocha"
},
Expand Down

0 comments on commit 2962140

Please sign in to comment.