diff --git a/papaparse.js b/papaparse.js index 193f62d..88a3d51 100644 --- a/papaparse.js +++ b/papaparse.js @@ -380,6 +380,7 @@ this._rowCount = 0; this._start = 0; this._nextChunk = null; + this._chunkIndex = 0; this._completeResults = { data: [], errors: [], @@ -389,6 +390,14 @@ this.parseChunk = function(chunk) { + if (this._chunkIndex == 0 && isFunction(this._config.beforeFirstChunk)) { + var result = this._config.beforeFirstChunk(chunk); + if (result !== undefined) + chunk = result; + } + + this._chunkIndex++; + // Rejoin the line we likely just split in two by chunking the file var aggregate = this._partialLine + chunk; this._partialLine = ""; diff --git a/tests/test-cases.js b/tests/test-cases.js index a4f2c3f..2b2336a 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -1309,6 +1309,7 @@ var CUSTOM_TESTS = [ var updates = 0; Papa.parse("/tests/long-sample.csv", { worker: true, + download: true, chunkSize: 500, step: function(response, handle) { updates++; @@ -1319,5 +1320,26 @@ var CUSTOM_TESTS = [ } }); } + }, + { + description: "beforeFirstChunk can manipulate first chunk", + expected: 7, + run: function(callback) { + var updates = 0; + Papa.parse("/tests/long-sample.csv", { + download: true, + chunkSize: 500, + beforeFirstChunk: function(chunk) { + return chunk.replace(/.*?\n/, ''); + }, + step: function(response) { + updates++; + }, + complete: function() { + callback(updates); + } + }); + } } + ];