Comparing version 0.0.8 to 0.1.0
@@ -1,2 +0,36 @@ | ||
exports.toCSV = function(args, callback) { | ||
var Readable = require("stream").Readable | ||
var exporter = function() { | ||
this.stream = null | ||
} | ||
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.createReadStream = function(opts) { | ||
this.stream = new Readable | ||
this._toCSV(opts) | ||
return this.stream | ||
} | ||
exporter.prototype.toCSV = function(args, callback) { | ||
var s = this.createReadStream(args) | ||
var result = '' | ||
var buffer = null; | ||
s.on('readable', function() { | ||
buffer = s.read() | ||
if (buffer !== null) | ||
result += buffer | ||
}) | ||
s.on('end', function() { | ||
callback(null, result) | ||
}) | ||
} | ||
exporter.prototype._toCSV = function(args) { | ||
var flatString = '' | ||
@@ -14,2 +48,4 @@ var ix = 0 | ||
flatString += '\r\n' | ||
this.stream.push(flatString) | ||
flatString = '' | ||
for(var jx = 0;jx<args.fields.length; jx++) { | ||
@@ -31,13 +67,14 @@ if (jx > 0) | ||
flatString += '\r\n' | ||
callback(null, flatString) | ||
this.stream.push(flatString) | ||
this.stream.push(null) | ||
} | ||
exports.getValue = function(data, arg) { | ||
exporter.prototype.getValue = function(data, arg) { | ||
var args = arg.split('.') | ||
if (args.length > 0) | ||
return getValue(data, args, 0) | ||
return this.getValueIx(data, args, 0) | ||
return data[args[0]]; | ||
} | ||
var getValue = function(data, args, ix) { | ||
exporter.prototype.getValueIx = function(data, args, ix) { | ||
var val = data[args[ix]] | ||
@@ -48,3 +85,3 @@ if (typeof val === 'undefined') | ||
if ((args.length-1) > ix) | ||
return getValue(val, args, ix+1); | ||
return this.getValueIx(val, args, ix+1); | ||
@@ -54,8 +91,2 @@ return val; | ||
exports.prepValue = function(arg, forceQuoted) { | ||
var quoted = forceQuoted || arg.indexOf('"') >= 0 || arg.indexOf(',') >= 0 | ||
var result = arg.replace(/\"/g,'""') | ||
if (quoted) | ||
result = '"' + result + '"' | ||
return result | ||
} | ||
module.exports = new exporter() |
{ | ||
"name": "json-csv", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"description": "Export a richly structured, JSON array to CSV", | ||
"homepage": "https://github.com/IWSLLC/json-csv", | ||
"author": "Nathan Bridgewater <nathan@integratedwebsystems.com> (http://integratedwebsystems.com/)", | ||
"author": "Nathan Bridgewater <nathan@iws.io> (http://iws.io/)", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "csv", |
Sorry, the diff of this file is not supported yet
13108
178