line-by-line
Advanced tools
Comparing version
@@ -11,2 +11,4 @@ /* | ||
var stream = require('stream'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var path = require('path'); | ||
@@ -22,16 +24,21 @@ var fs = require('fs'); | ||
this._filepath = path.normalize(filepath); | ||
this._encoding = options && options.encoding || 'utf8'; | ||
this._skipEmptyLines = options && options.skipEmptyLines || false; | ||
this._streamOptions = { encoding: this._encoding }; | ||
if (options && options.start) { | ||
this._streamOptions.start = options.start; | ||
if (filepath instanceof stream.Readable) { | ||
this._readStream = filepath; | ||
} | ||
else { | ||
this._readStream = null; | ||
this._filepath = path.normalize(filepath); | ||
this._streamOptions = { encoding: this._encoding }; | ||
if (options && options.end) { | ||
this._streamOptions.end = options.end; | ||
if (options && options.start) { | ||
this._streamOptions.start = options.start; | ||
} | ||
if (options && options.end) { | ||
this._streamOptions.end = options.end; | ||
} | ||
} | ||
this._skipEmptyLines = options && options.skipEmptyLines || false; | ||
this._readStream = null; | ||
this._lines = []; | ||
@@ -42,2 +49,3 @@ this._lineFragment = ''; | ||
this._ended = false; | ||
this.decoder = new StringDecoder(this._encoding); | ||
@@ -60,3 +68,4 @@ events.EventEmitter.call(this); | ||
var self = this, | ||
readStream = fs.createReadStream(this._filepath, this._streamOptions); | ||
readStream = this._readStream ? this._readStream : | ||
fs.createReadStream(this._filepath, this._streamOptions); | ||
@@ -73,3 +82,7 @@ readStream.on('error', function (err) { | ||
self._readStream.pause(); | ||
self._lines = self._lines.concat(data.split(/(?:\n|\r\n|\r)/g)); | ||
var dataAsString = data; | ||
if (data instanceof Buffer) { | ||
dataAsString = self.decoder.write(data); | ||
} | ||
self._lines = self._lines.concat(dataAsString.split(/(?:\n|\r\n|\r)/g)); | ||
@@ -109,3 +122,5 @@ self._lines[0] = self._lineFragment + self._lines[0]; | ||
} | ||
this.end(); | ||
if (!this._paused) { | ||
this.end(); | ||
} | ||
} else { | ||
@@ -156,2 +171,3 @@ this._readStream.resume(); | ||
this._end = true; | ||
this._lines = []; | ||
@@ -158,0 +174,0 @@ setImmediate(function () { |
@@ -6,3 +6,3 @@ { | ||
"description": "A NodeJS module that helps you reading large text files, line by line, without buffering the files into memory.", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"keywords": [ "line", "file", "reader", "fs" ], | ||
@@ -25,3 +25,3 @@ "homepage": "https://github.com/Osterjour/line-by-line", | ||
"engines": { | ||
"node": ">=0.8.0" | ||
"node": ">=4.0.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "licenses": [ |
@@ -57,3 +57,7 @@ [](https://travis-ci.org/Osterjour/line-by-line) | ||
}); | ||
Initialize with Stream: | ||
var LineByLineReader = require('line-by-line'), | ||
lr = new LineByLineReader(S3.getObject({ Bucket, Key }).createReadStream()); | ||
@@ -64,3 +68,3 @@ ## API: | ||
`path` specifies the file to read | ||
`path` specifies the file to read or Stream | ||
@@ -77,3 +81,3 @@ `options` is an object with the following defaults: | ||
You can also pass `start` and `end` position to read from file region: | ||
You can also pass `start` and `end` position in bytes to read from file region: | ||
@@ -80,0 +84,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82772
2.7%152
11.76%133
3.1%