Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "readline", | ||
"version": "0.0.4", | ||
"description": "simple streaming readline module", | ||
"version": "0.0.5", | ||
"description": "Simple streaming readline module.", | ||
"main": "readline.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,58 +0,45 @@ | ||
var fs = require('fs'); | ||
var EventEmitter = require('events').EventEmitter; | ||
var util = require('util'); | ||
var fs = require('fs'), | ||
EventEmitter = require('events').EventEmitter, | ||
util = require('util'), | ||
newlines = [ | ||
13, // \r | ||
10 // \n | ||
]; | ||
var newlines = [ | ||
13, // \r | ||
10 // \n | ||
]; | ||
var readLine = module.exports = function(file, opts) { | ||
if (!(this instanceof readLine)) return new readLine(file); | ||
function readLine (file, opts){ | ||
EventEmitter.call(this); | ||
opts = opts || {}; | ||
var self = this, | ||
line = [], | ||
emit = function(line) { | ||
self.emit('line', new Buffer(line).toString()); | ||
}; | ||
if (!(this instanceof readLine)) return new readLine(file); | ||
EventEmitter.call(this); | ||
opts = opts || {}; | ||
var self = this; | ||
var readStream = fs.createReadStream(file); | ||
var line = []; | ||
readStream.on("open",function (fd){ | ||
self.emit('open',fd); | ||
}); | ||
readStream.on("data", function (data){ | ||
for(var i=0; i < data.length; i++){ | ||
if(newlines.indexOf(data[i]) !== -1){ | ||
if (line.length) { | ||
var tmpBuf = new Buffer(line); | ||
self.emit("line",tmpBuf.toString()); | ||
} | ||
line = []; | ||
}else{ | ||
line.push(data[i]); | ||
fs.createReadStream(file).on('open', function(fd) { | ||
self.emit('open', fd); | ||
}) | ||
.on('data', function(data) { | ||
for (var i = 0; i < data.length; i++) { | ||
if (0 <= newlines.indexOf(data[i])) { // Newline char was found. | ||
if (line.length) emit(line); | ||
line = []; // Empty buffer. | ||
} else { | ||
line.push(data[i]); // Buffer new line data. | ||
} | ||
} | ||
}); | ||
readStream.on("error", function (err){ | ||
self.emit("error",err); | ||
}); | ||
readStream.on("end", function (){ | ||
// emit last line if anything left over since EOF doesn't trigger it | ||
if (line.length) { | ||
var tmpBuf = new Buffer(line); | ||
self.emit("line",tmpBuf.toString()); | ||
} | ||
self.emit("end"); | ||
}); | ||
readStream.on("close", function (){ | ||
self.emit("close"); | ||
}); | ||
}) | ||
.on('error', function(err) { | ||
self.emit('error', err); | ||
}) | ||
.on('end', function() { | ||
// Emit last line if anything left over since EOF won't trigger it. | ||
if (line.length) emit(line); | ||
self.emit('end'); | ||
}) | ||
.on('close', function() { | ||
self.emit('close'); | ||
}); | ||
}; | ||
util.inherits(readLine, EventEmitter); | ||
module.exports = readLine; |
@@ -1,23 +0,28 @@ | ||
readline | ||
======== | ||
## _readline_ | ||
> Read a file line by line. | ||
read line module for nodejs. Read a file line by line. | ||
## Install | ||
TODO | ||
===== | ||
```sh | ||
npm install readline | ||
``` | ||
test different new line chars \r | ||
## What's this? | ||
Example | ||
======= | ||
Simple streaming readline module for NodeJS. Reads a file and buffer new lines emitting a _line_ event for each line. | ||
## Usage | ||
```js | ||
var readline = require('readline'), | ||
rl = readline('./somefile.txt'); | ||
rl.on('line', function(line) { | ||
// do something with the line of text | ||
}) | ||
.on('error', function(e) { | ||
// something went wrong | ||
}); | ||
``` | ||
var readline = require('readline'); | ||
var rl = readline("./somefile.txt"); | ||
rl.on("line", function (line){ | ||
//do something with the line of text | ||
}); | ||
rl.on('error', function (e){ | ||
//something went wrong | ||
}); | ||
``` | ||
## License | ||
BSD © [Craig Brookes](http://craigbrookes.com/) |
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
29
1954566
7
82