diff --git a/papaparse.js b/papaparse.js index d145423..ad84d24 100644 --- a/papaparse.js +++ b/papaparse.js @@ -379,7 +379,7 @@ this._rowCount = 0; this._start = 0; this._nextChunk = null; - this._chunkIndex = 0; + this.isFirstChunk = true; this._completeResults = { data: [], errors: [], @@ -389,13 +389,14 @@ this.parseChunk = function(chunk) { - if (this._chunkIndex == 0 && isFunction(this._config.beforeFirstChunk)) { - var result = this._config.beforeFirstChunk(chunk); - if (result !== undefined) - chunk = result; + // First chunk pre-processing + if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk)) + { + var modifiedChunk = this._config.beforeFirstChunk(chunk); + if (modifiedChunk !== undefined) + chunk = modifiedChunk; } - - this._chunkIndex++; + this.isFirstChunk = false; // Rejoin the line we likely just split in two by chunking the file var aggregate = this._partialLine + chunk; diff --git a/player/player.js b/player/player.js index dceec7b..9560c24 100644 --- a/player/player.js +++ b/player/player.js @@ -104,7 +104,8 @@ function buildConfig() download: $('#download').prop('checked'), fastMode: $('#fastmode').prop('checked'), skipEmptyLines: $('#skipEmptyLines').prop('checked'), - chunk: $('#chunk').prop('checked') ? chunkFn : undefined + chunk: $('#chunk').prop('checked') ? chunkFn : undefined, + beforeFirstChunk: undefined, }; function getLineEnding() diff --git a/tests/test-cases.js b/tests/test-cases.js index 2b2336a..bceb9d1 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1322,7 +1322,7 @@ var CUSTOM_TESTS = [ } }, { - description: "beforeFirstChunk can manipulate first chunk", + description: "beforeFirstChunk manipulates only first chunk", expected: 7, run: function(callback) { var updates = 0; @@ -1340,6 +1340,25 @@ var CUSTOM_TESTS = [ } }); } + }, + { + description: "First chunk not modified if beforeFirstChunk returns nothing", + expected: 8, + run: function(callback) { + var updates = 0; + Papa.parse("/tests/long-sample.csv", { + download: true, + chunkSize: 500, + beforeFirstChunk: function(chunk) { + }, + step: function(response) { + updates++; + }, + complete: function() { + callback(updates); + } + }); + } } ];