diff --git a/papaparse.js b/papaparse.js index 6c52906e..f5a1f8ff 100755 --- a/papaparse.js +++ b/papaparse.js @@ -235,6 +235,7 @@ License: MIT } else if (typeof _input === 'string') { + _input = stripBom(_input); if (_config.download) streamer = new NetworkStreamer(_config); else @@ -248,6 +249,14 @@ License: MIT streamer = new FileStreamer(_config); return streamer.stream(_input); + + // Strip character from UTF-8 BOM encoded files that cause issue parsing the file + function stripBom(string) { + if (string.charCodeAt(0) === 0xfeff) { + return string.slice(1); + } + return string; + } } diff --git a/tests/node-tests.js b/tests/node-tests.js index 9c57a225..6d012ec8 100644 --- a/tests/node-tests.js +++ b/tests/node-tests.js @@ -5,6 +5,7 @@ var Papa = require("../papaparse.js"); var fs = require('fs'); var assert = require('assert'); var longSampleRawCsv = fs.readFileSync(__dirname + '/long-sample.csv', 'utf8'); +var utf8BomSampleRawCsv = fs.readFileSync(__dirname + '/utf-8-bom-sample.csv', 'utf8'); function assertLongSampleParsedCorrectly(parsedCsv) { assert.equal(8, parsedCsv.data.length); @@ -287,4 +288,14 @@ describe('PapaParse', function() { } }); }); + + it('handles utf-8 BOM encoded files', function(done) { + Papa.parse(utf8BomSampleRawCsv, { + header: true, + complete: function(parsedCsv) { + assert.deepEqual(parsedCsv.data[0], { A: 'X', B: 'Y', C: 'Z' }); + done(); + } + }); + }); }); diff --git a/tests/utf-8-bom-sample.csv b/tests/utf-8-bom-sample.csv new file mode 100644 index 00000000..4f85ff06 --- /dev/null +++ b/tests/utf-8-bom-sample.csv @@ -0,0 +1,2 @@ +A,B,C +X,Y,Z