File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ function Converter(params) {
4242 this . pipe ( this . resultObject ) ; // it is important to have downstream for a transform otherwise it will stuck
4343 this . started = false ;
4444 this . recordNum = 0 ;
45+ this . lineNumber = 0 ;
4546 this . runningProcess = 0 ;
4647 //this._pipe(this.lineParser).pipe(this.processor);
4748 if ( this . param . fork ) {
@@ -244,14 +245,18 @@ Converter.prototype.toLines = function(data) {
244245 var eol = this . getEol ( data ) ;
245246 return data . split ( eol ) ;
246247}
247- var lineNumber = 0 ;
248+ Converter . prototype . preProcessLine = function ( line , lineNumber ) {
249+ return line ;
250+ }
248251Converter . prototype . toCSVLines = function ( fileLines , last ) {
249252 var recordLine = "" ;
250253 var lines = [ ] ;
251254 while ( fileLines . length > 1 ) {
252- lineNumber ++ ;
253- var line = fileLines . shift ( ) ;
254- lines = lines . concat ( this . _line ( line ) ) ;
255+ this . lineNumber ++ ;
256+ var line = this . preProcessLine ( fileLines . shift ( ) , this . lineNumber ) ;
257+ if ( line && line . length > 0 ) {
258+ lines = lines . concat ( this . _line ( line ) ) ;
259+ }
255260 }
256261 this . _lineBuffer = fileLines [ 0 ] ;
257262 if ( last && this . _csvLineBuffer . length > 0 ) {
Original file line number Diff line number Diff line change 1818 "email" : " t3dodson@gmail.com"
1919 }
2020 ],
21- "version" : " 0.5.13 " ,
21+ "version" : " 0.5.14 " ,
2222 "keywords" : [
2323 " csv" ,
2424 " csvtojson" ,
Original file line number Diff line number Diff line change @@ -338,6 +338,29 @@ conv.preProcessRaw=function(data,cb){
338338 }
339339}
340340```
341+ ## preProcessLine
342+ this hook is called when a file line is emitted. It is called with two parameters ` fileLineData,lineNumber ` . The ` lineNumber ` is starting from 1.
343+ ``` js
344+ /*
345+ CSV data:
346+ a,b,c,d,e
347+ 12,e3,fb,w2,dd
348+ */
349+
350+ var conv= new Converter ();
351+ conv .preProcessLine = function (line ,lineNumber ){
352+ // only change 12 to 23 for line 2
353+ if (lineNumber === 2 ){
354+ line= line .replace (" 12" ," 23" );
355+ }
356+ return line;
357+ }
358+ conv .fromString (csv,function (err ,json ){
359+ // json:{a:23 ....}
360+ })
361+ ```
362+ Notice that preProcessLine does not support async changes not like preProcessRaw hook.
363+
341364
342365# Events
343366
Original file line number Diff line number Diff line change @@ -177,7 +177,7 @@ describe("CSV Converter", function() {
177177 } ) ;
178178 rs . pipe ( conv ) ;
179179 } )
180- it ( "should pre process data in the line" , function ( done ) {
180+ it ( "should pre process raw data in the line" , function ( done ) {
181181 var testData = __dirname + "/data/quoteTolerant" ;
182182 var rs = fs . createReadStream ( testData ) ;
183183 var conv = new Converter ( ) ;
@@ -191,6 +191,22 @@ describe("CSV Converter", function() {
191191 } ) ;
192192 rs . pipe ( conv ) ;
193193 } )
194+ it ( "should pre process by line in the line" , function ( done ) {
195+ var testData = __dirname + "/data/quoteTolerant" ;
196+ var rs = fs . createReadStream ( testData ) ;
197+ var conv = new Converter ( ) ;
198+ conv . preProcessLine = function ( line , lineNumber ) {
199+ if ( lineNumber === 2 ) {
200+ line = line . replace ( '32"' , '32""' ) ;
201+ }
202+ return line ;
203+ }
204+ conv . on ( "end_parsed" , function ( res ) {
205+ assert ( res [ 0 ] . Description . indexOf ( '32"' ) > - 1 ) ;
206+ done ( ) ;
207+ } ) ;
208+ rs . pipe ( conv ) ;
209+ } )
194210 // it ("should convert big csv",function(done){
195211 // // var rs=fs.createReadStream(__dirname+"/data/large-csv-sample.csv");
196212 // var rs=fs.createReadStream("/Users/kxiang/tmp/csvdata");
You can’t perform that action at this time.
0 commit comments