Skip to content

Commit aa00468

Browse files
peteruithovenpeteruithoven
andauthored
Handle parsing utf-8 bom encoded files (#961)
Co-authored-by: peteruithoven <[email protected]>
1 parent c1cbe16 commit aa00468

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

papaparse.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ License: MIT
235235
}
236236
else if (typeof _input === 'string')
237237
{
238+
_input = stripBom(_input);
238239
if (_config.download)
239240
streamer = new NetworkStreamer(_config);
240241
else
@@ -248,6 +249,14 @@ License: MIT
248249
streamer = new FileStreamer(_config);
249250

250251
return streamer.stream(_input);
252+
253+
// Strip character from UTF-8 BOM encoded files that cause issue parsing the file
254+
function stripBom(string) {
255+
if (string.charCodeAt(0) === 0xfeff) {
256+
return string.slice(1);
257+
}
258+
return string;
259+
}
251260
}
252261

253262

tests/node-tests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Papa = require("../papaparse.js");
55
var fs = require('fs');
66
var assert = require('assert');
77
var longSampleRawCsv = fs.readFileSync(__dirname + '/long-sample.csv', 'utf8');
8+
var utf8BomSampleRawCsv = fs.readFileSync(__dirname + '/utf-8-bom-sample.csv', 'utf8');
89

910
function assertLongSampleParsedCorrectly(parsedCsv) {
1011
assert.equal(8, parsedCsv.data.length);
@@ -287,4 +288,14 @@ describe('PapaParse', function() {
287288
}
288289
});
289290
});
291+
292+
it('handles utf-8 BOM encoded files', function(done) {
293+
Papa.parse(utf8BomSampleRawCsv, {
294+
header: true,
295+
complete: function(parsedCsv) {
296+
assert.deepEqual(parsedCsv.data[0], { A: 'X', B: 'Y', C: 'Z' });
297+
done();
298+
}
299+
});
300+
});
290301
});

tests/utf-8-bom-sample.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A,B,C
2+
X,Y,Z

0 commit comments

Comments
 (0)