From 0225ae27744063037d731ae63ca729bbc72b1c81 Mon Sep 17 00:00:00 2001 From: Ricardo Spear Date: Wed, 25 Oct 2023 17:12:51 -0500 Subject: [PATCH 1/2] strip BOM when streaming chunk --- papaparse.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/papaparse.js b/papaparse.js index 4bc3dc37..b48f8d7d 100755 --- a/papaparse.js +++ b/papaparse.js @@ -186,7 +186,13 @@ License: MIT } - + // 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; + } function CsvToJson(_input, _config) { @@ -249,14 +255,6 @@ 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; - } } @@ -513,6 +511,7 @@ License: MIT // First chunk pre-processing if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) { + chunk = stripBom(chunk); var modifiedChunk = this._config.beforeFirstChunk(chunk); if (modifiedChunk !== undefined) chunk = modifiedChunk; From b104eadc768b9f6a4ef4db487675adbc43a5fdd1 Mon Sep 17 00:00:00 2001 From: Ricardo Spear Date: Thu, 26 Oct 2023 12:08:40 -0500 Subject: [PATCH 2/2] strimbom even if beforeFirstChunk is not declared --- papaparse.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/papaparse.js b/papaparse.js index b48f8d7d..dd8255a8 100755 --- a/papaparse.js +++ b/papaparse.js @@ -509,12 +509,14 @@ License: MIT this.parseChunk = function(chunk, isFakeChunk) { // First chunk pre-processing - if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) + if (this.isFirstChunk) { chunk = stripBom(chunk); - var modifiedChunk = this._config.beforeFirstChunk(chunk); - if (modifiedChunk !== undefined) - chunk = modifiedChunk; + if (isFunction(this._config.beforeFirstChunk)) { + var modifiedChunk = this._config.beforeFirstChunk(chunk); + if (modifiedChunk !== undefined) + chunk = modifiedChunk; + } } this.isFirstChunk = false; this._halted = false;