readfileline
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -0,1 +1,7 @@ | ||
/** | ||
* User: zenboss | ||
* GitHub: zenboss | ||
* Email: zenyes@gmail.com | ||
* Version: 2.0 | ||
*/ | ||
"use strict"; | ||
@@ -16,2 +22,4 @@ | ||
events.EventEmitter.call(self); | ||
self.readLineIsStop = false; | ||
self.chunk = ''; | ||
@@ -34,5 +42,6 @@ opt = opt||{}; | ||
var lines = theTurnData.split(lineEnding); | ||
self.emit('line',frs.chunk+lines.shift()); | ||
frs.chunk = lines.pop(); | ||
self.emit('line',self.chunk+lines.shift()); | ||
self.chunk = lines.pop(); | ||
for(var i in lines){ | ||
if(self.readLineIsStop)break; | ||
var row = lines[i]; | ||
@@ -42,7 +51,7 @@ self.emit('line',row); | ||
}else{ | ||
frs.chunk+=theTurnData; | ||
self.chunk+=theTurnData; | ||
} | ||
}); | ||
frs.on('end',function(chunk){ | ||
self.emit('line',frs.chunk); | ||
self.emit('line',self.chunk); | ||
self.emit('end'); | ||
@@ -63,22 +72,17 @@ }) | ||
}); | ||
} | ||
} | ||
readfileline.prototype.close = function(){ | ||
util.inherits(readfileline,events.EventEmitter); | ||
readfileline.prototype.close = readfileline.prototype.stop = function(){ | ||
var self = this; | ||
self.frs.close(); | ||
if(!self.readLineIsStop){ | ||
self.readLineIsStop = true; | ||
self.frs.close(); | ||
} | ||
return self; | ||
}; | ||
util.inherits(readfileline,events.EventEmitter); | ||
// var stream = require('stream'); | ||
// util.inherits(readfileline,stream.Readable); | ||
} | ||
var exports = module.exports = function(path,opt,lineCB,endCB){ | ||
if('function' == typeof opt){ | ||
endCB = lineCB; | ||
lineCB = opt; | ||
opt={}; | ||
} | ||
var exports = module.exports = function(path,lineCB,endCB,opt){ | ||
opt = opt || {}; | ||
@@ -90,16 +94,17 @@ lineCB = lineCB || function(){}; | ||
var rfl = new readfileline(path,opt); | ||
var lineNum = -1; | ||
var lineNum = 0; | ||
rfl.on('line',function(lineData){ | ||
lineNum++; | ||
lineCB.call(rfl,lineData,lineNum); | ||
lineCB.call(rfl,lineData,lineNum,rfl); | ||
}); | ||
rfl.on('error',function(err){ | ||
endCB.call(rfl,err,'error',lineNum+1); | ||
endCB.call(rfl,err,'error',lineNum,rfl); | ||
}); | ||
rfl.on('close',function(err){ | ||
endCB.call(rfl,err,'close',lineNum+1); | ||
endCB.call(rfl,err,'close',lineNum,rfl); | ||
}); | ||
rfl.on('end',function(err){ | ||
endCB.call(rfl,err,'end',lineNum+1); | ||
endCB.call(rfl,err,'end',lineNum,rfl); | ||
}); | ||
@@ -106,0 +111,0 @@ }; |
{ | ||
"name": "readfileline", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "read file line by line", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,3 +14,46 @@ # ReadFileLine | ||
``` javascript | ||
//TODO | ||
var fl = require('readfileline'); | ||
var fileContents = []; | ||
fl(__dirname+'/data.txt',function(lineData,lineNum){ | ||
fileContents.push(lineData); | ||
console.log(lineNum+':'+lineData+'\n'); | ||
},function(err,eventType,totalLineNum){ | ||
if(eventType=='end'){ | ||
//if file read all line to success, "close" event still will emit, so you need distinguish between they | ||
console.log('read line is done, total '+totalLineNum+' line(s)'); | ||
} | ||
if(eventType=='close'){ | ||
console.log('read line is closed, read to '+totalLineNum+' line(s)'); | ||
} | ||
if(eventType=='error'){ | ||
console.log('read file line has an error:'+err.message); | ||
} | ||
}); | ||
//-------- | ||
var fileContents2=[]; | ||
fl(__dirname+'/data.txt',function(lineData,lineNum){ | ||
var thisFL = this; | ||
if(lineNum>=5){ | ||
this.close(); //And also has a function is "this.stop()", but it still emit "close" event so you still need listen "close" event, so mustn't listen "stop" event | ||
return; | ||
} | ||
fileContents2.push(lineData); | ||
console.log(lineNum+':'+lineData+'\n'); | ||
},function(err,eventType,totalLineNum){ | ||
if(eventType=='end'){ | ||
//if file read all line to success, "close" event still will emit, so you need distinguish between they | ||
console.log('read line is done, total '+totalLineNum+' line(s)'); | ||
} | ||
if(eventType=='close'){ | ||
console.log('read line is closed, read to '+totalLineNum+' line(s)'); | ||
} | ||
if(eventType=='error'){ | ||
console.log('read file line has an error:'+err.message); | ||
} | ||
}); | ||
``` |
@@ -21,3 +21,3 @@ "use strict"; | ||
fl(__dirname+'/nofile.txt',function(lineNum,lineData){ | ||
fl(__dirname+'/nofile.txt',function(lineData,lineNum){ | ||
@@ -30,2 +30,18 @@ },function(err,eventType,lineNum){ | ||
} | ||
}); | ||
var fileContent2 = []; | ||
fl(__dirname+'/data.txt',function(lineData,lineNum,rl){ | ||
if(lineNum==5){ | ||
rl.close() | ||
return; | ||
} | ||
fileContent2.push(lineData); | ||
},function(err,eventType,lineNum){ | ||
assert.equal(fileContent2.length,4); | ||
assert.equal(lineNum,5); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19935
127
58