Comparing version 0.0.3 to 0.0.4
@@ -24,4 +24,4 @@ exports.toCSV = function(args, callback) { | ||
} | ||
flatString += this.prepValue(val.toString()) | ||
var quoted = typeof field.quoted !== 'undefined' && field.quoted | ||
flatString += this.prepValue(val.toString(), quoted) | ||
} | ||
@@ -52,4 +52,4 @@ } | ||
exports.prepValue = function(arg) { | ||
var quoted = arg.indexOf('"') >= 0 || arg.indexOf(',') >= 0 | ||
exports.prepValue = function(arg, forceQuoted) { | ||
var quoted = forceQuoted || arg.indexOf('"') >= 0 || arg.indexOf(',') >= 0 | ||
var result = arg.replace('"','""') | ||
@@ -56,0 +56,0 @@ if (quoted) |
{ | ||
"name": "json-csv", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Export a richly structured, JSON array to CSV", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/cynaptik/json-csv", |
@@ -6,3 +6,36 @@ json-csv | ||
Usage | ||
----- | ||
``` | ||
var csv = require('json-csv') | ||
csv.toCSV(args, callback) | ||
var callback = function(err,csv) { | ||
//csv contains string of converted data in CSV format. | ||
} | ||
``` | ||
Arguments: | ||
``` | ||
{ | ||
//required: array of data | ||
data : [], | ||
//field definitions for CSV export | ||
fields : | ||
[ | ||
{ | ||
//required: field name for source value | ||
name : 'string', | ||
//required: column label for CSV header | ||
label : 'string', | ||
//optional: filter to tranfsorm value before exporting | ||
filter : function(value) { return value; } | ||
} | ||
] | ||
} | ||
``` | ||
Example | ||
@@ -41,3 +74,4 @@ ------- | ||
name : 'name', | ||
label : 'Name' | ||
label : 'Name', | ||
quoted : true | ||
}, | ||
@@ -61,4 +95,4 @@ { | ||
Name,Email,Amount | ||
fred,fred@somewhere,1.02 | ||
jo,jo@somewhere,1.02 | ||
"fred",fred@somewhere,1.02 | ||
"jo",jo@somewhere,1.02 | ||
"jo with a comma,",jo@somewhere,1.02 | ||
@@ -65,0 +99,0 @@ "jo with a quote""",jo@somewhere,1.02 |
@@ -29,3 +29,4 @@ var csv = require('../json-csv') | ||
name : 'name', | ||
label : 'Name' | ||
label : 'Name', | ||
quoted : true | ||
}, | ||
@@ -32,0 +33,0 @@ { |
Sorry, the diff of this file is not supported yet
9690
151
170