Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6 lines
12 KiB

/*
Papa Parse
v3.0.1
https://github.com/mholt/PapaParse
*/
;(function(e){"use strict";function u(e,n){var r=t?n:g(n);var i=r.worker&&Papa.WORKERS_SUPPORTED;if(i){var s=d();s.userStep=r.step;s.userComplete=r.complete;s.userError=r.error;r.step=b(r.step);r.complete=b(r.complete);r.error=b(r.error);delete r.worker;s.postMessage({input:e,config:r,workerId:s.id})}else{if(typeof e==="string"){if(r.download){var o=new f(r);o.stream(e)}else{var u=new c(r);var a=u.parse(e);if(b(r.complete))r.complete(a);return a}}else if(e instanceof File){if(r.step){var o=new l(r);o.stream(e)}else{var u=new c(r);if(t){var h=new FileReaderSync;var p=h.readAsText(e,r.encoding);return u.parse(p)}else{h=new FileReader;h.onload=function(e){var t=new c(r);var n=t.parse(e.target.result);if(b(r.complete))r.complete(n)};h.readAsText(e,r.encoding)}}}}}function a(t,n){function a(){if(typeof n!=="object")return;if(typeof n.delimiter==="string"&&n.delimiter.length==1&&e.Papa.BAD_DELIMITERS.indexOf(n.delimiter)==-1){o=n.delimiter}if(typeof n.quotes==="boolean")s=n.quotes;if(typeof n.newline==="string")u=n.newline}function f(e){if(typeof e!=="object")return[];var t=[];for(var n in e)t.push(n);return t}function l(e,t){var n="";if(typeof e==="string")e=JSON.parse(e);if(typeof t==="string")t=JSON.parse(t);var r=e instanceof Array&&e.length>0;var i=!(t[0]instanceof Array);if(r){for(var s=0;s<e.length;s++){if(s>0)n+=o;n+=c(e[s])}if(t.length>0)n+=u}for(var a=0;a<t.length;a++){var f=r?e.length:t[a].length;for(var l=0;l<f;l++){if(l>0)n+=o;var h=r&&i?e[l]:l;n+=c(t[a][h])}if(a<t.length-1)n+=u}return n}function c(t){if(typeof t==="undefined")return"";t=t.toString().replace(/"/g,'""');var n=s||h(t,e.Papa.BAD_DELIMITERS)||t.indexOf(o)>-1||t.charAt(0)==" "||t.charAt(t.length-1)==" ";return n?'"'+t+'"':t}function h(e,t){for(var n=0;n<t.length;n++)if(e.indexOf(t[n])>-1)return true;return false}var r="";var i=[];var s=false;var o=",";var u="\r\n";a();if(typeof t==="string")t=JSON.parse(t);if(t instanceof Array){if(!t.length||t[0]instanceof Array)return l(null,t);else if(typeof t[0]==="object")return l(f(t[0]),t)}else if(typeof t==="object"){if(typeof t.data==="string")t.data=JSON.parse(t.data);if(t.data instanceof Array){if(!t.fields)t.fields=t.data[0]instanceof Array?t.fields:f(t.data[0]);if(!(t.data[0]instanceof Array)&&typeof t.data[0]!=="object")t.data=[t.data];return l(t.fields,t.data)}}throw"exception: Unable to serialize unrecognized input"}function f(n){n=n||{};if(!n.chunkSize)n.chunkSize=1024*1024*5;var r=0,i=0;var s="";var o="";var u,a;var f=new c(y(n));this.stream=function(l){function c(){u=new XMLHttpRequest;if(!t){u.onload=h;u.onerror=p}u.open("GET",l,!t);if(n.step){var e=r+n.chunkSize-1;if(i&&e>i)e=i;u.setRequestHeader("Range","bytes="+r+"-"+e)}u.send();if(t&&u.status==0)p();else r+=n.chunkSize}function h(){if(u.readyState!=4)return;if(u.status<200||u.status>=400){p();return}s+=o+u.responseText;o="";var i=!n.step||r>d(u);if(!i){var l=s.lastIndexOf("\n");if(l<0)l=s.lastIndexOf("\r");if(l>-1){o=s.substring(l+1);s=s.substring(0,l)}else{a();return}}var c=f.parse(s);s="";if(t){e.postMessage({results:c,workerId:Papa.WORKER_ID,finished:i})}if(i&&b(n.complete))n.complete(c);else if(c&&c.meta.aborted&&b(n.complete))n.complete(c);else if(!i)a()}function p(){if(b(n.error))n.error(u.statusText);else if(t&&n.error){e.postMessage({workerId:Papa.WORKER_ID,error:u.statusText,finished:false})}}function d(e){var t=e.getResponseHeader("Content-Range");return parseInt(t.substr(t.lastIndexOf("/")+1))}if(t){a=function(){c();h()}}else{a=function(){c()}}a()}}function l(n){n=n||{};if(!n.chunkSize)n.chunkSize=1024*1024*10;var r=0;var i="";var s="";var o,u,a;var f=new c(y(n));this.stream=function(u){function l(){if(r<u.size)c()}function c(){var e=Math.min(r+n.chunkSize,u.size);var t=o.readAsText(a.call(u,r,e),n.encoding);r+=n.chunkSize;return t}function h(o){i+=s+o.target.result;s="";var a=r>=u.size;if(!a){var c=i.lastIndexOf("\n");if(c<0)c=i.lastIndexOf("\r");if(c>-1){s=i.substring(c+1);i=i.substring(0,c)}else{l();return}}var h=f.parse(i);i="";if(t){e.postMessage({results:h,workerId:Papa.WORKER_ID,finished:a})}if(a&&b(n.complete))n.complete(unde