Browse Source

Using latest

pull/89/head
Matthew Holt 11 years ago
parent
commit
05e46a3df7
  1. 39
      resources/js/papaparse.js

39
resources/js/papaparse.js

@ -547,13 +547,22 @@
var reader, nextChunk, slice; var reader, nextChunk, slice;
var handle = new ParserHandle(copy(config)); var handle = new ParserHandle(copy(config));
// FileReader is better than FileReaderSync (even in worker) - see http://stackoverflow.com/q/24708649/1048862
// But Firefox is a pill, too - see issue #76: https://github.com/mholt/PapaParse/issues/76
var usingAsyncReader = typeof FileReader === 'function';
this.stream = function(file) this.stream = function(file)
{ {
var slice = file.slice || file.webkitSlice || file.mozSlice; var slice = file.slice || file.webkitSlice || file.mozSlice;
reader = new FileReader(); // Better than FileReaderSync (even in worker). See: http://stackoverflow.com/q/24708649/1048862 if (usingAsyncReader)
{
reader = new FileReader(); // Preferred method of reading files, even in workers
reader.onload = chunkLoaded; reader.onload = chunkLoaded;
reader.onerror = chunkError; reader.onerror = chunkError;
}
else
reader = new FileReaderSync(); // Hack for running in a web worker in Firefox
nextChunk(); // Starts streaming nextChunk(); // Starts streaming
@ -567,12 +576,15 @@
{ {
var end = Math.min(start + config.chunkSize, file.size); var end = Math.min(start + config.chunkSize, file.size);
var txt = reader.readAsText(slice.call(file, start, end), config.encoding); var txt = reader.readAsText(slice.call(file, start, end), config.encoding);
start += config.chunkSize; if (!usingAsyncReader)
return txt; chunkLoaded({ target: { result: txt } }); // mimic the async signature
} }
function chunkLoaded(event) function chunkLoaded(event)
{ {
// Very important to increment start each time before handling results
start += config.chunkSize;
// 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
aggregate += partialLine + event.target.result; aggregate += partialLine + event.target.result;
partialLine = ""; partialLine = "";
@ -869,6 +881,7 @@
var _errors; // Parse errors var _errors; // Parse errors
var _rowIdx; // Current row index within results (0-based) var _rowIdx; // Current row index within results (0-based)
var _colIdx; // Current col index within result row (0-based) var _colIdx; // Current col index within result row (0-based)
var _runningRowIdx; // Cumulative row index, used by the preview feature
var _aborted = false; // Abort flag var _aborted = false; // Abort flag
var _paused = false; // Pause flag var _paused = false; // Pause flag
@ -928,7 +941,7 @@
while (_i < _input.length) while (_i < _input.length)
{ {
if (_aborted) break; if (_aborted) break;
if (_preview > 0 && _rowIdx >= _preview) break; if (_preview > 0 && _runningRowIdx >= _preview) break;
if (_paused) return finishParsing(); if (_paused) return finishParsing();
if (_ch == '"') if (_ch == '"')
@ -977,6 +990,8 @@
function parseInQuotes() function parseInQuotes()
{ {
if (twoCharLineBreak(_i) || oneCharLineBreak(_i))
_lineNum++;
saveChar(); saveChar();
} }
@ -984,12 +999,12 @@
{ {
if (_ch == _delimiter) if (_ch == _delimiter)
newField(); newField();
else if (twoCharLineBreak()) else if (twoCharLineBreak(_i))
{ {
newRow(); newRow();
nextChar(); nextChar();
} }
else if (oneCharLineBreak()) else if (oneCharLineBreak(_i))
newRow(); newRow();
else if (isCommentStart()) else if (isCommentStart())
skipLine(); skipLine();
@ -1010,8 +1025,8 @@
function skipLine() function skipLine()
{ {
while (!twoCharLineBreak() while (!twoCharLineBreak(_i)
&& !oneCharLineBreak() && !oneCharLineBreak(_i)
&& _i < _input.length) && _i < _input.length)
{ {
nextChar(); nextChar();
@ -1034,6 +1049,7 @@
endRow(); endRow();
_lineNum++; _lineNum++;
_runningRowIdx++;
_data.push([]); _data.push([]);
_rowIdx = _data.length - 1; _rowIdx = _data.length - 1;
newField(); newField();
@ -1064,8 +1080,6 @@
function twoCharLineBreak(i) function twoCharLineBreak(i)
{ {
if (typeof i !== 'number')
i = _i;
return i < _input.length - 1 && return i < _input.length - 1 &&
((_input[i] == "\r" && _input[i+1] == "\n") ((_input[i] == "\r" && _input[i+1] == "\n")
|| (_input[i] == "\n" && _input[i+1] == "\r")) || (_input[i] == "\n" && _input[i+1] == "\r"))
@ -1073,8 +1087,6 @@
function oneCharLineBreak(i) function oneCharLineBreak(i)
{ {
if (typeof i !== 'number')
i = _i;
return _input[i] == "\r" || _input[i] == "\n"; return _input[i] == "\r" || _input[i] == "\n";
} }
@ -1118,8 +1130,7 @@
{ {
_input = input; _input = input;
_inQuotes = false; _inQuotes = false;
_lineNum = 1; _i = 0, _runningRowIdx = 0, _lineNum = 1;
_i = 0;
clearErrorsAndData(); clearErrorsAndData();
_data = [ [""] ]; // starting parsing requires an empty field _data = [ [""] ]; // starting parsing requires an empty field
_ch = _input[_i]; _ch = _input[_i];

Loading…
Cancel
Save