Comparing version 1.2.1 to 1.3.0
{ | ||
"name": "readline", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "Simple streaming readline module.", | ||
@@ -11,3 +11,4 @@ "main": "readline.js", | ||
"devDependencies":{ | ||
"tap":"0.4.3" | ||
"tap":"0.4.3", | ||
"iconv-lite":"0.4.13" | ||
}, | ||
@@ -14,0 +15,0 @@ "repository": "git@github.com:maleck13/readline.git", |
@@ -11,2 +11,3 @@ var fs = require('fs'), | ||
opts.maxLineLength = opts.maxLineLength || 4096; // 4K | ||
opts.retainBuffer = !!opts.retainBuffer; //do not convert to String prior to invoking emit 'line' event | ||
var self = this, | ||
@@ -19,3 +20,4 @@ lineBuffer = new Buffer(opts.maxLineLength), | ||
try { | ||
self.emit('line', lineBuffer.slice(0, lineLength).toString(), lineCount, byteCount); | ||
var line = lineBuffer.slice(0, lineLength); | ||
self.emit('line', opts.retainBuffer? line : line.toString(), lineCount, byteCount); | ||
} catch (err) { | ||
@@ -27,8 +29,8 @@ self.emit('error', err); | ||
}; | ||
this.input = ('string' === typeof file) ? fs.createReadStream(file, opts) : file; | ||
this.input.on('open', function(fd) { | ||
self.emit('open', fd); | ||
this.input = ('string' === typeof file) ? fs.createReadStream(file, opts) : file; | ||
this.input.on('open', function(fd) { | ||
self.emit('open', fd); | ||
}) | ||
.on('data', function(data) { | ||
for (var i = 0; i < data.length; i++) { | ||
.on('data', function(data) { | ||
for (var i = 0; i < data.length; i++) { | ||
if (data[i] == 10 || data[i] == 13) { // Newline char was found. | ||
@@ -44,19 +46,19 @@ if (data[i] == 10) { | ||
byteCount++; | ||
} | ||
}) | ||
.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 (lineLength) { | ||
lineCount++; | ||
emit(lineCount, byteCount); | ||
} | ||
self.emit('end'); | ||
}) | ||
.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 (lineLength) { | ||
lineCount++; | ||
emit(lineCount, byteCount); | ||
} | ||
self.emit('end'); | ||
}) | ||
.on('close', function() { | ||
self.emit('close'); | ||
}); | ||
}; | ||
util.inherits(readLine, EventEmitter); |
@@ -23,5 +23,6 @@ ## _readline_ | ||
Simple streaming readline module for NodeJS. Reads a file and buffer new lines emitting a _line_ event for each line. | ||
Simple streaming readline module for NodeJS. Reads a file and buffers new lines emitting a _line_ event for each line. | ||
## Usage | ||
### Simple | ||
```js | ||
@@ -37,7 +38,23 @@ var readline = require('linebyline'), | ||
``` | ||
### ASCII file decoding | ||
As the underlying `fs.createReadStream` doesn't care about the specific ASCII encoding of the file, an alternative way to decode the file is by telling the `readline` library to retain buffer and then decoding it using a converter (e.g. [`iconv-lite`](https://www.npmjs.com/package/iconv-lite)). | ||
```js | ||
var readline = require('linebyline'), | ||
rl = readline('./file-in-win1251.txt', { | ||
retainBuffer: true //tell readline to retain buffer | ||
}); | ||
rl.on("line", function (data,linecount){ | ||
var line = iconv.decode(data, 'win1251'); | ||
// do something with the line of converted text | ||
}); | ||
``` | ||
##API | ||
## readfile(readingObject) | ||
## readLine(readingObject[, options]) | ||
### Params: | ||
* **** *readingObject* {file path or stream} | ||
* `readingObject` - file path or stream object | ||
* `options` can include: | ||
* `maxLineLength` - override the default 4K buffer size (lines longer than this will not be read) | ||
* `retainBuffer` - avoid converting to String prior to emitting 'line' event; will pass raw buffer with encoded data to the callback | ||
@@ -44,0 +61,0 @@ ### Return: |
@@ -14,3 +14,3 @@ var fs = require('fs'); | ||
}); | ||
}); | ||
@@ -118,1 +118,23 @@ | ||
}); | ||
test("test ascii file reading",function(t){ | ||
var iconv = require('iconv-lite'); | ||
var testFileValidationKeywords = { | ||
1: 'папка', | ||
3: 'телефон', | ||
11: 'электричество', | ||
14: 'дерево' | ||
}; | ||
var rl = readLine('./fixtures/file-in-win1251.txt', { | ||
retainBuffer: true | ||
}); | ||
rl.on("line", function (data,linecount){ | ||
var line = iconv.decode(data, 'win1251'); | ||
t.ok(!testFileValidationKeywords[linecount] || line.indexOf(testFileValidationKeywords[linecount]) > -1); | ||
}); | ||
rl.on("end",function (){ | ||
t.end(); | ||
}); | ||
}); |
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
178
67
1959414
2
8