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. 31
      papaparse.js

31
papaparse.js

@ -4,6 +4,16 @@ @@ -4,6 +4,16 @@
https://github.com/mholt/PapaParse
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)
{
/* globals define */
@ -41,7 +51,6 @@ @@ -41,7 +51,6 @@
return {};
})();
var IS_WORKER = !global.document && !!global.postMessage,
IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search),
LOADED_SYNC = false, AUTO_SCRIPT_PATH;
@ -290,9 +299,9 @@ @@ -290,9 +299,9 @@
if (typeof _input === 'string')
_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);
else if (typeof _input[0] === 'object')
return serialize(objectKeys(_input[0]), _input, _skipEmptyLines);
@ -302,17 +311,17 @@ @@ -302,17 +311,17 @@
if (typeof _input.data === 'string')
_input.data = JSON.parse(_input.data);
if (_input.data instanceof Array)
if (Array.isArray(_input.data))
{
if (!_input.fields)
_input.fields = _input.meta && _input.meta.fields;
if (!_input.fields)
_input.fields = _input.data[0] instanceof Array
_input.fields = Array.isArray(_input.data[0])
? _input.fields
: 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']
}
@ -335,7 +344,7 @@ @@ -335,7 +344,7 @@
}
if (typeof _config.quotes === 'boolean'
|| _config.quotes instanceof Array)
|| Array.isArray(_config.quotes))
_quotes = _config.quotes;
if (typeof _config.skipEmptyLines === 'boolean'
@ -374,8 +383,8 @@ @@ -374,8 +383,8 @@
if (typeof data === 'string')
data = JSON.parse(data);
var hasHeader = fields instanceof Array && fields.length > 0;
var dataKeyedByField = !(data[0] instanceof Array);
var hasHeader = Array.isArray(fields) && fields.length > 0;
var dataKeyedByField = !(Array.isArray(data[0]));
// If there a header row, write it first
if (hasHeader && _writeHeader)
@ -424,7 +433,7 @@ @@ -424,7 +433,7 @@
str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar);
var needsQuotes = (typeof _quotes === 'boolean' && _quotes)
|| (_quotes instanceof Array && _quotes[col])
|| (Array.isArray(_quotes) && _quotes[col])
|| hasAny(str, Papa.BAD_DELIMITERS)
|| str.indexOf(_delimiter) > -1
|| str.charAt(0) === ' '
@ -1778,7 +1787,7 @@ @@ -1778,7 +1787,7 @@
{
if (typeof obj !== 'object' || obj === null)
return obj;
var cpy = obj instanceof Array ? [] : {};
var cpy = Array.isArray(obj) ? [] : {};
for (var key in obj)
cpy[key] = copy(obj[key]);
return cpy;

Loading…
Cancel
Save