Browse Source

Merge pull request #256 from breck7/master

Updated double equals to triple equals, removed whitespace at end of …
pull/277/head
Matt Holt 10 years ago
parent
commit
921e25076f
  1. 123
      papaparse.js

123
papaparse.js

@ -5,7 +5,7 @@
*/ */
(function(global) (function(global)
{ {
"use strict"; 'use strict';
var IS_WORKER = !global.document && !!global.postMessage, var IS_WORKER = !global.document && !!global.postMessage,
IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search), IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search),
@ -19,15 +19,15 @@
Papa.RECORD_SEP = String.fromCharCode(30); Papa.RECORD_SEP = String.fromCharCode(30);
Papa.UNIT_SEP = String.fromCharCode(31); Papa.UNIT_SEP = String.fromCharCode(31);
Papa.BYTE_ORDER_MARK = "\ufeff"; Papa.BYTE_ORDER_MARK = '\ufeff';
Papa.BAD_DELIMITERS = ["\r", "\n", "\"", Papa.BYTE_ORDER_MARK]; Papa.BAD_DELIMITERS = ['\r', '\n', '"', Papa.BYTE_ORDER_MARK];
Papa.WORKERS_SUPPORTED = !IS_WORKER && !!global.Worker; Papa.WORKERS_SUPPORTED = !IS_WORKER && !!global.Worker;
Papa.SCRIPT_PATH = null; // Must be set by your code if you use workers and this lib is loaded asynchronously Papa.SCRIPT_PATH = null; // Must be set by your code if you use workers and this lib is loaded asynchronously
// Configurable chunk sizes for local and remote files, respectively // Configurable chunk sizes for local and remote files, respectively
Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 MB
Papa.RemoteChunkSize = 1024 * 1024 * 5; // 5 MB Papa.RemoteChunkSize = 1024 * 1024 * 5; // 5 MB
Papa.DefaultDelimiter = ","; // Used if not specified and detection fails Papa.DefaultDelimiter = ','; // Used if not specified and detection fails
// Exposed for testing and development only // Exposed for testing and development only
Papa.Parser = Parser; Papa.Parser = Parser;
@ -62,11 +62,11 @@
this.each(function(idx) this.each(function(idx)
{ {
var supported = $(this).prop('tagName').toUpperCase() == "INPUT" var supported = $(this).prop('tagName').toUpperCase() === 'INPUT'
&& $(this).attr('type').toLowerCase() == "file" && $(this).attr('type').toLowerCase() === 'file'
&& global.FileReader; && global.FileReader;
if (!supported || !this.files || this.files.length == 0) if (!supported || !this.files || this.files.length === 0)
return true; // continue to next input element return true; // continue to next input element
for (var i = 0; i < this.files.length; i++) for (var i = 0; i < this.files.length; i++)
@ -85,7 +85,7 @@
function parseNextFile() function parseNextFile()
{ {
if (queue.length == 0) if (queue.length === 0)
{ {
if (isFunction(options.complete)) if (isFunction(options.complete))
options.complete(); options.complete();
@ -100,12 +100,12 @@
if (typeof returned === 'object') if (typeof returned === 'object')
{ {
if (returned.action == "abort") if (returned.action === 'abort')
{ {
error("AbortError", f.file, f.inputElem, returned.reason); error('AbortError', f.file, f.inputElem, returned.reason);
return; // Aborts all queued files immediately return; // Aborts all queued files immediately
} }
else if (returned.action == "skip") else if (returned.action === 'skip')
{ {
fileComplete(); // parse the next file in the queue, if any fileComplete(); // parse the next file in the queue, if any
return; return;
@ -113,7 +113,7 @@
else if (typeof returned.config === 'object') else if (typeof returned.config === 'object')
f.instanceConfig = $.extend(f.instanceConfig, returned.config); f.instanceConfig = $.extend(f.instanceConfig, returned.config);
} }
else if (returned == "skip") else if (returned === 'skip')
{ {
fileComplete(); // parse the next file in the queue, if any fileComplete(); // parse the next file in the queue, if any
return; return;
@ -221,7 +221,7 @@
function JsonToCsv(_input, _config) function JsonToCsv(_input, _config)
{ {
var _output = ""; var _output = '';
var _fields = []; var _fields = [];
// Default configuration // Default configuration
@ -230,10 +230,10 @@
var _quotes = false; var _quotes = false;
/** delimiting character */ /** delimiting character */
var _delimiter = ","; var _delimiter = ',';
/** newline character(s) */ /** newline character(s) */
var _newline = "\r\n"; var _newline = '\r\n';
unpackConfig(); unpackConfig();
@ -263,14 +263,14 @@
: objectKeys(_input.data[0]); : objectKeys(_input.data[0]);
if (!(_input.data[0] instanceof Array) && typeof _input.data[0] !== 'object') if (!(_input.data[0] instanceof Array) && typeof _input.data[0] !== 'object')
_input.data = [_input.data]; // handles input like [1,2,3] or ["asdf"] _input.data = [_input.data]; // handles input like [1,2,3] or ['asdf']
} }
return serialize(_input.fields || [], _input.data || []); return serialize(_input.fields || [], _input.data || []);
} }
// Default (any valid paths should return before this) // Default (any valid paths should return before this)
throw "exception: Unable to serialize unrecognized input"; throw 'exception: Unable to serialize unrecognized input';
function unpackConfig() function unpackConfig()
@ -279,8 +279,8 @@
return; return;
if (typeof _config.delimiter === 'string' if (typeof _config.delimiter === 'string'
&& _config.delimiter.length == 1 && _config.delimiter.length === 1
&& Papa.BAD_DELIMITERS.indexOf(_config.delimiter) == -1) && Papa.BAD_DELIMITERS.indexOf(_config.delimiter) === -1)
{ {
_delimiter = _config.delimiter; _delimiter = _config.delimiter;
} }
@ -308,7 +308,7 @@
/** The double for loop that iterates the data and writes out a CSV string including header row */ /** The double for loop that iterates the data and writes out a CSV string including header row */
function serialize(fields, data) function serialize(fields, data)
{ {
var csv = ""; var csv = '';
if (typeof fields === 'string') if (typeof fields === 'string')
fields = JSON.parse(fields); fields = JSON.parse(fields);
@ -354,8 +354,8 @@
/** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */ /** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */
function safe(str, col) function safe(str, col)
{ {
if (typeof str === "undefined" || str === null) if (typeof str === 'undefined' || str === null)
return ""; return '';
str = str.toString().replace(/"/g, '""'); str = str.toString().replace(/"/g, '""');
@ -363,8 +363,8 @@
|| (_quotes instanceof Array && _quotes[col]) || (_quotes instanceof Array && _quotes[col])
|| hasAny(str, Papa.BAD_DELIMITERS) || hasAny(str, Papa.BAD_DELIMITERS)
|| str.indexOf(_delimiter) > -1 || str.indexOf(_delimiter) > -1
|| str.charAt(0) == ' ' || str.charAt(0) === ' '
|| str.charAt(str.length - 1) == ' '; || str.charAt(str.length - 1) === ' ';
return needsQuotes ? '"' + str + '"' : str; return needsQuotes ? '"' + str + '"' : str;
} }
@ -386,7 +386,7 @@
this._finished = false; this._finished = false;
this._input = null; this._input = null;
this._baseIndex = 0; this._baseIndex = 0;
this._partialLine = ""; this._partialLine = '';
this._rowCount = 0; this._rowCount = 0;
this._start = 0; this._start = 0;
this._nextChunk = null; this._nextChunk = null;
@ -411,7 +411,7 @@
// 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 = '';
var results = this._handle.parse(aggregate, this._baseIndex, !this._finished); var results = this._handle.parse(aggregate, this._baseIndex, !this._finished);
@ -536,19 +536,20 @@
{ {
xhr.withCredentials = this._config.withCredentials; xhr.withCredentials = this._config.withCredentials;
} }
if (!IS_WORKER) if (!IS_WORKER)
{ {
xhr.onload = bindFunction(this._chunkLoaded, this); xhr.onload = bindFunction(this._chunkLoaded, this);
xhr.onerror = bindFunction(this._chunkError, this); xhr.onerror = bindFunction(this._chunkError, this);
} }
xhr.open("GET", this._input, !IS_WORKER); xhr.open('GET', this._input, !IS_WORKER);
if (this._config.chunkSize) 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);
xhr.setRequestHeader("If-None-Match", "webkit-no-cache"); // https://bugs.webkit.org/show_bug.cgi?id=82672 xhr.setRequestHeader('If-None-Match', 'webkit-no-cache'); // https://bugs.webkit.org/show_bug.cgi?id=82672
} }
try { try {
@ -558,7 +559,7 @@
this._chunkError(err.message); this._chunkError(err.message);
} }
if (IS_WORKER && xhr.status == 0) if (IS_WORKER && xhr.status === 0)
this._chunkError(); this._chunkError();
else else
this._start += this._config.chunkSize; this._start += this._config.chunkSize;
@ -587,8 +588,8 @@
function getFileSize(xhr) function getFileSize(xhr)
{ {
var contentRange = xhr.getResponseHeader("Content-Range"); var contentRange = xhr.getResponseHeader('Content-Range');
return parseInt(contentRange.substr(contentRange.lastIndexOf("/") + 1)); return parseInt(contentRange.substr(contentRange.lastIndexOf('/') + 1));
} }
} }
NetworkStreamer.prototype = Object.create(ChunkStreamer.prototype); NetworkStreamer.prototype = Object.create(ChunkStreamer.prototype);
@ -724,7 +725,7 @@
processResults(); processResults();
// It's possbile that this line was empty and there's no row here after all // It's possbile that this line was empty and there's no row here after all
if (_results.data.length == 0) if (_results.data.length === 0)
return; return;
_stepCounter += results.data.length; _stepCounter += results.data.length;
@ -800,21 +801,21 @@
_results.meta.aborted = true; _results.meta.aborted = true;
if (isFunction(_config.complete)) if (isFunction(_config.complete))
_config.complete(_results); _config.complete(_results);
_input = ""; _input = '';
}; };
function processResults() function processResults()
{ {
if (_results && _delimiterError) if (_results && _delimiterError)
{ {
addError("Delimiter", "UndetectableDelimiter", "Unable to auto-detect delimiting character; defaulted to '"+Papa.DefaultDelimiter+"'"); addError('Delimiter', 'UndetectableDelimiter', 'Unable to auto-detect delimiting character; defaulted to \''+Papa.DefaultDelimiter+'\'');
_delimiterError = false; _delimiterError = false;
} }
if (_config.skipEmptyLines) if (_config.skipEmptyLines)
{ {
for (var i = 0; i < _results.data.length; i++) for (var i = 0; i < _results.data.length; i++)
if (_results.data[i].length == 1 && _results.data[i][0] == "") if (_results.data[i].length === 1 && _results.data[i][0] === '')
_results.data.splice(i--, 1); _results.data.splice(i--, 1);
} }
@ -826,7 +827,7 @@
function needsHeaderRow() function needsHeaderRow()
{ {
return _config.header && _fields.length == 0; return _config.header && _fields.length === 0;
} }
function fillHeaderFields() function fillHeaderFields()
@ -853,9 +854,9 @@
if (_config.dynamicTyping) if (_config.dynamicTyping)
{ {
var value = _results.data[i][j]; var value = _results.data[i][j];
if (value == "true" || value == "TRUE") if (value === 'true' || value === 'TRUE')
_results.data[i][j] = true; _results.data[i][j] = true;
else if (value == "false" || value == "FALSE") else if (value === 'false' || value === 'FALSE')
_results.data[i][j] = false; _results.data[i][j] = false;
else else
_results.data[i][j] = tryParseFloat(value); _results.data[i][j] = tryParseFloat(value);
@ -865,9 +866,9 @@
{ {
if (j >= _fields.length) if (j >= _fields.length)
{ {
if (!row["__parsed_extra"]) if (!row['__parsed_extra'])
row["__parsed_extra"] = []; row['__parsed_extra'] = [];
row["__parsed_extra"].push(_results.data[i][j]); row['__parsed_extra'].push(_results.data[i][j]);
} }
else else
row[_fields[j]] = _results.data[i][j]; row[_fields[j]] = _results.data[i][j];
@ -878,9 +879,9 @@
{ {
_results.data[i] = row; _results.data[i] = row;
if (j > _fields.length) if (j > _fields.length)
addError("FieldMismatch", "TooManyFields", "Too many fields: expected " + _fields.length + " fields but parsed " + j, i); addError('FieldMismatch', 'TooManyFields', 'Too many fields: expected ' + _fields.length + ' fields but parsed ' + j, i);
else if (j < _fields.length) else if (j < _fields.length)
addError("FieldMismatch", "TooFewFields", "Too few fields: expected " + _fields.length + " fields but parsed " + j, i); addError('FieldMismatch', 'TooFewFields', 'Too few fields: expected ' + _fields.length + ' fields but parsed ' + j, i);
} }
} }
@ -891,7 +892,7 @@
function guessDelimiter(input) function guessDelimiter(input)
{ {
var delimChoices = [",", "\t", "|", ";", Papa.RECORD_SEP, Papa.UNIT_SEP]; var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP];
var bestDelim, bestDelta, fieldCountPrevRow; var bestDelim, bestDelta, fieldCountPrevRow;
for (var i = 0; i < delimChoices.length; i++) for (var i = 0; i < delimChoices.length; i++)
@ -947,13 +948,13 @@
var r = input.split('\r'); var r = input.split('\r');
if (r.length == 1) if (r.length === 1)
return '\n'; return '\n';
var numWithN = 0; var numWithN = 0;
for (var i = 0; i < r.length; i++) for (var i = 0; i < r.length; i++)
{ {
if (r[i][0] == '\n') if (r[i][0] === '\n')
numWithN++; numWithN++;
} }
@ -996,13 +997,13 @@
// Delimiter must be valid // Delimiter must be valid
if (typeof delim !== 'string' if (typeof delim !== 'string'
|| Papa.BAD_DELIMITERS.indexOf(delim) > -1) || Papa.BAD_DELIMITERS.indexOf(delim) > -1)
delim = ","; delim = ',';
// Comment character must be valid // Comment character must be valid
if (comments === delim) if (comments === delim)
throw "Comment character same as delimiter"; throw 'Comment character same as delimiter';
else if (comments === true) else if (comments === true)
comments = "#"; comments = '#';
else if (typeof comments !== 'string' else if (typeof comments !== 'string'
|| Papa.BAD_DELIMITERS.indexOf(comments) > -1) || Papa.BAD_DELIMITERS.indexOf(comments) > -1)
comments = false; comments = false;
@ -1019,7 +1020,7 @@
{ {
// For some reason, in Chrome, this speeds things up (!?) // For some reason, in Chrome, this speeds things up (!?)
if (typeof input !== 'string') if (typeof input !== 'string')
throw "Input must be a string"; throw 'Input must be a string';
// We don't need to compute some of these every time parse() is called, // We don't need to compute some of these every time parse() is called,
// but having them in a more local scope seems to perform better // but having them in a more local scope seems to perform better
@ -1047,7 +1048,7 @@
cursor += newline.length; cursor += newline.length;
else if (ignoreLastRow) else if (ignoreLastRow)
return returnable(); return returnable();
if (comments && row.substr(0, commentsLen) == comments) if (comments && row.substr(0, commentsLen) === comments)
continue; continue;
if (stepIsFunction) if (stepIsFunction)
{ {
@ -1075,7 +1076,7 @@
for (;;) for (;;)
{ {
// Field has opening quote // Field has opening quote
if (input[cursor] == '"') if (input[cursor] === '"')
{ {
// Start our search for the closing quote where the cursor is // Start our search for the closing quote where the cursor is
var quoteSearch = cursor; var quoteSearch = cursor;
@ -1093,9 +1094,9 @@
if (!ignoreLastRow) { if (!ignoreLastRow) {
// No closing quote... what a pity // No closing quote... what a pity
errors.push({ errors.push({
type: "Quotes", type: 'Quotes',
code: "MissingQuotes", code: 'MissingQuotes',
message: "Quoted field unterminated", message: 'Quoted field unterminated',
row: data.length, // row has yet to be inserted row: data.length, // row has yet to be inserted
index: cursor index: cursor
}); });
@ -1111,13 +1112,13 @@
} }
// If this quote is escaped, it's part of the data; skip it // If this quote is escaped, it's part of the data; skip it
if (input[quoteSearch+1] == '"') if (input[quoteSearch+1] === '"')
{ {
quoteSearch++; quoteSearch++;
continue; continue;
} }
if (input[quoteSearch+1] == delim) if (input[quoteSearch+1] === delim)
{ {
// Closing quote followed by delimiter // Closing quote followed by delimiter
row.push(input.substring(cursor, quoteSearch).replace(/""/g, '"')); row.push(input.substring(cursor, quoteSearch).replace(/""/g, '"'));
@ -1154,7 +1155,7 @@
// Comment found at start of new line // Comment found at start of new line
if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments) if (comments && row.length === 0 && input.substr(cursor, commentsLen) === comments)
{ {
if (nextNewline == -1) // Comment ends at EOF if (nextNewline === -1) // Comment ends at EOF
return returnable(); return returnable();
cursor = nextNewline + newlineLen; cursor = nextNewline + newlineLen;
nextNewline = input.indexOf(newline, cursor); nextNewline = input.indexOf(newline, cursor);
@ -1291,7 +1292,7 @@
'You need to set Papa.SCRIPT_PATH manually.' 'You need to set Papa.SCRIPT_PATH manually.'
); );
var workerUrl = Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH; var workerUrl = Papa.SCRIPT_PATH || AUTO_SCRIPT_PATH;
// Append "papaworker" to the search string to tell papaparse that this is our worker. // Append 'papaworker' to the search string to tell papaparse that this is our worker.
workerUrl += (workerUrl.indexOf('?') !== -1 ? '&' : '?') + 'papaworker'; workerUrl += (workerUrl.indexOf('?') !== -1 ? '&' : '?') + 'papaworker';
var w = new global.Worker(workerUrl); var w = new global.Worker(workerUrl);
w.onmessage = mainThreadReceivedMessage; w.onmessage = mainThreadReceivedMessage;
@ -1356,7 +1357,7 @@
} }
function notImplemented() { function notImplemented() {
throw "Not implemented."; throw 'Not implemented.';
} }
/** Callback when worker thread receives a message */ /** Callback when worker thread receives a message */

Loading…
Cancel
Save