diff --git a/papaparse.js b/papaparse.js index a945358..3cce77b 100755 --- a/papaparse.js +++ b/papaparse.js @@ -41,6 +41,14 @@ return {}; })(); + // 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]'; + }; + } var IS_WORKER = !global.document && !!global.postMessage, IS_PAPA_WORKER = IS_WORKER && /(\?|&)papaworker(=|&|$)/.test(global.location.search), @@ -290,9 +298,9 @@ if (typeof _input === 'string') _input = JSON.parse(_input); - if (isArray(_input)) + if (Array.isArray(_input)) { - if (!_input.length || isArray(_input[0])) + 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 +310,17 @@ if (typeof _input.data === 'string') _input.data = JSON.parse(_input.data); - if (isArray(_input.data)) + if (Array.isArray(_input.data)) { if (!_input.fields) _input.fields = _input.meta && _input.meta.fields; if (!_input.fields) - _input.fields = isArray(_input.data[0]) + _input.fields = Array.isArray(_input.data[0]) ? _input.fields : objectKeys(_input.data[0]); - if (!(isArray(_input.data[0])) && 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 +343,7 @@ } if (typeof _config.quotes === 'boolean' - || isArray(_config.quotes)) + || Array.isArray(_config.quotes)) _quotes = _config.quotes; if (typeof _config.skipEmptyLines === 'boolean' @@ -374,8 +382,8 @@ if (typeof data === 'string') data = JSON.parse(data); - var hasHeader = isArray(fields) && fields.length > 0; - var dataKeyedByField = !(isArray(data[0])); + 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 +432,7 @@ str = str.toString().replace(quoteCharRegex, _quoteChar + _quoteChar); var needsQuotes = (typeof _quotes === 'boolean' && _quotes) - || (isArray(_quotes) && _quotes[col]) + || (Array.isArray(_quotes) && _quotes[col]) || hasAny(str, Papa.BAD_DELIMITERS) || str.indexOf(_delimiter) > -1 || str.charAt(0) === ' ' @@ -1778,7 +1786,7 @@ { if (typeof obj !== 'object' || obj === null) return obj; - var cpy = isArray(obj) ? [] : {}; + var cpy = Array.isArray(obj) ? [] : {}; for (var key in obj) cpy[key] = copy(obj[key]); return cpy; @@ -1794,10 +1802,5 @@ return typeof func === 'function'; } - function isArray(obj) - { - return Object.prototype.toString.call(obj) === '[object Array]'; - } - return Papa; }));