Skip to content

Commit

Permalink
columns: detect column length and fix lines count
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 26, 2016
1 parent 73b5670 commit 1b94c63
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
35 changes: 19 additions & 16 deletions lib/index.js

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

18 changes: 9 additions & 9 deletions src/index.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Options are documented [here](http://csv.adaltas.com/parse/).
@options.rtrim ?= false
@options.auto_parse ?= false
@options.auto_parse_date ?= false
@options.relax ?= false
@options.skip_empty_lines ?= false
@options.max_limit_on_data_read ?= 128000
# Counters
Expand Down Expand Up @@ -156,20 +157,19 @@ Implementation of the [`stream.Transform` API][transform]
this.emit 'error', err
Parser.prototype.__push = (line) ->
if not @line_length and line.length > 0
@line_length = line.length
if line.length isnt @line_length
if @options.columns?
this.emit 'error', new Error "Number of columns on line #{@lines+1} does not match header"
else
this.emit 'error', new Error "Number of columns is inconsistent on line #{@lines+1}"
if @options.columns is true
@options.columns = line
return
else if typeof @options.columns is 'function'
@options.columns = @options.columns line
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
if @options.columns?
@emit 'error', Error "Number of columns on line #{@lines} does not match header"
else
@emit 'error', Error "Number of columns is inconsistent on line #{@lines+1}"
@count++
if @options.columns?
lineAsColumns = {}
Expand Down Expand Up @@ -278,7 +278,7 @@ Implementation of the [`stream.Transform` API][transform]
throw Error "Invalid opening quote at line #{@lines+1}"
# Otherwise, treat quote as a regular character
isRowDelimiter = (@options.rowDelimiter and chars.substr(i, @options.rowDelimiter.length) is @options.rowDelimiter)
@lines++ if isRowDelimiter
@lines++ if isRowDelimiter or (end and i is l - 1)
# Set the commenting flag
wasCommenting = false
if not @commenting and not @quoting and @options.comment and chars.substr(i, @options.comment.length) is @options.comment
Expand Down
20 changes: 15 additions & 5 deletions test/columns.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,22 @@ describe 'columns', ->
]
next()

it 'handles missing column if array', (next) ->
it 'validate options column length on first line', (next) ->
parse """
20322051544,1979,8.8017226E7,ABC,45,2000-01-01
28392898392,1974,8.8392926E7,23,2050-11-27
""", columns: ["FIELD_1", "FIELD_2", "FIELD_3", "FIELD_4", "FIELD_5", "FIELD_6"], (err, data) ->
err.message.should.match(/Number of columns on line \d.+ does not match header/)
1,2,3
4,5,6,x
7,8,9,x
""", columns: ["a", "b", "c", "d"], (err, data) ->
err.message.should.eql 'Number of columns on line 1 does not match header'
next()

it 'validate options column length on last line', (next) ->
parse """
1,2,3,x
4,5,6,x
7,8,9
""", columns: ["a", "b", "c", "d"], (err, data) ->
err.message.should.eql 'Number of columns on line 3 does not match header'
next()

it 'handles missing column if number of columns is inconsistent', (next) ->
Expand Down

0 comments on commit 1b94c63

Please sign in to comment.