Browse Source

Refactored chunkIndex into isFirstChunk

pull/204/head
Matthew Holt 10 years ago
parent
commit
1c64d5c098
  1. 15
      papaparse.js
  2. 3
      player/player.js
  3. 21
      tests/test-cases.js

15
papaparse.js

@ -379,7 +379,7 @@ @@ -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 @@ @@ -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;

3
player/player.js

@ -104,7 +104,8 @@ function buildConfig() @@ -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()

21
tests/test-cases.js

@ -1322,7 +1322,7 @@ var CUSTOM_TESTS = [ @@ -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 = [ @@ -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);
}
});
}
}
];

Loading…
Cancel
Save