@@ -39,28 +39,41 @@ function Converter(params,options) {
3939 this . _needJson = null ;
4040 this . _needEmitResult = null ;
4141 this . _needEmitFinalResult = null ;
42+ this . _needEmitJson = null ;
4243 this . _needPush = null ;
44+ this . _needEmitCsv = null ;
45+ this . _csvTransf = null ;
4346 this . finalResult = [ ] ;
4447 // this.on("data", function() {});
4548 this . on ( "error" , function ( ) { } ) ;
4649 this . initWorker ( ) ;
50+ process . nextTick ( function ( ) {
51+ if ( this . _needEmitFinalResult === null ) {
52+ this . _needEmitFinalResult = this . listeners ( "end_parsed" ) . length > 0
53+ }
54+ if ( this . _needEmitResult === null ) {
55+ this . _needEmitResult = this . listeners ( "record_parsed" ) . length > 0
56+ }
57+ if ( this . _needEmitJson === null ) {
58+ this . _needEmitJson = this . listeners ( "json" ) . length > 0
59+ }
60+ if ( this . _needEmitCsv === null ) {
61+ this . _needEmitCsv = this . listeners ( "csv" ) . length > 0
62+ }
63+ if ( this . _needJson === null ) {
64+ this . _needJson = this . _needEmitJson || this . _needEmitFinalResult || this . _needEmitResult || this . transform || this . _options . objectMode ;
65+ }
66+ if ( this . _needPush === null ) {
67+ this . _needPush = this . listeners ( "data" ) . length > 0 || this . listeners ( "readable" ) . length > 0
68+ // this._needPush=false;
69+ }
70+ this . param . _needParseJson = this . _needJson || this . _needPush ;
71+
72+ } . bind ( this ) )
4773 return this ;
4874}
4975util . inherits ( Converter , Transform ) ;
5076Converter . prototype . _transform = function ( data , encoding , cb ) {
51- if ( this . _needEmitFinalResult === null ) {
52- this . _needEmitFinalResult = this . listeners ( "end_parsed" ) . length > 0
53- }
54- if ( this . _needEmitResult === null ) {
55- this . _needEmitResult = this . listeners ( "record_parsed" ) . length > 0
56- }
57- if ( this . _needJson === null ) {
58- this . _needJson = this . _needEmitFinalResult || this . _needEmitResult || this . transform || this . _options . objectMode ;
59- }
60- if ( this . _needPush === null ) {
61- this . _needPush = this . listeners ( "data" ) . length > 0 || this . listeners ( "readable" ) . length > 0
62- // this._needPush=false;
63- }
6477 if ( this . param . toArrayString && this . started === false ) {
6578 this . started = true ;
6679 if ( this . _needPush ) {
@@ -206,11 +219,21 @@ Converter.prototype.emitResult=function(r){
206219 row = JSON . parse ( row )
207220 }
208221 }
209- if ( this . param . constructResult && this . _needEmitFinalResult ) {
210- this . finalResult . push ( resultJson )
211- }
212222 if ( this . transform && typeof this . transform === "function" ) {
213223 this . transform ( resultJson , row , index ) ;
224+ resultStr = null ;
225+ }
226+ if ( this . _needEmitJson ) {
227+ this . emit ( "json" , resultJson , index )
228+ }
229+ if ( this . _needEmitCsv ) {
230+ if ( typeof row === "string" ) {
231+ row = JSON . parse ( row )
232+ }
233+ this . emit ( "csv" , row , index )
234+ }
235+ if ( this . param . constructResult && this . _needEmitFinalResult ) {
236+ this . finalResult . push ( resultJson )
214237 }
215238 if ( this . _needEmitResult ) {
216239 this . emit ( "record_parsed" , resultJson , row , index ) ;
@@ -305,51 +328,65 @@ Converter.prototype.getEol = function(data) {
305328} ;
306329Converter . prototype . fromFile = function ( filePath , cb ) {
307330 var fs = require ( 'fs' ) ;
331+ var rs = null ;
332+ this . wrapCallback ( cb , function ( ) {
333+ if ( rs && rs . destroy ) {
334+ rs . destroy ( ) ;
335+ }
336+ } ) ;
308337 fs . exists ( filePath , function ( exist ) {
309338 if ( exist ) {
310- var rs = fs . createReadStream ( filePath ) ;
339+ rs = fs . createReadStream ( filePath ) ;
311340 rs . pipe ( this ) ;
312- this . wrapCallback ( cb , function ( ) {
313- rs . destroy ( ) ;
314- } ) ;
315341 } else {
316- cb ( new Error ( filePath + " cannot be found. ") ) ;
342+ this . emit ( 'error' , new Error ( "File not exist ") )
317343 }
318344 } . bind ( this ) ) ;
319345 return this ;
320346}
347+ Converter . prototype . fromStream = function ( readStream , cb ) {
348+ if ( cb && typeof cb === "function" ) {
349+ this . wrapCallback ( cb ) ;
350+ }
351+ process . nextTick ( function ( ) {
352+ readStream . pipe ( this ) ;
353+ } . bind ( this ) )
354+ return this ;
355+ }
356+ Converter . prototype . transf = function ( func ) {
357+ this . transform = func ;
358+ return this ;
359+ }
321360Converter . prototype . fromString = function ( csvString , cb ) {
322- var rs = new Readable ( ) ;
323- var offset = 0 ;
324361 if ( typeof csvString != "string" ) {
325362 return cb ( new Error ( "Passed CSV Data is not a string." ) ) ;
326363 }
327- rs . _read = function ( len ) {
328- // console.log(offset,len,csvString.length);
329- var sub = csvString . substr ( offset , len ) ;
330- this . push ( sub ) ;
331- offset += len ;
332- if ( offset >= csvString . length ) {
333- this . push ( null ) ;
334- }
335- } ;
336- rs . pipe ( this ) ;
337364 if ( cb && typeof cb === "function" ) {
338365 this . wrapCallback ( cb , function ( ) {
339- rs . pause ( ) ;
340366 } ) ;
341367 }
368+ process . nextTick ( function ( ) {
369+ this . end ( csvString )
370+ } . bind ( this ) )
342371 return this ;
343372} ;
344373Converter . prototype . wrapCallback = function ( cb , clean ) {
345- this . once ( "end_parsed" , function ( res ) {
346- if ( ! this . hasError ) {
347- cb ( null , res ) ;
348- }
349- } . bind ( this ) ) ;
374+
375+ if ( clean === undefined ) {
376+ clean = function ( ) { }
377+ }
378+ if ( cb && typeof cb === "function" ) {
379+ this . once ( "end_parsed" , function ( res ) {
380+ if ( ! this . hasError ) {
381+ cb ( null , res ) ;
382+ }
383+ } . bind ( this ) ) ;
384+ }
350385 this . once ( "error" , function ( err ) {
351386 this . hasError = true ;
352- cb ( err ) ;
387+ if ( cb && typeof cb === "function" ) {
388+ cb ( err ) ;
389+ }
353390 clean ( ) ;
354391 } . bind ( this ) ) ;
355392}
0 commit comments