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 @@
this._rowCount = 0; this._rowCount = 0;
this._start = 0; this._start = 0;
this._nextChunk = null; this._nextChunk = null;
this._chunkIndex = 0; this.isFirstChunk = true;
this._completeResults = { this._completeResults = {
data: [], data: [],
errors: [], errors: [],
@ -389,13 +389,14 @@
this.parseChunk = function(chunk) this.parseChunk = function(chunk)
{ {
if (this._chunkIndex == 0 && isFunction(this._config.beforeFirstChunk)) { // First chunk pre-processing
var result = this._config.beforeFirstChunk(chunk); if (this.isFirstChunk && isFunction(this._config.beforeFirstChunk))
if (result !== undefined) {
chunk = result; var modifiedChunk = this._config.beforeFirstChunk(chunk);
if (modifiedChunk !== undefined)
chunk = modifiedChunk;
} }
this.isFirstChunk = false;
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;

3
player/player.js

@ -104,7 +104,8 @@ function buildConfig()
download: $('#download').prop('checked'), download: $('#download').prop('checked'),
fastMode: $('#fastmode').prop('checked'), fastMode: $('#fastmode').prop('checked'),
skipEmptyLines: $('#skipEmptyLines').prop('checked'), skipEmptyLines: $('#skipEmptyLines').prop('checked'),
chunk: $('#chunk').prop('checked') ? chunkFn : undefined chunk: $('#chunk').prop('checked') ? chunkFn : undefined,
beforeFirstChunk: undefined,
}; };
function getLineEnding() function getLineEnding()

21
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, expected: 7,
run: function(callback) { run: function(callback) {
var updates = 0; 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);
}
});
}
} }
]; ];

Loading…
Cancel
Save