diff --git a/lib/es5/index.js b/lib/es5/index.js index 3cf90f4..efbc2c5 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -301,6 +301,9 @@ Parser.prototype.__flush = function () { Parser.prototype.__push = function (line) { var call_column_udf, columns, err, field, i, j, len, lineAsColumns, record; + if (this.isEnded) { + return; + } if (this.options.skip_lines_with_empty_values && line.join('').trim() === '') { return; } @@ -375,14 +378,6 @@ Parser.prototype.__push = function (line) { if (this.count < this.options.from) { return; } - if (this.count > this.options.to) { - if (this.options.endStreamWithTo) { - this.push(null); - return; - } else { - return; - } - } if (this.options.raw) { this.push({ raw: this._.rawBuf, @@ -395,6 +390,11 @@ Parser.prototype.__push = function (line) { if (this.listenerCount('record')) { this.emit('record', record); } + // When to is reached set ingore any future calls + if (this.count >= this.options.to) { + this.isEnded = true; + return this.push(null); + } return null; }; diff --git a/lib/index.js b/lib/index.js index 89b04e7..b05fe9e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -289,6 +289,9 @@ Parser.prototype.__flush = function() { Parser.prototype.__push = function(line) { var call_column_udf, columns, err, field, i, j, len, lineAsColumns, record; + if (this.isEnded) { + return; + } if (this.options.skip_lines_with_empty_values && line.join('').trim() === '') { return; } @@ -356,14 +359,6 @@ Parser.prototype.__push = function(line) { if (this.count < this.options.from) { return; } - if (this.count > this.options.to) { - if (this.options.endStreamWithTo) { - this.push(null); - return; - } else { - return; - } - } if (this.options.raw) { this.push({ raw: this._.rawBuf, @@ -376,6 +371,11 @@ Parser.prototype.__push = function(line) { if (this.listenerCount('record')) { this.emit('record', record); } + // When to is reached set ingore any future calls + if (this.count >= this.options.to) { + this.isEnded = true; + return this.push(null); + } return null; }; diff --git a/src/index.coffee.md b/src/index.coffee.md index 5a6d419..bf0b145 100644 --- a/src/index.coffee.md +++ b/src/index.coffee.md @@ -187,6 +187,7 @@ Implementation of the [`stream.Transform` API][transform] return @__push @_.line if @_.line.length > 0 Parser.prototype.__push = (line) -> + return if @isEnded return if @options.skip_lines_with_empty_values and line.join('').trim() is '' record = null if @options.columns is true @@ -234,18 +235,16 @@ Implementation of the [`stream.Transform` API][transform] else record = line return if @count < @options.from - if @count > @options.to - if @options.endStreamWithTo - @push null - return - else - return if @options.raw @push { raw: @_.rawBuf, row: record } @_.rawBuf = '' else @push record @emit 'record', record if @listenerCount('record') + # When to is reached set ingore any future calls + if @count >= @options.to + @isEnded = true + return @push null null Parser.prototype.__write = (chars, end) -> diff --git a/test/options.to.coffee b/test/options.to.coffee index 7b05e0e..30873e0 100644 --- a/test/options.to.coffee +++ b/test/options.to.coffee @@ -31,12 +31,12 @@ describe 'options to', -> ] next() - it 'end stream when reach to', (next) -> + it 'end stream when to reached, further lines not parsed', (next) -> parse """ 1,2,3 4,5,6 - 7,8,9 - """, to: 2, endStreamWithTo: true, (err, data) -> + 7,8 + """, to: 2, (err, data) -> return next err if err data.should.eql [ [ '1','2','3' ]