Browse Source

Merge pull request #192 from bluej100/beforeFirstChunk

beforeFirstChunk callback, fixes #186
pull/204/head
Matt Holt 10 years ago
parent
commit
6417f8aa68
  1. 9
      papaparse.js
  2. 22
      tests/test-cases.js

9
papaparse.js

@ -379,6 +379,7 @@
this._rowCount = 0; this._rowCount = 0;
this._start = 0; this._start = 0;
this._nextChunk = null; this._nextChunk = null;
this._chunkIndex = 0;
this._completeResults = { this._completeResults = {
data: [], data: [],
errors: [], errors: [],
@ -388,6 +389,14 @@
this.parseChunk = function(chunk) 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 // Rejoin the line we likely just split in two by chunking the file
var aggregate = this._partialLine + chunk; var aggregate = this._partialLine + chunk;
this._partialLine = ""; this._partialLine = "";

22
tests/test-cases.js

@ -1309,6 +1309,7 @@ var CUSTOM_TESTS = [
var updates = 0; var updates = 0;
Papa.parse("/tests/long-sample.csv", { Papa.parse("/tests/long-sample.csv", {
worker: true, worker: true,
download: true,
chunkSize: 500, chunkSize: 500,
step: function(response, handle) { step: function(response, handle) {
updates++; 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);
}
});
}
} }
]; ];

Loading…
Cancel
Save