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
13 KiB
6 lines
13 KiB
/* |
|
Papa Parse |
|
v3.1.4 |
|
https://github.com/mholt/PapaParse |
|
*/ |
|
;(function(e){"use strict";function u(r,i){var s=t?i:g(i);var o=s.worker&&Papa.WORKERS_SUPPORTED&&n;if(o){var u=d();u.userStep=s.step;u.userChunk=s.chunk;u.userComplete=s.complete;u.userError=s.error;s.step=b(s.step);s.chunk=b(s.chunk);s.complete=b(s.complete);s.error=b(s.error);delete s.worker;u.postMessage({input:r,config:s,workerId:u.id})}else{if(typeof r==="string"){if(s.download){var a=new f(s);a.stream(r)}else{var h=new c(s);var p=h.parse(r);return p}}else if(e.File&&r instanceof File||r instanceof Object){if(s.step||s.chunk){var a=new l(s);a.stream(r)}else{var h=new c(s);if(t){var v=new FileReaderSync;var m=v.readAsText(r,s.encoding);return h.parse(m)}else{v=new FileReader;v.onload=function(e){var t=new c(s);var n=t.parse(e.target.result)};v.onerror=function(){if(b(s.error))s.error(v.error,r)};v.readAsText(r,s.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 p(){if(l){d();return}u=new XMLHttpRequest;if(!t){u.onload=d;u.onerror=v}u.open("GET",a,!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)v();else r+=n.chunkSize}function d(){if(u.readyState!=4)return;if(u.status<200||u.status>=400){v();return}s+=o+u.responseText;o="";l=!n.step||r>m(u);if(!l){var i=s.lastIndexOf("\n");if(i<0)i=s.lastIndexOf("\r");if(i>-1){o=s.substring(i+1);s=s.substring(0,i)}else{f();return}}var a=h.parse(s);s="";if(t){e.postMessage({results:a,workerId:Papa.WORKER_ID,finished:l})}else if(b(n.chunk)){console.log("CHUNKED");n.chunk(a);a=undefined}if(!l&&!a.meta.paused)f()}function v(){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 m(e){var t=e.getResponseHeader("Content-Range");return parseInt(t.substr(t.lastIndexOf("/")+1))}n=n||{};if(!n.chunkSize)n.chunkSize=Papa.RemoteChunkSize;var r=0,i=0;var s="";var o="";var u,a,f,l;var h=new c(y(n));h.streamer=this;this.resume=function(){f()};this.finished=function(){return l};this.stream=function(e){a=e;if(t){f=function(){p();d()}}else{f=function(){p()}}f()}}function l(n){function f(){if(!l)d()}function d(){var e=Math.min(r+n.chunkSize,i.size);var t=a.readAsText(s.call(i,r,e),n.encoding);if(!p)v({target:{result:t}})}function v(s){r+=n.chunkSize;o+=u+s.target.result;u="";l=r>=i.size;if(!l){var a=o.lastIndexOf("\n");if(a<0)a=o.lastIndexOf("\r");if(a>-1){u=o.substring(a+1);o=o.substring(0,a)}else{f();return}}var c=h.parse(o);o="";if(t){e.postMessage({results:c,workerId:Papa.WORKER_ID,finished:l})}else if(b(n.chunk)){n.chunk(c,i);c=undefined}if(!c||!c.meta.paused)f()}function m(){if(b(n.error))n.error(a.error,i);else if(t&&n.error){e.postMessage({workerId:Papa.WORKER_ID,error:a.error,file:i,finished:false})}}n=n||{};if(!n.chunkSize)n.chunkSize=Papa.LocalChunkSize;var r=0;var i;var s;var o="";var u="";var a,f,s,l;var h=new c(y(n));h.streamer=this;var p=typeof FileReader!=="undefined";this.stream=function(e){i=e;s=i.slice||i.webkitSlice||i.mozSlice;if(p){a=new FileReader;a.onload=v;a.onerror=m}else a=new FileReaderSync;f()};this.finished=function(){return l};this.resume=function(){f()}}function c(e){function c(){if(f&&u){w("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to '"+Papa.DefaultDelimiter+"'");u=false}if(p())d();return v()}function p(){return e.header&&a.length==0}function d(){if(!f)return;for(var e=0;p()&&e<f.data.length;e++)for(var t=0;t<f.data[e].length;t++)a.push(f.data[e][t]);f.data.splice(0,1)}function v(){if(!f||!e.header&&!e.dynamicTyping)return f;for(var t=0;t<f.data.length;t++){var n={};for(var r=0;r<f.data[t].length;r++){if(e.dynamicTyping){var i=f.data[t][r];if(i==="true"||i==="TRUE")f.data[t][r]=true;else if(i==="false"||i==="FALSE")f.data[t][r]=false;else f.data[t][r]=g(i)}if(e.header){if(r>=a.length){if(!n["__parsed_extra"])n["__parsed_extra"]=[];n["__parsed_extra"].push(f.data[t][r])}else n[a[r]]=f.data[t][r]}}if(e.header){f.data[t]=n;if(r>a.length)w("FieldMismatch","TooManyFields","Too many fields: expected "+a.length+" fields but parsed "+r,t);else if(r<a.length)w("FieldMismatch","TooFewFields","Too few fields: expected "+a.length+" fields but parsed "+r,t)}}if(e.header&&f.meta)f.meta.fields=a;return f}function m(t){var n=[","," ","|",";",Papa.RECORD_SEP,Papa.UNIT_SEP];var r,i,s;for(var o=0;o<n.length;o++){var u=n[o];var a=0,f=0;s=undefined;var l=(new h({delimiter:u,preview:10})).parse(t);for(var c=0;c<l.data.length;c++){var p=l.data[c].length;f+=p;if(typeof s==="undefined"){s=p;continue}else if(p>1){a+=Math.abs(p-s);s=p}}f/=l.data.length;if((typeof i==="undefined"||a<i)&&f>1.99){i=a;r=u}}e.delimiter=r;return{successful:!!r,bestDelimiter:r}}function g(e){var n=t.test(e);return n?parseFloat(e):e}function w(e,t,n,r){f.errors.push({type:e,code:t,message:n,row:r})}var t=/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;var n=this;var r=0;var i;var s;var o=false;var u;var a=[];var f={data:[],errors:[],meta:{}};if(b(e.step)){var l=e.step;e.step=function(t){f=t;if(p())c();else{r+=t.data.length;if(e.preview&&r>e.preview)s.abort();else l(c(),n)}}}this.parse=function(t){u=false;if(!e.delimiter){var r=m(t);if(r.successful)e.delimiter=r.bestDelimiter;else{u=true;e.delimiter=Papa.DefaultDelimiter}f.meta.delimiter=e.delimiter}var a=y(e);if(e.preview&&e.header)a.preview++;i=t;s=new h(a);f=s.parse(i);c();if(b(e.complete)&&!o&&(!n.streamer||n.streamer.finished()))e.complete(f);return o?{meta:{paused:true}}:f||{meta:{paused:false}}};this.pause=function(){o=true;s.abort();i=i.substr(s.getCharIndex())};this.resume=function(){o=false;s=new h(e);s.parse(i);if(!o){if(n.streamer&&!n.streamer.finished())n.streamer.resume();else if(b(e.complete))e.complete(f)}};this.abort=function(){s.abort();if(b(e.complete))e.complete(f);i=""}}function h(e){function y(){while(f<n.length){if(g)break;if(u>0&&m>=u)break;if(a=='"')S();else if(l)x();else T();w()}return E()}function w(){f++;a=n[f]}function E(){if(g)j("Abort","ParseAbort","Parsing was aborted by the user's step function");if(l)j("Quotes","MissingQuotes","Unescaped or mismatched quotes");O();if(!b(s))return q()}function S(){if(H()&&!P())l=!l;else{k();if(l&&P())f++;else j("Quotes","UnexpectedQuotes","Unexpected quotes")}}function x(){if(_(f)||D(f))c++;k()}function T(){if(a==r)L();else if(_(f)){A();w()}else if(D(f))A();else if(N())C();else k()}function N(){if(!i)return false;var e=f==0||D(f-1)||_(f-2);return e&&n[f]===i}function C(){while(!_(f)&&!D(f)&&f<n.length){w()}}function k(){h[d][v]+=a}function L(){h[d].push("");v=h[d].length-1}function A(){O();c++;m++;h.push([]);d=h.length-1;L()}function O(){M();if(b(s)){if(h[d])s(q());I()}}function M(){if(h[d].length==1&&t.test(h[d][0])){if(e.keepEmptyRows)h[d].splice(0,1);else h.splice(d,1);d=h.length-1}}function _(e){return e<n.length-1&&(n[e]=="\r"&&n[e+1]=="\n"||n[e]=="\n"&&n[e+1]=="\r")}function D(e){return n[e]=="\r"||n[e]=="\n"}function P(){return!H()&&f<n.length-1&&n[f+1]=='"'}function H(){return!l&&B(f-1)||B(f+1)}function B(e){if(typeof e!="number")e=f;var t=n[e];return e<=-1||e>=n.length||t==r||t=="\r"||t=="\n"}function j(e,t,n){p.push({type:e,code:t,message:n,line:c,row:d,index:f})}function F(e){n=e;l=false;f=0,m=0,c=1;I();h=[[""]];a=n[f]}function I(){h=[];p=[];d=0;v=0}function q(){return{data:h,errors:p,meta:{lines:c,delimiter:r,aborted:g,truncated:u>0&&f<n.length}}}var t=/^\s*$/;var n;var r;var i;var s;var o;var u;var a;var f;var l;var c;var h;var p;var d;var v;var m;var g=false;e=e||{};r=e.delimiter;i=e.comments;s=e.step;u=e.preview;if(typeof r!=="string"||r.length!=1||Papa.BAD_DELIMITERS.indexOf(r)>-1)r=",";if(i===true)i="#";else if(typeof i!=="string"||i.length!=1||Papa.BAD_DELIMITERS.indexOf(i)>-1||i==r)i=false;this.parse=function(e){if(typeof e!=="string")throw"Input must be a string";F(e);return y()};this.abort=function(){g=true};this.getCharIndex=function(){return f}}function p(){var e="worker"+String(Math.random()).substr(2);document.write('<script id="'+e+'"></s'+"cript>");return document.getElementById(e).previousSibling.src}function d(){if(!Papa.WORKERS_SUPPORTED)return false;var t=new e.Worker(n);t.onmessage=v;t.id=i++;r[t.id]=t;return t}function v(e){var t=e.data;var n=r[t.workerId];if(t.error)n.userError(t.error,t.file);else if(t.results&&t.results.data){if(b(n.userStep)){for(var i=0;i<t.results.data.length;i++){n.userStep({data:[t.results.data[i]],errors:t.results.errors,meta:t.results.meta})}delete t.results}else if(b(n.userChunk)){n.userChunk(t.results,t.file);delete t.results}}if(t.finished){if(b(r[t.workerId].userComplete))r[t.workerId].userComplete(t.results);r[t.workerId].terminate();delete r[t.workerId]}}function m(t){var n=t.data;if(typeof Papa.WORKER_ID==="undefined"&&n)Papa.WORKER_ID=n.workerId;if(typeof n.input==="string"){e.postMessage({workerId:Papa.WORKER_ID,results:Papa.parse(n.input,n.config),finished:true})}else if(e.File&&n.input instanceof File||n.input instanceof Object){var r=Papa.parse(n.input,n.config);if(r)e.postMessage({workerId:Papa.WORKER_ID,results:r,finished:true})}}function g(e){if(typeof e!=="object")e={};var t=y(e);if(typeof t.delimiter!=="string"||t.delimiter.length!=1||Papa.BAD_DELIMITERS.indexOf(t.delimiter)>-1)t.delimiter=s.delimiter;if(typeof t.header!=="boolean")t.header=s.header;if(typeof t.dynamicTyping!=="boolean")t.dynamicTyping=s.dynamicTyping;if(typeof t.preview!=="number")t.preview=s.preview;if(typeof t.step!=="function")t.step=s.step;if(typeof t.complete!=="function")t.complete=s.complete;if(typeof t.error!=="function")t.error=s.error;if(typeof t.encoding!=="string")t.encoding=s.encoding;if(typeof t.worker!=="boolean")t.worker=s.worker;if(typeof t.download!=="boolean")t.download=s.download;if(typeof t.keepEmptyRows!=="boolean")t.keepEmptyRows=s.keepEmptyRows;return t}function y(e){if(typeof e!=="object")return e;var t=e instanceof Array?[]:{};for(var n in e)t[n]=y(e[n]);return t}function b(e){return typeof e==="function"}var t=!e.document,n;var r={},i=0;var s={delimiter:"",header:false,dynamicTyping:false,preview:0,step:undefined,encoding:"",worker:false,comments:false,complete:undefined,error:undefined,download:false,chunk:undefined,keepEmptyRows:false};e.Papa={};e.Papa.parse=u;e.Papa.unparse=a;e.Papa.RECORD_SEP=String.fromCharCode(30);e.Papa.UNIT_SEP=String.fromCharCode(31);e.Papa.BYTE_ORDER_MARK="";e.Papa.BAD_DELIMITERS=["\r","\n",'"',e.Papa.BYTE_ORDER_MARK];e.Papa.WORKERS_SUPPORTED=!!e.Worker;e.Papa.LocalChunkSize=1024*1024*10;e.Papa.RemoteChunkSize=1024*1024*5;e.Papa.DefaultDelimiter=",";e.Papa.Parser=h;e.Papa.ParserHandle=c;e.Papa.NetworkStreamer=f;e.Papa.FileStreamer=l;if(e.jQuery){var o=e.jQuery;o.fn.parse=function(t){function i(){if(r.length==0)return;var e=r[0];if(b(t.before)){var n=t.before(e.file,e.inputElem);if(typeof n==="object"){if(n.action=="abort"){s("AbortError",e.file,e.inputElem,n.reason);return}else if(n.action=="skip"){u();return}else if(typeof n.config==="object")e.instanceConfig=o.extend(e.instanceConfig,n.config)}else if(n=="skip"){u();return}}var i=e.instanceConfig.complete;e.instanceConfig.complete=function(t){if(b(i))i(t,e.file,e.inputElem);u()};Papa.parse(e.file,e.instanceConfig)}function s(e,n,r,i){if(b(t.error))t.error({name:e},n,r,i)}function u(){r.splice(0,1);i()}var n=t.config||{};var r=[];this.each(function(t){var i=o(this).prop("tagName").toUpperCase()=="INPUT"&&o(this).attr("type").toLowerCase()=="file"&&e.FileReader;if(!i||!this.files||this.files.length==0)return true;for(var s=0;s<this.files.length;s++){r.push({file:this.files[s],inputElem:this,instanceConfig:o.extend({},n)})}});i();return this}}if(t)e.onmessage=m;else if(Papa.WORKERS_SUPPORTED)n=p()})(this); |