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
14 KiB

/*
Papa Parse
v4.0.4
https://github.com/mholt/PapaParse
*/
;(function(e){"use strict";function u(e,r){var i=t?r:g(r);var s=i.worker&&Papa.WORKERS_SUPPORTED&&n;if(s){var o=d();o.userStep=i.step;o.userChunk=i.chunk;o.userComplete=i.complete;o.userError=i.error;i.step=b(i.step);i.chunk=b(i.chunk);i.complete=b(i.complete);i.error=b(i.error);delete i.worker;o.postMessage({input:e,config:i,workerId:o.id})}else{if(typeof e==="string"){if(i.download){var u=new f(i);u.stream(e)}else{var a=new c(i);var h=a.parse(e);return h}}else if(e instanceof File){if(i.step||i.chunk){var u=new l(i);u.stream(e)}else{var a=new c(i);if(t){var p=new FileReaderSync;var v=p.readAsText(e,i.encoding);return a.parse(v)}else{p=new FileReader;p.onload=function(e){var t=new c(i);var n=t.parse(e.target.result)};p.onerror=function(){if(b(i.error))i.error(p.error,e)};p.readAsText(e,i.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"||n.quotes instanceof Array)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],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],l)}if(a<t.length-1)n+=u}return n}function c(t,n){if(typeof t==="undefined"||t===null)return"";t=t.toString().replace(/"/g,'""');var r=typeof s==="boolean"&&s||s instanceof Array&&s[n]||h(t,e.Papa.BAD_DELIMITERS)||t.indexOf(o)>-1||t.charAt(0)==" "||t.charAt(t.length-1)==" ";return r?'"'+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){function m(){if(h){g();return}a=new XMLHttpRequest;if(!t){a.onload=g;a.onerror=w}a.open("GET",f,!t);if(n.step||n.chunk){var e=r+v.chunkSize-1;if(i&&e>i)e=i;a.setRequestHeader("Range","bytes="+r+"-"+e)}try{a.send()}catch(s){w(s.message)}if(t&&a.status==0)w();else r+=v.chunkSize}function g(){if(a.readyState!=4)return;if(a.status<200||a.status>=400){w();return}o+=u+a.responseText;u="";h=!n.step&&!n.chunk||r>E(a);if(!h){var i=o.lastIndexOf("\r");if(i==-1)i=o.lastIndexOf("\n");if(i!=-1){u=o.substring(i+1);o=o.substring(0,i)}else{l();return}}var f=d.parse(o);o="";if(f&&f.data)s+=f.data.length;var c=h||v.preview&&s>=v.preview;if(t){e.postMessage({results:f,workerId:Papa.WORKER_ID,finished:c})}else if(b(n.chunk)){n.chunk(f,d);f=undefined}if(b(p)&&c)p(f);if(!c&&(!f||!f.meta.paused))l()}function w(r){var i=a.statusText||r;if(b(n.error))n.error(i);else if(t&&n.error){e.postMessage({workerId:Papa.WORKER_ID,error:i,finished:false})}}function E(e){var t=e.getResponseHeader("Content-Range");return parseInt(t.substr(t.lastIndexOf("/")+1))}function S(e){v=y(e);p=v.complete;v.complete=undefined;v.chunkSize=parseInt(v.chunkSize);d=new c(v);d.streamer=this}n=n||{};if(!n.chunkSize)n.chunkSize=Papa.RemoteChunkSize;var r=0,i=0,s=0;var o="";var u="";var a,f,l,h;var p,d,v;S(n);this.resume=function(){paused=false;l()};this.finished=function(){return h};this.pause=function(){paused=true};this.abort=function(){h=true;if(b(p))p({data:[],errors:[],meta:{aborted:true}})};this.stream=function(e){f=e;if(t){l=function(){m();g()}}else{l=function(){m()}}l()}}function l(n){