Browse Source

Merge pull request #181 from bluej100/complete

read files in a single chunk for complete results (#179)
pull/166/merge
Matt Holt 10 years ago
parent
commit
21a475bd9b
  1. 17
      papaparse.js
  2. 12
      tests/test-cases.js

17
papaparse.js

@ -450,6 +450,8 @@
// Deep-copy the config so we can edit it // Deep-copy the config so we can edit it
var configCopy = copy(config); var configCopy = copy(config);
configCopy.chunkSize = parseInt(configCopy.chunkSize); // VERY important so we don't concatenate strings! configCopy.chunkSize = parseInt(configCopy.chunkSize); // VERY important so we don't concatenate strings!
if (!config.step && !config.chunk)
configCopy.chunkSize = null;
this._handle = new ParserHandle(configCopy); this._handle = new ParserHandle(configCopy);
this._handle.streamer = this; this._handle.streamer = this;
this._config = configCopy; // persist the copy to the caller this._config = configCopy; // persist the copy to the caller
@ -506,7 +508,7 @@
xhr.open("GET", this._input, !IS_WORKER); xhr.open("GET", this._input, !IS_WORKER);
if (this._config.step || this._config.chunk) if (this._config.chunkSize)
{ {
var end = this._start + this._config.chunkSize - 1; // minus one because byte range is inclusive var end = this._start + this._config.chunkSize - 1; // minus one because byte range is inclusive
xhr.setRequestHeader("Range", "bytes="+this._start+"-"+end); xhr.setRequestHeader("Range", "bytes="+this._start+"-"+end);
@ -537,7 +539,7 @@
return; return;
} }
this._finished = (!this._config.step && !this._config.chunk) || this._start > getFileSize(xhr); this._finished = !this._config.chunkSize || this._start > getFileSize(xhr);
this.parseChunk(xhr.responseText); this.parseChunk(xhr.responseText);
} }
@ -595,8 +597,13 @@
this._readChunk = function() this._readChunk = function()
{ {
var end = Math.min(this._start + this._config.chunkSize, this._input.size); var input = this._input;
var txt = reader.readAsText(slice.call(this._input, this._start, end), this._config.encoding); if (this._config.chunkSize)
{
var end = Math.min(this._start + this._config.chunkSize, this._input.size);
input = slice.call(input, this._start, end);
}
var txt = reader.readAsText(input, this._config.encoding);
if (!usingAsyncReader) if (!usingAsyncReader)
this._chunkLoaded({ target: { result: txt } }); // mimic the async signature this._chunkLoaded({ target: { result: txt } }); // mimic the async signature
} }
@ -605,7 +612,7 @@
{ {
// Very important to increment start each time before handling results // Very important to increment start each time before handling results
this._start += this._config.chunkSize; this._start += this._config.chunkSize;
this._finished = this._start >= this._input.size; this._finished = !this._config.chunkSize || this._start >= this._input.size;
this.parseChunk(event.target.result); this.parseChunk(event.target.result);
} }

12
tests/test-cases.js

@ -1041,6 +1041,18 @@ var UNPARSE_TESTS = [
var CUSTOM_TESTS = [ var CUSTOM_TESTS = [
{
description: "Complete is called with all results if neither step nor chunk is defined",
expected: [['A', 'b', 'c'], ['d', 'E', 'f'], ['G', 'h', 'i']],
run: function(callback) {
Papa.parse(new File(['A,b,c\nd,E,f\nG,h,i'], 'sample.csv'), {
chunkSize: 3,
complete: function(response) {
callback(response.data);
}
});
}
},
{ {
description: "Step is called for each row", description: "Step is called for each row",
expected: 2, expected: 2,

Loading…
Cancel
Save