Skip to content

Commit e3801c2

Browse files
committedDec 30, 2016
fix #128 ; add done event& hooks; removed parserMgr and parser; updated readme; use lodash.set now
1 parent 271901d commit e3801c2

18 files changed

+602
-301
lines changed
 

‎libs/core/Converter.js

+45-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var defParam=require("./defParam");
77
var csvline=require("./csvline");
88
var fileline=require("./fileline");
99
var dataToCSVLine=require("./dataToCSVLine");
10+
var fileLineToCSVLine=require("./fileLineToCSVLine");
1011
var linesToJson=require("./linesToJson");
1112
var CSVError=require("./CSVError");
1213
var workerMgr=require("./workerMgr");
@@ -41,7 +42,8 @@ function Converter(params,options) {
4142
this._csvTransf=null;
4243
this.finalResult=[];
4344
// this.on("data", function() {});
44-
this.on("error", function() {});
45+
this.on("error", emitDone(this));
46+
this.on("end", emitDone(this));
4547
this.initWorker();
4648
process.nextTick(function(){
4749
if (this._needEmitFinalResult === null){
@@ -69,6 +71,13 @@ function Converter(params,options) {
6971
return this;
7072
}
7173
util.inherits(Converter, Transform);
74+
function emitDone(conv){
75+
return function(err){
76+
process.nextTick(function(){
77+
conv.emit('done',err)
78+
})
79+
}
80+
}
7281
Converter.prototype._transform = function(data, encoding, cb) {
7382
if (this.param.toArrayString && this.started === false) {
7483
this.started = true;
@@ -94,21 +103,38 @@ Converter.prototype.setPartialData=function(d){
94103
}
95104
Converter.prototype.processData=function(data,cb){
96105
var params=this.param;
106+
var fileLines=fileline(data,this.param)
107+
if (this.preProcessLine && typeof this.preProcessLine === "function"){
108+
fileLines.lines=this._preProcessLines(fileLines.lines,this.lastIndex)
109+
}
97110
if (!params._headers){ //header is not inited. init header
98-
this.processHead(data,cb);
111+
this.processHead(fileLines,cb);
99112
}else{
100113
if (params.workerNum<=1){
101-
var lines=dataToCSVLine(data,params);
114+
var lines=fileLineToCSVLine(fileLines,params);
102115
this.setPartialData(lines.partial);
103116
var jsonArr=linesToJson(lines.lines,params,this.recordNum);
104117
this.processResult(jsonArr)
105118
this.lastIndex+=jsonArr.length;
106119
this.recordNum+=jsonArr.length;
107120
cb();
108121
}else{
109-
this.workerProcess(data,cb);
122+
this.workerProcess(fileLines,cb);
123+
}
124+
}
125+
}
126+
Converter.prototype._preProcessLines=function(lines,startIdx){
127+
var rtn=[]
128+
for (var i=0;i<lines.length;i++){
129+
var result=this.preProcessLine(lines[i],startIdx+i+1)
130+
if (typeof result ==="string"){
131+
rtn.push(result)
132+
}else{
133+
rtn.push(lines[i])
134+
this.emit("error",new Error("preProcessLine should return a string but got: "+JSON.stringify(result)))
110135
}
111136
}
137+
return rtn
112138
}
113139
Converter.prototype.initWorker=function(){
114140
var workerNum=this.param.workerNum-1;
@@ -117,14 +143,22 @@ Converter.prototype.initWorker=function(){
117143
this.workerMgr.initWorker(workerNum,this.param);
118144
}
119145
}
146+
Converter.prototype.preRawData=function(func){
147+
this.preProcessRaw=func;
148+
return this;
149+
}
150+
Converter.prototype.preFileLine=function(func){
151+
this.preProcessLine=func;
152+
return this;
153+
}
120154
/**
121155
* workerpRocess does not support embeded multiple lines.
122156
*/
123157

124-
Converter.prototype.workerProcess=function(data,cb){
158+
Converter.prototype.workerProcess=function(fileLine,cb){
125159
var self=this;
126-
var line=fileline(data,this.param)
127-
var eol=this.getEol(data)
160+
var line=fileLine
161+
var eol=this.getEol()
128162
this.setPartialData(line.partial)
129163
this.workerMgr.sendWorker(line.lines.join(eol)+eol,this.lastIndex,cb,function(results,lastIndex){
130164
var cur=self.sequenceBuffer[0];
@@ -154,10 +188,10 @@ Converter.prototype.workerProcess=function(data,cb){
154188
});
155189
this.lastIndex+=line.lines.length;
156190
}
157-
Converter.prototype.processHead=function(data,cb){
191+
Converter.prototype.processHead=function(fileLine,cb){
158192
var params=this.param;
159193
if (!params._headers){ //header is not inited. init header
160-
var lines=dataToCSVLine(data,params);
194+
var lines=fileLineToCSVLine(fileLine,params);
161195
this.setPartialData(lines.partial);
162196
if (params.noheader){
163197
if (params.headers){
@@ -198,6 +232,7 @@ Converter.prototype.processResult=function(result){
198232
// this.lastIndex+=result.length;
199233
// cb();
200234
}
235+
201236
Converter.prototype.emitResult=function(r){
202237
var index=r.index;
203238
var row=r.row;
@@ -320,7 +355,7 @@ Converter.prototype.getEol = function(data) {
320355
this.param.eol=eol;
321356
}
322357

323-
return this.param.eol;
358+
return this.param.eol || eol;
324359
};
325360
Converter.prototype.fromFile = function(filePath, cb) {
326361
var fs = require('fs');

‎libs/core/defParam.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ module.exports=function(params){
1414
flatKeys: false, // Don't interpret dots and square brackets in header fields as nested object or array identifiers at all.
1515
maxRowLength: 0, //the max character a csv row could have. 0 means infinite. If max number exceeded, parser will emit "error" of "row_exceed". if a possibly corrupted csv data provided, give it a number like 65535 so the parser wont consume memory. default: 0
1616
checkColumn: false, //whether check column number of a row is the same as headers. If column number mismatched headers number, an error of "mismatched_column" will be emitted.. default: false
17-
escape:'"' //escape char for quoted column
17+
escape:'"', //escape char for quoted column
18+
19+
/**below are internal params */
20+
_headerType:[],
21+
_headerTitle:[],
22+
_headerFlag:[],
23+
_headers:null
1824
};
1925
if (!params){
2026
params={};

‎libs/core/fileLineToCSVLine.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var csvline=require("./csvline");
2+
/**
3+
* Convert data chunk to csv lines with cols
4+
* @param {[type]} data [description]
5+
* @param {[type]} params [description]
6+
* @return {[type]} {lines:[[col1,col2,col3]],partial:String}
7+
*/
8+
module.exports=function(fileLine,params){
9+
var lines=fileLine.lines;
10+
var csvLines=csvline(lines,params);
11+
return {
12+
lines:csvLines.lines,
13+
partial:csvLines.partial+fileLine.partial
14+
}
15+
}

‎libs/core/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports=constructor;
22
module.exports.Converter = require("./Converter.js");
3-
module.exports.Parser = require("./parser.js");
4-
module.exports.parserMgr = require("./parserMgr.js");
3+
// module.exports.Parser = require("./parser.js");
4+
// module.exports.parserMgr = require("./parserMgr.js");
55

66

77
function constructor(param,options){

0 commit comments

Comments
 (0)
Failed to load comments.