Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #131 #170

Merged
merged 1 commit into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/parser/parser_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,21 @@ extended(ParserStream).extend({
}
},

__removeBOM: function (data) {
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (data && typeof data == 'string' && data.charCodeAt(0) == '0xFEFF') {
return data.slice(1);
}
return data;
},

_transform: function (data, encoding, done) {
var lines = this.lines,
lineData = (lines + this.decoder.write(data)),
self = this;
if (lineData.length > 1) {
lineData = this.__removeBOM(lineData);
this._parse(lineData, true, function (err, lineData) {
if (err) {
done(err);
Expand Down
10 changes: 10 additions & 0 deletions test/assets/test28.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
first_name,last_name,email_address,address
First1,Last1,[email protected],"1 Street St, State ST, 88888"
First2,Last2,[email protected],"2 Street St, State ST, 88888"
First3,Last3,[email protected],"3 Street St, State ST, 88888"
First4,Last4,[email protected],"4 Street St, State ST, 88888"
First5,Last5,[email protected],"5 Street St, State ST, 88888"
First6,Last6,[email protected],"6 Street St, State ST, 88888"
First7,Last7,[email protected],"7 Street St, State ST, 88888"
First8,Last8,[email protected],"8 Street St, State ST, 88888"
First9,Last9,[email protected],"9 Street St, State ST, 88888"
19 changes: 18 additions & 1 deletion test/issues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,21 @@ it.describe("github issues", function (it) {
});

});
});

it.describe("#131", function (it) {

it.should("parse a csv with a UTF-8 Byte Order Mark", function (next) {
var actual = [];
csv
.fromPath(path.resolve(__dirname, "./assets/test28.csv"), {headers: true})
.on("data", function (data, index) {
actual.push(data);
}).
on("end", function (count) {
assert.deepEqual(actual[0]["first_name"], "First1");
assert.equal(count, actual.length);
next();
});
});
});
});