From 4ae54949019d407baf5e896761262a196e8fc313 Mon Sep 17 00:00:00 2001 From: Breck Date: Sun, 13 Sep 2015 09:51:37 -0700 Subject: [PATCH] Updated double equals to triple equals, removed whitespace at end of lines and standardized quotes. --- papaparse.js | 135 ++++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/papaparse.js b/papaparse.js index 0ac52aa..681036a 100644 --- a/papaparse.js +++ b/papaparse.js @@ -5,7 +5,7 @@ */ (function(global) { - "use strict"; + 'use strict'; var IS_WORKER = !global.document && !!global.postMessage, IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search), @@ -19,15 +19,15 @@ Papa.RECORD_SEP = String.fromCharCode(30); Papa.UNIT_SEP = String.fromCharCode(31); - Papa.BYTE_ORDER_MARK = "\ufeff"; - Papa.BAD_DELIMITERS = ["\r", "\n", "\"", Papa.BYTE_ORDER_MARK]; + Papa.BYTE_ORDER_MARK = '\ufeff'; + Papa.BAD_DELIMITERS = ['\r', '\n', '"', Papa.BYTE_ORDER_MARK]; 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 // Configurable chunk sizes for local and remote files, respectively Papa.LocalChunkSize = 1024 * 1024 * 10; // 10 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 Papa.Parser = Parser; @@ -62,11 +62,11 @@ this.each(function(idx) { - var supported = $(this).prop('tagName').toUpperCase() == "INPUT" - && $(this).attr('type').toLowerCase() == "file" + var supported = $(this).prop('tagName').toUpperCase() === 'INPUT' + && $(this).attr('type').toLowerCase() === 'file' && 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 for (var i = 0; i < this.files.length; i++) @@ -85,7 +85,7 @@ function parseNextFile() { - if (queue.length == 0) + if (queue.length === 0) { if (isFunction(options.complete)) options.complete(); @@ -100,12 +100,12 @@ 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 } - else if (returned.action == "skip") + else if (returned.action === 'skip') { fileComplete(); // parse the next file in the queue, if any return; @@ -113,7 +113,7 @@ else if (typeof returned.config === 'object') 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 return; @@ -221,7 +221,7 @@ function JsonToCsv(_input, _config) { - var _output = ""; + var _output = ''; var _fields = []; // Default configuration @@ -230,10 +230,10 @@ var _quotes = false; /** delimiting character */ - var _delimiter = ","; + var _delimiter = ','; /** newline character(s) */ - var _newline = "\r\n"; + var _newline = '\r\n'; unpackConfig(); @@ -263,14 +263,14 @@ : objectKeys(_input.data[0]); 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 || []); } // Default (any valid paths should return before this) - throw "exception: Unable to serialize unrecognized input"; + throw 'exception: Unable to serialize unrecognized input'; function unpackConfig() @@ -279,8 +279,8 @@ return; if (typeof _config.delimiter === 'string' - && _config.delimiter.length == 1 - && Papa.BAD_DELIMITERS.indexOf(_config.delimiter) == -1) + && _config.delimiter.length === 1 + && Papa.BAD_DELIMITERS.indexOf(_config.delimiter) === -1) { _delimiter = _config.delimiter; } @@ -308,7 +308,7 @@ /** The double for loop that iterates the data and writes out a CSV string including header row */ function serialize(fields, data) { - var csv = ""; + var csv = ''; if (typeof fields === 'string') fields = JSON.parse(fields); @@ -354,8 +354,8 @@ /** Encloses a value around quotes if needed (makes a value safe for CSV insertion) */ function safe(str, col) { - if (typeof str === "undefined" || str === null) - return ""; + if (typeof str === 'undefined' || str === null) + return ''; str = str.toString().replace(/"/g, '""'); @@ -363,8 +363,8 @@ || (_quotes instanceof Array && _quotes[col]) || hasAny(str, Papa.BAD_DELIMITERS) || str.indexOf(_delimiter) > -1 - || str.charAt(0) == ' ' - || str.charAt(str.length - 1) == ' '; + || str.charAt(0) === ' ' + || str.charAt(str.length - 1) === ' '; return needsQuotes ? '"' + str + '"' : str; } @@ -386,7 +386,7 @@ this._finished = false; this._input = null; this._baseIndex = 0; - this._partialLine = ""; + this._partialLine = ''; this._rowCount = 0; this._start = 0; this._nextChunk = null; @@ -411,15 +411,15 @@ // Rejoin the line we likely just split in two by chunking the file var aggregate = this._partialLine + chunk; - this._partialLine = ""; + this._partialLine = ''; var results = this._handle.parse(aggregate, this._baseIndex, !this._finished); - + if (this._handle.paused() || this._handle.aborted()) return; - + var lastIndex = results.meta.cursor; - + if (!this._finished) { this._partialLine = aggregate.substring(lastIndex - this._baseIndex); @@ -531,24 +531,25 @@ } xhr = new XMLHttpRequest(); - + if (this._config.withCredentials) { xhr.withCredentials = this._config.withCredentials; } + if (!IS_WORKER) { xhr.onload = bindFunction(this._chunkLoaded, 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) { var end = this._start + this._config.chunkSize - 1; // minus one because byte range is inclusive - 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('Range', 'bytes='+this._start+'-'+end); + xhr.setRequestHeader('If-None-Match', 'webkit-no-cache'); // https://bugs.webkit.org/show_bug.cgi?id=82672 } try { @@ -558,7 +559,7 @@ this._chunkError(err.message); } - if (IS_WORKER && xhr.status == 0) + if (IS_WORKER && xhr.status === 0) this._chunkError(); else this._start += this._config.chunkSize; @@ -587,8 +588,8 @@ function getFileSize(xhr) { - var contentRange = xhr.getResponseHeader("Content-Range"); - return parseInt(contentRange.substr(contentRange.lastIndexOf("/") + 1)); + var contentRange = xhr.getResponseHeader('Content-Range'); + return parseInt(contentRange.substr(contentRange.lastIndexOf('/') + 1)); } } NetworkStreamer.prototype = Object.create(ChunkStreamer.prototype); @@ -724,7 +725,7 @@ processResults(); // 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; _stepCounter += results.data.length; @@ -800,21 +801,21 @@ _results.meta.aborted = true; if (isFunction(_config.complete)) _config.complete(_results); - _input = ""; + _input = ''; }; function processResults() { 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; } if (_config.skipEmptyLines) { 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); } @@ -826,7 +827,7 @@ function needsHeaderRow() { - return _config.header && _fields.length == 0; + return _config.header && _fields.length === 0; } function fillHeaderFields() @@ -853,9 +854,9 @@ if (_config.dynamicTyping) { var value = _results.data[i][j]; - if (value == "true" || value == "TRUE") + if (value === 'true' || value === 'TRUE') _results.data[i][j] = true; - else if (value == "false" || value == "FALSE") + else if (value === 'false' || value === 'FALSE') _results.data[i][j] = false; else _results.data[i][j] = tryParseFloat(value); @@ -865,9 +866,9 @@ { if (j >= _fields.length) { - if (!row["__parsed_extra"]) - row["__parsed_extra"] = []; - row["__parsed_extra"].push(_results.data[i][j]); + if (!row['__parsed_extra']) + row['__parsed_extra'] = []; + row['__parsed_extra'].push(_results.data[i][j]); } else row[_fields[j]] = _results.data[i][j]; @@ -878,9 +879,9 @@ { _results.data[i] = row; 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) - 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) { - var delimChoices = [",", "\t", "|", ";", Papa.RECORD_SEP, Papa.UNIT_SEP]; + var delimChoices = [',', '\t', '|', ';', Papa.RECORD_SEP, Papa.UNIT_SEP]; var bestDelim, bestDelta, fieldCountPrevRow; for (var i = 0; i < delimChoices.length; i++) @@ -947,13 +948,13 @@ var r = input.split('\r'); - if (r.length == 1) + if (r.length === 1) return '\n'; var numWithN = 0; for (var i = 0; i < r.length; i++) { - if (r[i][0] == '\n') + if (r[i][0] === '\n') numWithN++; } @@ -996,13 +997,13 @@ // Delimiter must be valid if (typeof delim !== 'string' || Papa.BAD_DELIMITERS.indexOf(delim) > -1) - delim = ","; + delim = ','; // Comment character must be valid if (comments === delim) - throw "Comment character same as delimiter"; + throw 'Comment character same as delimiter'; else if (comments === true) - comments = "#"; + comments = '#'; else if (typeof comments !== 'string' || Papa.BAD_DELIMITERS.indexOf(comments) > -1) comments = false; @@ -1019,7 +1020,7 @@ { // For some reason, in Chrome, this speeds things up (!?) 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, // but having them in a more local scope seems to perform better @@ -1047,7 +1048,7 @@ cursor += newline.length; else if (ignoreLastRow) return returnable(); - if (comments && row.substr(0, commentsLen) == comments) + if (comments && row.substr(0, commentsLen) === comments) continue; if (stepIsFunction) { @@ -1075,7 +1076,7 @@ for (;;) { // Field has opening quote - if (input[cursor] == '"') + if (input[cursor] === '"') { // Start our search for the closing quote where the cursor is var quoteSearch = cursor; @@ -1093,9 +1094,9 @@ if (!ignoreLastRow) { // No closing quote... what a pity errors.push({ - type: "Quotes", - code: "MissingQuotes", - message: "Quoted field unterminated", + type: 'Quotes', + code: 'MissingQuotes', + message: 'Quoted field unterminated', row: data.length, // row has yet to be inserted index: cursor }); @@ -1111,13 +1112,13 @@ } // If this quote is escaped, it's part of the data; skip it - if (input[quoteSearch+1] == '"') + if (input[quoteSearch+1] === '"') { quoteSearch++; continue; } - if (input[quoteSearch+1] == delim) + if (input[quoteSearch+1] === delim) { // Closing quote followed by delimiter row.push(input.substring(cursor, quoteSearch).replace(/""/g, '"')); @@ -1140,7 +1141,7 @@ if (aborted) return returnable(); } - + if (preview && data.length >= preview) return returnable(true); @@ -1154,7 +1155,7 @@ // Comment found at start of new line 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(); cursor = nextNewline + newlineLen; nextNewline = input.indexOf(newline, cursor); @@ -1291,7 +1292,7 @@ 'You need to set Papa.SCRIPT_PATH manually.' ); 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'; var w = new global.Worker(workerUrl); w.onmessage = mainThreadReceivedMessage; @@ -1356,7 +1357,7 @@ } function notImplemented() { - throw "Not implemented."; + throw 'Not implemented.'; } /** Callback when worker thread receives a message */