Skip to content

Commit

Permalink
Added skipped line count
Browse files Browse the repository at this point in the history
  • Loading branch information
Behcet committed Jul 14, 2016
1 parent 85d48cb commit dd51acf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
20 changes: 13 additions & 7 deletions lib/index.js

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

15 changes: 10 additions & 5 deletions src/index.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Options are documented [here](http://csv.adaltas.com/parse/).
# Counters
@lines = 0 # Number of lines encountered in the source dataset
@count = 0 # Number of records being processed
@skipped = 0 # Number of records skipped due to errors
# Constants
@is_int = /^(\-|\+)?([1-9]+[0-9]*)$/
# @is_float = /^(\-|\+)?([0-9]+(\.[0-9]+)([eE][0-9]+)?|Infinity)$/
Expand Down Expand Up @@ -166,13 +167,17 @@ Implementation of the [`stream.Transform` API][transform]
return
if not @line_length and line.length > 0
@line_length = if @options.columns then @options.columns.length else line.length
if line.length isnt @line_length and not (line.length is 1 and line[0] is '')
# Dont check column count with relax_column_count and on empty lines
if line.length isnt @line_length and not @options.relax_column_count and not (line.length is 1 and line[0] is '')
if @options.columns?
throw Error "Number of columns on line #{@lines} does not match header"
if not @options.relax_column_count
if @options.columns?
throw Error "Number of columns on line #{@lines} does not match header"
else
throw Error "Number of columns is inconsistent on line #{@lines}"
else
throw Error "Number of columns is inconsistent on line #{@lines}"
@count++
@skipped++
else
@count++
if @options.columns?
lineAsColumns = {}
for field, i in line
Expand Down
11 changes: 11 additions & 0 deletions test/columns.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ describe 'columns', ->
err.message.should.eql 'Number of columns is inconsistent on line 2'
process.nextTick next

it 'don\'t emit single error when column count is invalid on multiple lines and relax_column_count is true', (next) ->
parse """
1;2
1
3;4
5;6;7
"""
, delimiter: ';', relax_column_count: true, skip_empty_lines: true, (err, data) ->
return next err if err
process.nextTick next

it 'validate options column length on last line', (next) ->
parse """
1,2,3,x
Expand Down

0 comments on commit dd51acf

Please sign in to comment.