Skip to content

Commit 856d093

Browse files
committed
add stream support; change version syntax
1 parent 4dbe119 commit 856d093

7 files changed

Lines changed: 72 additions & 19 deletions

File tree

bin/csvtojson.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function csvtojson() {
6767
process.exit(1);
6868
})
6969
is.pipe(conv).pipe(process.stdout);
70+
// is.pipe(conv);
7071
}
7172

7273
function run(cmd, options) {

libs/core/Converter.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ var Worker = require("./Worker.js");
99
var utils = require("./utils.js");
1010
var async = require("async");
1111

12-
function Converter(params) {
13-
Transform.call(this);
12+
function Converter(params,options) {
13+
Transform.call(this,options);
1414
var _param = {
1515
constructResult: true, //set to false to not construct result in memory. suitable for big csv data
1616
delimiter: ',', // change the delimiter of csv columns. It is able to use an array to specify potencial delimiters. e.g. [",","|",";"]
@@ -37,7 +37,9 @@ function Converter(params) {
3737
console.warn("Parameter should be a JSON object like {'constructResult':false}");
3838
_param.constructResult = params;
3939
}
40+
this._options=options || {};
4041
this.param = _param;
42+
this.param._options=this._options;
4143
this.resultObject = new Result(this);
4244
this.pipe(this.resultObject); // it is important to have downstream for a transform otherwise it will stuck
4345
this.started = false;
@@ -184,7 +186,11 @@ Converter.prototype.flushBuffer = function() {
184186
if (this.param.toArrayString && this.recordNum > 0) {
185187
this.push("," + eol);
186188
}
187-
this.push(resultJSONStr, "utf8");
189+
if (this._options && this._options.objectMode){
190+
this.push(resultRow);
191+
}else{
192+
this.push(resultJSONStr, "utf8");
193+
}
188194
this.recordNum++;
189195
}
190196
this.checkAndFlush();

libs/core/Result.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,40 @@ var Writable = require("stream").Writable;
22
var util = require("util");
33
var eol=require("os").EOL;
44
function Result(csvParser) {
5-
Writable.call(this);
5+
Writable.call(this,csvParser._options);
6+
this._option=csvParser._options || {};
67
this.parser = csvParser;
78
this.param = csvParser.param;
8-
this.buffer = this.param.toArrayString?"":"["+eol;
9+
this.buffer =this._option.objectMode?[]:"["+eol;
910
this.started = false;
1011
var self = this;
1112
this.parser.on("end", function() {
12-
if (!self.param.toArrayString){
13+
if (typeof self.buffer === "string"){
1314
self.buffer += eol+ "]";
1415
}
1516
});
17+
this._write=this._option.objectMode?_writeObject:_writeBuffer;
1618
}
1719
util.inherits(Result, Writable);
18-
Result.prototype._write = function(data, encoding, cb) {
20+
21+
Result.prototype.getBuffer = function() {
22+
return typeof this.buffer ==="string"?JSON.parse(this.buffer):this.buffer;
23+
};
24+
25+
Result.prototype.disableConstruct = function() {
26+
this._write = function(d, e, cb) {
27+
// console.log(typeof d,d);
28+
cb(); //do nothing just dropit
29+
};
30+
};
31+
32+
33+
function _writeObject (data, encoding, cb) {
34+
this.buffer.push(data);
35+
cb();
36+
};
37+
38+
function _writeBuffer(data, encoding, cb) {
1939
if (encoding === "buffer") {
2040
encoding = "utf8";
2141
}
@@ -32,15 +52,4 @@ Result.prototype._write = function(data, encoding, cb) {
3252
cb();
3353
};
3454

35-
Result.prototype.getBuffer = function() {
36-
// console.log(this.buffer);
37-
return JSON.parse(this.buffer);
38-
};
39-
40-
Result.prototype.disableConstruct = function() {
41-
this._write = function(d, e, cb) {
42-
cb(); //do nothing just dropit
43-
};
44-
};
45-
4655
module.exports = Result;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"email": "t3dodson@gmail.com"
1919
}
2020
],
21-
"version": "0.5.14",
21+
"version": "1.0.0",
2222
"keywords": [
2323
"csv",
2424
"csvtojson",

readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ All you need nodejs csv to json converter.
3939
* [Header Configuration](#header-configuration)
4040
* [Error Handling](#error-handling)
4141
* [Customised Parser](#parser)
42+
* [Stream Options](#stream-options)
4243
* [Change Log](#change-log)
4344

4445
GitHub: https://github.com/Keyang/node-csvtojson
@@ -781,11 +782,26 @@ The parameter of Parse function is a JSON object. It contains following fields:
781782
]
782783
}
783784
```
785+
#Stream Options
786+
Since version 1.0.0, the Converter constructor takes stream options as second parameter.
784787

788+
```js
789+
const conv=new Converter(params,{
790+
objectMode:true, // stream down JSON object instead of JSON array
791+
highWaterMark:65535 //Buffer level
792+
})
793+
794+
```
795+
796+
See more detailed information [here](https://nodejs.org/api/stream.html#stream_class_stream_transform).
785797

786798

787799
#Change Log
788800

801+
## 1.0.0
802+
* Add [Stream Options](#stream-options)
803+
* Change version syntax to follow x.y.z
804+
789805
## 0.5.12
790806
* Added support for scientific notation number support (#100)
791807
* Added "off" option to quote parameter
@@ -886,3 +902,4 @@ csvConverter.fromString(csvString, callback);
886902
The callback function above is optional. see [Parse String](#parse-string).
887903

888904
After version 0.3, csvtojson requires node 0.10 and above.
905+

test/data/dataWithMultipleLineRow

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
aa,bb
2+
ss,"12345
3+
6789,abcde""
4+
ddee"

test/testCSVConverter2.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,22 @@ describe("CSV Converter", function() {
207207
});
208208
rs.pipe(conv);
209209
})
210+
it ("should support object mode",function(done){
211+
var testData = __dirname + "/data/complexJSONCSV";
212+
var rs = fs.createReadStream(testData);
213+
var conv=new Converter({},{
214+
objectMode:true
215+
});
216+
conv.on("data",function(d){
217+
assert(typeof d === "object");
218+
});
219+
conv.on("end_parsed",function(res){
220+
assert(res);
221+
assert(res.length>0);
222+
done();
223+
})
224+
rs.pipe(conv);
225+
})
210226
// it ("should convert big csv",function(done){
211227
// // var rs=fs.createReadStream(__dirname+"/data/large-csv-sample.csv");
212228
// var rs=fs.createReadStream("/Users/kxiang/tmp/csvdata");

0 commit comments

Comments
 (0)