Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
Ignore future records when to is reached
Browse files Browse the repository at this point in the history
Adds a property to ignore additional records once the to index is
reached. Futher lines including those with errors will not be parsed.
  • Loading branch information
khorwood authored and wdavidw committed Jun 6, 2018
1 parent 1f8a5c9 commit 82b1390
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
16 changes: 8 additions & 8 deletions lib/es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand All @@ -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;
};

Expand Down
16 changes: 8 additions & 8 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions src/index.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) ->
Expand Down
6 changes: 3 additions & 3 deletions test/options.to.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
Expand Down

0 comments on commit 82b1390

Please sign in to comment.