Browse Source

Use Array.isArray for determining arraynes (#570)

Also add polyfill in case it's not supported by browser version
pull/580/head
Jay L 6 years ago committed by Sergi Almacellas Abellana
parent
commit
4e0a80370f
  1. 39
      papaparse.js

39
papaparse.js

@ -1,9 +1,19 @@
/*@license /*@license
Papa Parse Papa Parse
v4.6.0 v4.6.0
https://github.com/mholt/PapaParse https://github.com/mholt/PapaParse
License: MIT License: MIT
*/ */
// Polyfills
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Polyfill
if (!Array.isArray)
{
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
(function(root, factory) (function(root, factory)
{ {
/* globals define */ /* globals define */
@ -41,7 +51,6 @@
return {}; return {};
})(); })();
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),
LOADED_SYNC = false, AUTO_SCRIPT_PATH; LOADED_SYNC = false, AUTO_SCRIPT_PATH;
@ -290,9 +299,9 @@
if (typeof _input === 'string') if (typeof _input === 'string')
_input = JSON.parse(_input); _input = JSON.parse(_input);
if (_input instanceof Array) if (Array.isArray(_input))
{ {
if (!_input.length || _input[0] instanceof Array) if (!_input.length || Array.isArray(_input[0]))
return serialize(null, _input, _skipEmptyLines); return serialize(null, _input, _skipEmptyLines);
else if (typeof _input[0] === 'object') else if (typeof _input[0] === 'object')
return serialize(objectKeys(_input[0]), _input, _skipEmptyLines); return serialize(objectKeys(_input[0]), _input, _skipEmptyLines);
@ -302,17 +311,17 @@
if (typeof _input.data === 'string') if (typeof _input.data === 'string')
_input.data = JSON.parse(_input.data); _input.data = JSON.parse(_input.data);
if (_input.data instanceof Array) if (Array.isArray(_input.data))
{ {
if (!_input.fields) if (!_input.fields)
_input.fields = _input.meta && _input.meta.fields; _input.fields = _input.meta && _input.meta.fields;
if (!_input.fields) if (!_input.fields)
_input.fields = _input.data[0] instanceof Array _input.fields = Array.isArray(_input.data[0])
? _input.fields ? _input.fields
: objectKeys(_input.data[0]); : objectKeys(_input.data[0]);
if (!(_input.data[0] instanceof Array) && typeof _input.data[0] !== 'object') if (!(Array.isArray(_input.data[0])) && 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']
} }
@ -335,7 +344,7 @@
} }
if (typeof _config.quotes === 'boolean' if (typeof _config.quotes === 'boolean'
|| _config.quotes instanceof Array) || Array.isArray(_config.quotes))
_quotes = _config.quotes; _quotes = _config.quotes;
if (typeof _config.skipEmptyLines === 'boolean' if (typeof _config.skipEmptyLines === 'boolean'
@ -374,8 +383,8 @@
if (typeof data === 'string') if (typeof data === 'string')
data = JSON.parse(data); data = JSON.parse(data);
var hasHeader = fields instanceof Array && fields.length > 0; var hasHeader = Array.isArray(fields) && fields.length > 0;
var dataKeyedByField = !(data[0] instanceof Array); var dataKeyedByField = !(Array.isArray(data[0]));
// If there a header row, write it first // If there a header row, write it first
if (hasHeader && _writeHeader) if (hasHeader && _writeHeader)
@ -424,7 +433,7 @@
str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar);
var needsQuotes = (typeof _quotes === 'boolean' && _quotes) var needsQuotes = (typeof _quotes === 'boolean' && _quotes)
|| (_quotes instanceof Array && _quotes[col]) || (Array.isArray(_quotes) && _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) === ' '
@ -1778,7 +1787,7 @@
{ {
if (typeof obj !== 'object' || obj === null) if (typeof obj !== 'object' || obj === null)
return obj; return obj;
var cpy = obj instanceof Array ? [] : {}; var cpy = Array.isArray(obj) ? [] : {};
for (var key in obj) for (var key in obj)
cpy[key] = copy(obj[key]); cpy[key] = copy(obj[key]);
return cpy; return cpy;

Loading…
Cancel
Save