Comparing version 0.3.14 to 0.3.15
@@ -31,3 +31,3 @@ module.exports = csvAdv; | ||
this.parseRules = []; | ||
this.resultObject = new Result(); | ||
this.resultObject = new Result(this); | ||
this.pipe(this.resultObject); | ||
@@ -62,9 +62,2 @@ if (!this.param.constructResult) { | ||
} | ||
csvAdv.prototype._startInit = function() { | ||
if (this._isStarted === false) { | ||
this.push("[" + this.getEol()) | ||
this._isStarted = true | ||
} | ||
} | ||
csvAdv.prototype._transform = function(data, encoding, cb) { | ||
@@ -89,3 +82,2 @@ var self = this; | ||
} | ||
this._startInit() | ||
if (this.eol) { | ||
@@ -108,7 +100,5 @@ //console.log(this._buffer); | ||
csvAdv.prototype._flush = function(cb) { | ||
this._startInit(); // this is called in case input is empty | ||
if (this._buffer.length != 0) { //emit last line | ||
this.emit("record", this._buffer, this.rowIndex++, true); | ||
} | ||
this.push(this.getEol() + "]"); | ||
cb(); | ||
@@ -115,0 +105,0 @@ }; |
@@ -7,3 +7,2 @@ /** | ||
var self = this; | ||
var started = false; | ||
self.on("record", function(rowStr, index, lastLine) { | ||
@@ -52,9 +51,5 @@ var quote = self.param.quote; | ||
self.emit("record_parsed", resultRow, row, index - 1); | ||
if (started === true) { | ||
self.push("," + self.getEol()); | ||
} | ||
self.push(JSON.stringify(resultRow)); | ||
started = true; | ||
self.push(JSON.stringify(resultRow),"utf8"); | ||
} | ||
}); | ||
} |
@@ -1,26 +0,37 @@ | ||
module.exports=Result; | ||
var Writable=require("stream").Writable; | ||
var util=require("util"); | ||
module.exports = Result; | ||
var Writable = require("stream").Writable; | ||
var util = require("util"); | ||
function Result(){ | ||
function Result(csvParser) { | ||
Writable.call(this); | ||
this.buffer=""; | ||
this.parser = csvParser; | ||
this.buffer = "["+csvParser.getEol(); | ||
this.started = false; | ||
var self = this; | ||
this.parser.on("end", function() { | ||
self.buffer += self.parser.getEol() + "]"; | ||
}); | ||
} | ||
util.inherits(Result,Writable); | ||
Result.prototype._write=function(data,encoding,cb){ | ||
if (encoding=="buffer"){ | ||
encoding="utf8"; | ||
util.inherits(Result, Writable); | ||
Result.prototype._write = function(data, encoding, cb) { | ||
if (encoding == "buffer") { | ||
encoding = "utf8"; | ||
} | ||
this.buffer+=data.toString(encoding); | ||
if (this.started) { | ||
this.buffer += "," + this.parser.getEol(); | ||
} else { | ||
this.started = true; | ||
} | ||
this.buffer += data.toString(encoding); | ||
cb(); | ||
} | ||
Result.prototype.getBuffer=function(){ | ||
Result.prototype.getBuffer = function() { | ||
return JSON.parse(this.buffer); | ||
} | ||
Result.prototype.disableConstruct=function(){ | ||
this._write=function(d,e,cb){ | ||
cb();//do nothing just dropit | ||
Result.prototype.disableConstruct = function() { | ||
this._write = function(d, e, cb) { | ||
cb(); //do nothing just dropit | ||
} | ||
} |
@@ -15,3 +15,3 @@ { | ||
}], | ||
"version": "0.3.14", | ||
"version": "0.3.15", | ||
"keywords": [ | ||
@@ -18,0 +18,0 @@ "csv", |
@@ -111,7 +111,4 @@ #CSV2JSON | ||
#Demo Product | ||
I am hosting a free online service to convert CSV to JSON. The tool can be found from [here](http://keyangxiang.com/projects.html?_ctl=project&_act=csv2json). | ||
Just paste your CSV data and it will convert to JSON data for you. | ||
To write a demo app, simply use csvtojson web interface. Paste following code to index.js: | ||
The product simply uses csvtojson web interface. All the source code is like below: | ||
```js | ||
@@ -121,5 +118,11 @@ var server=require("csvtojson").interfaces.web; | ||
server.startWebServer({ | ||
"port":process.env.VCAP_APP_PORT || 8801 | ||
"port":8801 | ||
}); | ||
``` | ||
Then run the app: | ||
``` | ||
node ./index.js | ||
``` | ||
Now you can post any csv data to http://localhost:8801/parseCSV | ||
It uses HTTP Request as readable stream and HTTP Response as writable stream. | ||
@@ -162,3 +165,3 @@ | ||
* trim: Indicate if parser trim off spaces surrounding column content. e.g. " content " will be trimmed to "content". Default: true | ||
* checkType: This parameter turns on and off weather check field type. default is true. See [Field type](field-type) | ||
* checkType: This parameter turns on and off weather check field type. default is true. See [Field type](#field-type) | ||
@@ -165,0 +168,0 @@ # Parser |
@@ -227,2 +227,18 @@ var CSVAdv = require("../libs/core/csvConverter.js"); | ||
}); | ||
it ("should emit data event correctly",function(done){ | ||
var testData=__dirname+"/data/large-csv-sample.csv"; | ||
var rs=fs.createReadStream(testData); | ||
var csvConverter=new CSVAdv({ | ||
constructResult:false | ||
}); | ||
var count=0; | ||
csvConverter.on("data",function(d){ | ||
count++; | ||
}); | ||
csvConverter.on("end",function(){ | ||
assert(count===5290); | ||
done(); | ||
}); | ||
rs.pipe(csvConverter); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
137476
1002
602