Skip to content

Commit

Permalink
rowDelimiter: simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jan 9, 2017
1 parent ac1ace1 commit 522d57f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 72 deletions.
93 changes: 40 additions & 53 deletions lib/index.js

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

21 changes: 4 additions & 17 deletions src/index.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,8 @@ Options are documented [here](http://csv.adaltas.com/parse/).
@options[k] = v
stream.Transform.call @, @options
# adding backwards compatibility
if @options.rowDelimiter?
if typeof @options.rowDelimiter is "string"
@options.rowDelimiter = [@options.rowDelimiter]
@receivedRowDelimiterAsString = true
else if @options.rowDelimiter.constructor is Array
@options.rowDelimiter = @options.rowDelimiter
@receivedRowDelimiterAsString = false
else
@options.rowDelimiter = null
@options.rowDelimiter ?= null
@options.rowDelimiter = [@options.rowDelimiter] if typeof @options.rowDelimiter is "string"
@options.delimiter ?= ','
@options.quote ?= '"'
@options.escape ?= '"'
Expand Down Expand Up @@ -417,10 +410,7 @@ Implementation of the [`stream.Transform` API][transform]
if not @commenting and @field.length > @options.max_limit_on_data_read
throw Error "Delimiter not found in the file #{JSON.stringify(@options.delimiter)}"
if not @commenting and @line.length > @options.max_limit_on_data_read
if @receivedRowDelimiterAsString
throw Error "Row delimiter(s) not found in the file #{JSON.stringify(@options.rowDelimiter[0])}"
else
throw Error "Row delimiter(s) not found in the file #{JSON.stringify(@options.rowDelimiter)}"
throw Error "Row delimiter not found in the file #{JSON.stringify @options.rowDelimiter}"
# Flush remaining fields and lines
if end
rtrimed = false
Expand All @@ -435,10 +425,7 @@ Implementation of the [`stream.Transform` API][transform]
if l is 0
@lines++
if @line.length > @options.max_limit_on_data_read
if @receivedRowDelimiterAsString
throw Error "Row delimiter(s) not found in the file #{JSON.stringify(@options.rowDelimiter[0])}"
else
throw Error "Row delimiter(s) not found in the file #{JSON.stringify(@options.rowDelimiter)}"
throw Error "Row delimiter not found in the file #{JSON.stringify @options.rowDelimiter}"
# Store un-parsed chars for next call
@buf = ''
while i < l
Expand Down
4 changes: 2 additions & 2 deletions test/options.rowDelimiter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe 'rowDelimiter', ->
a,b,c
a,b,c
""", delimiter: ',', rowDelimiter: '\t', max_limit_on_data_read: 10, (err, data) ->
err.message.should.eql 'Row delimiter(s) not found in the file "\\t"'
err.message.should.eql 'Row delimiter not found in the file ["\\t"]'
should(data).not.be.ok()
next()

Expand All @@ -205,6 +205,6 @@ describe 'rowDelimiter', ->
a,b,c
a,b,c
""", delimiter: ',', rowDelimiter: ['\t'], max_limit_on_data_read: 10, (err, data) ->
err.message.should.eql 'Row delimiter(s) not found in the file ["\\t"]'
err.message.should.eql 'Row delimiter not found in the file ["\\t"]'
should(data).not.be.ok()
next()

0 comments on commit 522d57f

Please sign in to comment.