Skip to content

Commit

Permalink
Skip UTF BOM from first data event on UTF-8 decoded stream
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Oct 18, 2012
1 parent 5d6e262 commit 2127daa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/from.js

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

6 changes: 5 additions & 1 deletion src/from.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ module.exports = (csv) ->
###
from.stream = (stream, options) ->
@options options
first = true
stream.on 'data', (data) ->
if csv.writable
if false is csv.write data.toString()
strip = first and typeof data is 'string' and stream.encoding is 'utf8' and 0xFEFF is data.charCodeAt 0
string = if strip then data.substring 1 else data.toString()
if false is csv.write string
stream.pause()
first = false
stream.on 'error', (e) ->
csv.error e
stream.on 'end', ->
Expand Down
11 changes: 11 additions & 0 deletions test/from.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ describe 'from', ->
.on 'end', ->
next()

describe 'path', ->

it 'should strip UTF-8 BOM', (next) ->
csv()
.from.path("#{__dirname}/from/file_bom.csv")
.on 'record', (record) ->
record.length.should.eql 5
record.should.eql ['1','2','3','4','5']
.on 'end', ->
next()

describe 'stream', ->

it 'should be able to pause', (next) ->
Expand Down
1 change: 1 addition & 0 deletions test/from/file_bom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"1","2","3","4","5"

0 comments on commit 2127daa

Please sign in to comment.