@@ -10,9 +10,7 @@ var utils = require("./utils.js");
1010var async = require ( "async" ) ;
1111
1212function Converter ( params ) {
13- Transform . call ( this , {
14- highWaterMark :1024
15- } ) ; //TODO what does this do? -->This calls the constructor of Transform and initialise anything the Transform needs.(like var initialisation)
13+ Transform . call ( this ) ;
1614 var _param = {
1715 constructResult : true , //set to false to not construct result in memory. suitable for big csv data
1816 delimiter : ',' , // change the delimiter of csv columns. It is able to use an array to specify potencial delimiters. e.g. [",","|",";"]
@@ -190,16 +188,25 @@ Converter.prototype.flushBuffer = function() {
190188 }
191189 this . checkAndFlush ( ) ;
192190}
193- var size = 0 ;
191+ Converter . prototype . preProcessRaw = function ( data , cb ) {
192+ cb ( data ) ;
193+ }
194194
195195Converter . prototype . _transformNoFork = function ( data , encoding , cb ) {
196- size += data . length ;
197196 if ( this . param . toArrayString && this . started === false ) {
198197 this . started = true ;
199198 this . push ( "[" + eol , "utf8" ) ;
200199 }
201- var lines = this . toCSVLines ( this . toLines ( data , encoding ) ) ; //lines of csv
202- this . processCSVLines ( lines , cb ) ;
200+ data = data . toString ( "utf8" ) ;
201+ var self = this ;
202+ this . preProcessRaw ( data , function ( d ) {
203+ if ( d && d . length > 0 ) {
204+ var lines = self . toCSVLines ( self . toLines ( d ) ) ; //lines of csv
205+ self . processCSVLines ( lines , cb ) ;
206+ } else {
207+ cb ( ) ;
208+ }
209+ } )
203210 // async.eachLimit(lines,1,function(line,scb){
204211 // this.push(line.data);
205212 // scb();
@@ -232,18 +239,17 @@ Converter.prototype.processCSVLines = function(csvLines, cb) {
232239 }
233240 } . bind ( this ) , cb ) ;
234241}
235- Converter . prototype . toLines = function ( data , encoding ) {
236- if ( encoding === "buffer" ) {
237- encoding = "utf8" ;
238- }
239- data = this . _lineBuffer + data . toString ( encoding ) ;
242+ Converter . prototype . toLines = function ( data ) {
243+ data = this . _lineBuffer + data ;
240244 var eol = this . getEol ( data ) ;
241245 return data . split ( eol ) ;
242246}
247+ var lineNumber = 0 ;
243248Converter . prototype . toCSVLines = function ( fileLines , last ) {
244249 var recordLine = "" ;
245250 var lines = [ ] ;
246251 while ( fileLines . length > 1 ) {
252+ lineNumber ++ ;
247253 var line = fileLines . shift ( ) ;
248254 lines = lines . concat ( this . _line ( line ) ) ;
249255 }
0 commit comments