Skip to content
Browse files

Skip UTF BOM from first data event on UTF-8 decoded stream

  • Loading branch information...
1 parent 5d6e262 commit 2127daadbcf7f4fd5632545dfeccdf13949b466c @dougwilson dougwilson committed
Showing with 25 additions and 3 deletions.
  1. +8 −2 lib/from.js
  2. +5 −1 src/from.coffee
  3. +11 −0 test/from.coffee
  4. +1 −0 test/from/file_bom.csv
View
10 lib/from.js
@@ -193,13 +193,19 @@ module.exports = function(csv) {
*/
from.stream = function(stream, options) {
+ var first;
this.options(options);
+ first = true;
stream.on('data', function(data) {
+ var string, strip;
if (csv.writable) {
- if (false === csv.write(data.toString())) {
- return stream.pause();
+ strip = first && typeof data === 'string' && stream.encoding === 'utf8' && 0xFEFF === data.charCodeAt(0);
+ string = strip ? data.substring(1) : data.toString();
+ if (false === csv.write(string)) {
+ stream.pause();
}
}
+ return first = false;
});
stream.on('error', function(e) {
return csv.error(e);
View
6 src/from.coffee
@@ -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', ->
View
11 test/from.coffee
@@ -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) ->
View
1 test/from/file_bom.csv
@@ -0,0 +1 @@
+"1","2","3","4","5"

0 comments on commit 2127daa

Please sign in to comment.
Something went wrong with that request. Please try again.