Browse Source

Update paparse demo version

pull/462/head
Sergi Almacellas Abellana 7 years ago
parent
commit
959d8ea17e
  1. 47
      resources/js/papaparse.js

47
resources/js/papaparse.js

@ -1,7 +1,8 @@
/*! /*!
Papa Parse Papa Parse
v4.3.2 v4.3.7
https://github.com/mholt/PapaParse https://github.com/mholt/PapaParse
License: MIT
*/ */
(function(root, factory) (function(root, factory)
{ {
@ -10,7 +11,7 @@
// AMD. Register as an anonymous module. // AMD. Register as an anonymous module.
define([], factory); define([], factory);
} }
else if (typeof module === 'object' && module.exports) else if (typeof module === 'object' && typeof exports !== 'undefined')
{ {
// Node. Does not work with strict CommonJS, but // Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports, // only CommonJS-like environments that support module.exports,
@ -711,7 +712,7 @@
this._chunkError = function() this._chunkError = function()
{ {
this._sendError(reader.error); this._sendError(reader.error.message);
} }
} }
@ -877,7 +878,7 @@
_delimiterError = false; _delimiterError = false;
if (!_config.delimiter) if (!_config.delimiter)
{ {
var delimGuess = guessDelimiter(input, _config.newline); var delimGuess = guessDelimiter(input, _config.newline, _config.skipEmptyLines);
if (delimGuess.successful) if (delimGuess.successful)
_config.delimiter = delimGuess.bestDelimiter; _config.delimiter = delimGuess.bestDelimiter;
else else
@ -1039,7 +1040,7 @@
return _results; return _results;
} }
function guessDelimiter(input, newline) function guessDelimiter(input, newline, skipEmptyLines)
{ {
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;
@ -1047,7 +1048,7 @@
for (var i = 0; i < delimChoices.length; i++) for (var i = 0; i < delimChoices.length; i++)
{ {
var delim = delimChoices[i]; var delim = delimChoices[i];
var delta = 0, avgFieldCount = 0; var delta = 0, avgFieldCount = 0, emptyLinesCount = 0;
fieldCountPrevRow = undefined; fieldCountPrevRow = undefined;
var preview = new Parser({ var preview = new Parser({
@ -1058,6 +1059,10 @@
for (var j = 0; j < preview.data.length; j++) for (var j = 0; j < preview.data.length; j++)
{ {
if (skipEmptyLines && preview.data[j].length === 1 && preview.data[j][0].length === 0) {
emptyLinesCount++
continue
}
var fieldCount = preview.data[j].length; var fieldCount = preview.data[j].length;
avgFieldCount += fieldCount; avgFieldCount += fieldCount;
@ -1074,7 +1079,7 @@
} }
if (preview.data.length > 0) if (preview.data.length > 0)
avgFieldCount /= preview.data.length; avgFieldCount /= (preview.data.length - emptyLinesCount);
if ((typeof bestDelta === 'undefined' || delta < bestDelta) if ((typeof bestDelta === 'undefined' || delta < bestDelta)
&& avgFieldCount > 1.99) && avgFieldCount > 1.99)
@ -1147,7 +1152,12 @@
var step = config.step; var step = config.step;
var preview = config.preview; var preview = config.preview;
var fastMode = config.fastMode; var fastMode = config.fastMode;
var quoteChar = config.quoteChar || '"'; /** Allows for no quoteChar by setting quoteChar to undefined in config */
if (config.quoteChar === undefined){
var quoteChar = '"';
} else {
var quoteChar = config.quoteChar;
}
// Delimiter must be valid // Delimiter must be valid
if (typeof delim !== 'string' if (typeof delim !== 'string'
@ -1245,6 +1255,7 @@
// Find closing quote // Find closing quote
var quoteSearch = input.indexOf(quoteChar, quoteSearch+1); var quoteSearch = input.indexOf(quoteChar, quoteSearch+1);
//No other quotes are found - no other delimiters
if (quoteSearch === -1) if (quoteSearch === -1)
{ {
if (!ignoreLastRow) { if (!ignoreLastRow) {
@ -1260,9 +1271,9 @@
return finish(); return finish();
} }
// Closing quote at EOF
if (quoteSearch === inputLen-1) if (quoteSearch === inputLen-1)
{ {
// Closing quote at EOF
var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar); var value = input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar);
return finish(value); return finish(value);
} }
@ -1274,9 +1285,9 @@
continue; continue;
} }
// Closing quote followed by delimiter
if (input[quoteSearch+1] === delim) if (input[quoteSearch+1] === delim)
{ {
// Closing quote followed by delimiter
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
cursor = quoteSearch + 1 + delimLen; cursor = quoteSearch + 1 + delimLen;
nextDelim = input.indexOf(delim, cursor); nextDelim = input.indexOf(delim, cursor);
@ -1284,9 +1295,9 @@
break; break;
} }
// Closing quote followed by newline
if (input.substr(quoteSearch+1, newlineLen) === newline) if (input.substr(quoteSearch+1, newlineLen) === newline)
{ {
// Closing quote followed by newline
row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar)); row.push(input.substring(cursor, quoteSearch).replace(quoteCharRegex, quoteChar));
saveRow(quoteSearch + 1 + newlineLen); saveRow(quoteSearch + 1 + newlineLen);
nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field nextDelim = input.indexOf(delim, cursor); // because we may have skipped the nextDelim in the quoted field
@ -1303,6 +1314,20 @@
break; break;
} }
// Checks for valid closing quotes are complete (escaped quotes or quote followed by EOF/delimiter/newline) -- assume these quotes are part of an invalid text string
errors.push({
type: 'Quotes',
code: 'InvalidQuotes',
message: 'Trailing quote on quoted field is malformed',
row: data.length, // row has yet to be inserted
index: cursor
});
quoteSearch++;
continue;
} }
continue; continue;

Loading…
Cancel
Save