diff --git a/papaparse.js b/papaparse.js index 059ccbb..31b58f5 100755 --- a/papaparse.js +++ b/papaparse.js @@ -1,9 +1,19 @@ /*@license - Papa Parse - v4.6.0 - https://github.com/mholt/PapaParse - License: MIT +Papa Parse +v4.6.0 +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 @@ 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 @@ 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 @@ 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 @@ } 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 @@ 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 @@ 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 @@ { 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;