forked from lombardpress/collation-vizualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
papaparse.min.js
7 lines (7 loc) · 16.7 KB
/
papaparse.min.js
1
2
3
4
5
6
7
/*@license
Papa Parse
v4.4.0
https://github.com/mholt/PapaParse
License: MIT
*/
!function(e,t){"function"==typeof define&&define.amd?define([],t):"object"==typeof module&&"undefined"!=typeof exports?module.exports=t():e.Papa=t()}(this,function(){"use strict";var s,e,f="undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0!==f?f:{},r=!f.document&&!!f.postMessage,o=r&&/(\?|&)papaworker(=|&|$)/.test(f.location.search),a=!1,h={},u=0,g={parse:function(e,t){var i=(t=t||{}).dynamicTyping||!1;z(i)&&(t.dynamicTypingFunction=i,i={});if(t.dynamicTyping=i,t.worker&&g.WORKERS_SUPPORTED){var n=function(){if(!g.WORKERS_SUPPORTED)return!1;if(!a&&null===g.SCRIPT_PATH)throw new Error("Script path cannot be determined automatically when Papa Parse is loaded asynchronously. You need to set Papa.SCRIPT_PATH manually.");var e=g.SCRIPT_PATH||s;e+=(-1!==e.indexOf("?")?"&":"?")+"papaworker";var t=new f.Worker(e);return t.onmessage=k,t.id=u++,h[t.id]=t}();return n.userStep=t.step,n.userChunk=t.chunk,n.userComplete=t.complete,n.userError=t.error,t.step=z(t.step),t.chunk=z(t.chunk),t.complete=z(t.complete),t.error=z(t.error),delete t.worker,void n.postMessage({input:e,config:t,workerId:n.id})}var r=null;"string"==typeof e?r=t.download?new l(t):new _(t):!0===e.readable&&z(e.read)&&z(e.on)?r=new m(t):(f.File&&e instanceof File||e instanceof Object)&&(r=new p(t));return r.stream(e)},unparse:function(e,t){var n=!1,f=!0,d=",",c="\r\n",r='"';!function(){if("object"!=typeof t)return;"string"==typeof t.delimiter&&1===t.delimiter.length&&-1===g.BAD_DELIMITERS.indexOf(t.delimiter)&&(d=t.delimiter);("boolean"==typeof t.quotes||t.quotes instanceof Array)&&(n=t.quotes);"string"==typeof t.newline&&(c=t.newline);"string"==typeof t.quoteChar&&(r=t.quoteChar);"boolean"==typeof t.header&&(f=t.header)}();var s=new RegExp(r,"g");"string"==typeof e&&(e=JSON.parse(e));if(e instanceof Array){if(!e.length||e[0]instanceof Array)return a(null,e);if("object"==typeof e[0])return a(i(e[0]),e)}else if("object"==typeof e)return"string"==typeof e.data&&(e.data=JSON.parse(e.data)),e.data instanceof Array&&(e.fields||(e.fields=e.meta&&e.meta.fields),e.fields||(e.fields=e.data[0]instanceof Array?e.fields:i(e.data[0])),e.data[0]instanceof Array||"object"==typeof e.data[0]||(e.data=[e.data])),a(e.fields||[],e.data||[]);throw"exception: Unable to serialize unrecognized input";function i(e){if("object"!=typeof e)return[];var t=[];for(var i in e)t.push(i);return t}function a(e,t){var i="";"string"==typeof e&&(e=JSON.parse(e)),"string"==typeof t&&(t=JSON.parse(t));var n=e instanceof Array&&0<e.length,r=!(t[0]instanceof Array);if(n&&f){for(var s=0;s<e.length;s++)0<s&&(i+=d),i+=l(e[s],s);0<t.length&&(i+=c)}for(var a=0;a<t.length;a++){for(var o=n?e.length:t[a].length,h=0;h<o;h++){0<h&&(i+=d);var u=n&&r?e[h]:h;i+=l(t[a][u],h)}a<t.length-1&&(i+=c)}return i}function l(e,t){if(null==e)return"";e=e.toString().replace(s,r+r);var i="boolean"==typeof n&&n||n instanceof Array&&n[t]||function(e,t){for(var i=0;i<t.length;i++)if(-1<e.indexOf(t[i]))return!0;return!1}(e,g.BAD_DELIMITERS)||-1<e.indexOf(d)||" "===e.charAt(0)||" "===e.charAt(e.length-1);return i?r+e+r:e}}};if(g.RECORD_SEP=String.fromCharCode(30),g.UNIT_SEP=String.fromCharCode(31),g.BYTE_ORDER_MARK="\ufeff",g.BAD_DELIMITERS=["\r","\n",'"',g.BYTE_ORDER_MARK],g.WORKERS_SUPPORTED=!r&&!!f.Worker,g.SCRIPT_PATH=null,g.LocalChunkSize=10485760,g.RemoteChunkSize=5242880,g.DefaultDelimiter=",",g.Parser=v,g.ParserHandle=i,g.NetworkStreamer=l,g.FileStreamer=p,g.StringStreamer=_,g.ReadableStreamStreamer=m,f.jQuery){var d=f.jQuery;d.fn.parse=function(o){var i=o.config||{},h=[];return this.each(function(e){if(!("INPUT"===d(this).prop("tagName").toUpperCase()&&"file"===d(this).attr("type").toLowerCase()&&f.FileReader)||!this.files||0===this.files.length)return!0;for(var t=0;t<this.files.length;t++)h.push({file:this.files[t],inputElem:this,instanceConfig:d.extend({},i)})}),e(),this;function e(){if(0!==h.length){var e,t,i,n,r=h[0];if(z(o.before)){var s=o.before(r.file,r.inputElem);if("object"==typeof s){if("abort"===s.action)return e="AbortError",t=r.file,i=r.inputElem,n=s.reason,void(z(o.error)&&o.error({name:e},t,i,n));if("skip"===s.action)return void u();"object"==typeof s.config&&(r.instanceConfig=d.extend(r.instanceConfig,s.config))}else if("skip"===s)return void u()}var a=r.instanceConfig.complete;r.instanceConfig.complete=function(e){z(a)&&a(e,r.file,r.inputElem),u()},g.parse(r.file,r.instanceConfig)}else z(o.complete)&&o.complete()}function u(){h.splice(0,1),e()}}}function c(e){this._handle=null,this._finished=!1,this._completed=!1,this._input=null,this._baseIndex=0,this._partialLine="",this._rowCount=0,this._start=0,this._nextChunk=null,this.isFirstChunk=!0,this._completeResults={data:[],errors:[],meta:{}},function(e){var t=C(e);t.chunkSize=parseInt(t.chunkSize),e.step||e.chunk||(t.chunkSize=null);this._handle=new i(t),(this._handle.streamer=this)._config=t}.call(this,e),this.parseChunk=function(e,t){if(this.isFirstChunk&&z(this._config.beforeFirstChunk)){var i=this._config.beforeFirstChunk(e);void 0!==i&&(e=i)}this.isFirstChunk=!1;var n=this._partialLine+e;this._partialLine="";var r=this._handle.parse(n,this._baseIndex,!this._finished);if(!this._handle.paused()&&!this._handle.aborted()){var s=r.meta.cursor;this._finished||(this._partialLine=n.substring(s-this._baseIndex),this._baseIndex=s),r&&r.data&&(this._rowCount+=r.data.length);var a=this._finished||this._config.preview&&this._rowCount>=this._config.preview;if(o)f.postMessage({results:r,workerId:g.WORKER_ID,finished:a});else if(z(this._config.chunk)&&!t){if(this._config.chunk(r,this._handle),this._handle.paused()||this._handle.aborted())return;r=void 0,this._completeResults=void 0}return this._config.step||this._config.chunk||(this._completeResults.data=this._completeResults.data.concat(r.data),this._completeResults.errors=this._completeResults.errors.concat(r.errors),this._completeResults.meta=r.meta),this._completed||!a||!z(this._config.complete)||r&&r.meta.aborted||(this._config.complete(this._completeResults,this._input),this._completed=!0),a||r&&r.meta.paused||this._nextChunk(),r}},this._sendError=function(e){z(this._config.error)?this._config.error(e):o&&this._config.error&&f.postMessage({workerId:g.WORKER_ID,error:e,finished:!1})}}function l(e){var n;(e=e||{}).chunkSize||(e.chunkSize=g.RemoteChunkSize),c.call(this,e),this._nextChunk=r?function(){this._readChunk(),this._chunkLoaded()}:function(){this._readChunk()},this.stream=function(e){this._input=e,this._nextChunk()},this._readChunk=function(){if(this._finished)this._chunkLoaded();else{if(n=new XMLHttpRequest,this._config.withCredentials&&(n.withCredentials=this._config.withCredentials),r||(n.onload=w(this._chunkLoaded,this),n.onerror=w(this._chunkError,this)),n.open("GET",this._input,!r),this._config.downloadRequestHeaders){var e=this._config.downloadRequestHeaders;for(var t in e)n.setRequestHeader(t,e[t])}if(this._config.chunkSize){var i=this._start+this._config.chunkSize-1;n.setRequestHeader("Range","bytes="+this._start+"-"+i),n.setRequestHeader("If-None-Match","webkit-no-cache")}try{n.send()}catch(e){this._chunkError(e.message)}r&&0===n.status?this._chunkError():this._start+=this._config.chunkSize}},this._chunkLoaded=function(){4===n.readyState&&(n.status<200||400<=n.status?this._chunkError():(this._finished=!this._config.chunkSize||this._start>function(e){var t=e.getResponseHeader("Content-Range");if(null===t)return-1;return parseInt(t.substr(t.lastIndexOf("/")+1))}(n),this.parseChunk(n.responseText)))},this._chunkError=function(e){var t=n.statusText||e;this._sendError(new Error(t))}}function p(e){var n,r;(e=e||{}).chunkSize||(e.chunkSize=g.LocalChunkSize),c.call(this,e);var s="undefined"!=typeof FileReader;this.stream=function(e){this._input=e,r=e.slice||e.webkitSlice||e.mozSlice,s?((n=new FileReader).onload=w(this._chunkLoaded,this),n.onerror=w(this._chunkError,this)):n=new FileReaderSync,this._nextChunk()},this._nextChunk=function(){this._finished||this._config.preview&&!(this._rowCount<this._config.preview)||this._readChunk()},this._readChunk=function(){var e=this._input;if(this._config.chunkSize){var t=Math.min(this._start+this._config.chunkSize,this._input.size);e=r.call(e,this._start,t)}var i=n.readAsText(e,this._config.encoding);s||this._chunkLoaded({target:{result:i}})},this._chunkLoaded=function(e){this._start+=this._config.chunkSize,this._finished=!this._config.chunkSize||this._start>=this._input.size,this.parseChunk(e.target.result)},this._chunkError=function(){this._sendError(n.error)}}function _(e){var i;c.call(this,e=e||{}),this.stream=function(e){return i=e,this._nextChunk()},this._nextChunk=function(){if(!this._finished){var e=this._config.chunkSize,t=e?i.substr(0,e):i;return i=e?i.substr(e):"",this._finished=!i,this.parseChunk(t)}}}function m(e){c.call(this,e=e||{});var t=[],i=!0,n=!1;this.pause=function(){c.prototype.pause.apply(this,arguments),this._input.pause()},this.resume=function(){c.prototype.resume.apply(this,arguments),this._input.resume()},this.stream=function(e){this._input=e,this._input.on("data",this._streamData),this._input.on("end",this._streamEnd),this._input.on("error",this._streamError)},this._checkIsFinished=function(){n&&1===t.length&&(this._finished=!0)},this._nextChunk=function(){this._checkIsFinished(),t.length?this.parseChunk(t.shift()):i=!0},this._streamData=w(function(e){try{t.push("string"==typeof e?e:e.toString(this._config.encoding)),i&&(i=!1,this._checkIsFinished(),this.parseChunk(t.shift()))}catch(e){this._streamError(e)}},this),this._streamError=w(function(e){this._streamCleanUp(),this._sendError(e)},this),this._streamEnd=w(function(){this._streamCleanUp(),n=!0,this._streamData("")},this),this._streamCleanUp=w(function(){this._input.removeListener("data",this._streamData),this._input.removeListener("end",this._streamEnd),this._input.removeListener("error",this._streamError)},this)}function i(_){var s,a,o,n=/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,t=this,i=0,h=!1,e=!1,u=[],f={data:[],errors:[],meta:{}};if(z(_.step)){var r=_.step;_.step=function(e){if(f=e,c())d();else{if(d(),0===f.data.length)return;i+=e.data.length,_.preview&&i>_.preview?a.abort():r(f,t)}}}function d(){if(f&&o&&(p("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to '"+g.DefaultDelimiter+"'"),o=!1),_.skipEmptyLines)for(var e=0;e<f.data.length;e++)1===f.data[e].length&&""===f.data[e][0]&&f.data.splice(e--,1);return c()&&function(){if(!f)return;for(var e=0;c()&&e<f.data.length;e++)for(var t=0;t<f.data[e].length;t++){var i=f.data[e][t];_.trimHeaders&&(i=i.trim()),u.push(i)}f.data.splice(0,1)}(),function(){if(!f||!_.header&&!_.dynamicTyping)return f;for(var e=0;e<f.data.length;e++){var t,i=_.header?{}:[];for(t=0;t<f.data[e].length;t++){var n=t,r=f.data[e][t];_.header&&(n=t>=u.length?"__parsed_extra":u[t]),r=l(n,r),"__parsed_extra"===n?(i[n]=i[n]||[],i[n].push(r)):i[n]=r}f.data[e]=i,_.header&&(t>u.length?p("FieldMismatch","TooManyFields","Too many fields: expected "+u.length+" fields but parsed "+t,e):t<u.length&&p("FieldMismatch","TooFewFields","Too few fields: expected "+u.length+" fields but parsed "+t,e))}_.header&&f.meta&&(f.meta.fields=u);return f}()}function c(){return _.header&&0===u.length}function l(e,t){return i=e,_.dynamicTypingFunction&&void 0===_.dynamicTyping[i]&&(_.dynamicTyping[i]=_.dynamicTypingFunction(i)),!0===(_.dynamicTyping[i]||_.dynamicTyping)?"true"===t||"TRUE"===t||"false"!==t&&"FALSE"!==t&&(n.test(t)?parseFloat(t):""===t?null:t):t;var i}function p(e,t,i,n){f.errors.push({type:e,code:t,message:i,row:n})}this.parse=function(e,t,i){if(_.newline||(_.newline=function(e){var t=(e=e.substr(0,1048576)).split("\r"),i=e.split("\n"),n=1<i.length&&i[0].length<t[0].length;if(1===t.length||n)return"\n";for(var r=0,s=0;s<t.length;s++)"\n"===t[s][0]&&r++;return r>=t.length/2?"\r\n":"\r"}(e)),o=!1,_.delimiter)z(_.delimiter)&&(_.delimiter=_.delimiter(e),f.meta.delimiter=_.delimiter);else{var n=function(e,t,i){for(var n,r,s,a=[",","\t","|",";",g.RECORD_SEP,g.UNIT_SEP],o=0;o<a.length;o++){var h=a[o],u=0,f=0,d=0;s=void 0;for(var c=new v({delimiter:h,newline:t,preview:10}).parse(e),l=0;l<c.data.length;l++)if(i&&1===c.data[l].length&&0===c.data[l][0].length)d++;else{var p=c.data[l].length;f+=p,void 0!==s?1<p&&(u+=Math.abs(p-s),s=p):s=p}0<c.data.length&&(f/=c.data.length-d),(void 0===r||u<r)&&1.99<f&&(r=u,n=h)}return{successful:!!(_.delimiter=n),bestDelimiter:n}}(e,_.newline,_.skipEmptyLines);n.successful?_.delimiter=n.bestDelimiter:(o=!0,_.delimiter=g.DefaultDelimiter),f.meta.delimiter=_.delimiter}var r=C(_);return _.preview&&_.header&&r.preview++,s=e,a=new v(r),f=a.parse(s,t,i),d(),h?{meta:{paused:!0}}:f||{meta:{paused:!1}}},this.paused=function(){return h},this.pause=function(){h=!0,a.abort(),s=s.substr(a.getCharIndex())},this.resume=function(){h=!1,t.streamer.parseChunk(s,!0)},this.aborted=function(){return e},this.abort=function(){e=!0,a.abort(),f.meta.aborted=!0,z(_.complete)&&_.complete(f),s=""}}function v(e){var S,x=(e=e||{}).delimiter,I=e.newline,O=e.comments,T=e.step,D=e.preview,L=e.fastMode,F=S=void 0===e.quoteChar?'"':e.quoteChar;if(void 0!==e.escapeChar&&(F=e.escapeChar),("string"!=typeof x||-1<g.BAD_DELIMITERS.indexOf(x))&&(x=","),O===x)throw"Comment character same as delimiter";!0===O?O="#":("string"!=typeof O||-1<g.BAD_DELIMITERS.indexOf(O))&&(O=!1),"\n"!==I&&"\r"!==I&&"\r\n"!==I&&(I="\n");var P=0,A=!1;this.parse=function(n,t,i){if("string"!=typeof n)throw"Input must be a string";var r=n.length,e=x.length,s=I.length,a=O.length,o=z(T),h=[],u=[],f=[],d=P=0;if(!n)return E();if(L||!1!==L&&-1===n.indexOf(S)){for(var c=n.split(I),l=0;l<c.length;l++){if(f=c[l],P+=f.length,l!==c.length-1)P+=I.length;else if(i)return E();if(!O||f.substr(0,a)!==O){if(o){if(h=[],y(f.split(x)),R(),A)return E()}else y(f.split(x));if(D&&D<=l)return h=h.slice(0,D),E(!0)}}return E()}for(var p,_=n.indexOf(x,P),g=n.indexOf(I,P),m=new RegExp(F.replace(/[-[\]/{}()*+?.\\^$|]/g,"\\$&")+S,"g");;)if(n[P]!==S)if(O&&0===f.length&&n.substr(P,a)===O){if(-1===g)return E();P=g+s,g=n.indexOf(I,P),_=n.indexOf(x,P)}else if(-1!==_&&(_<g||-1===g))f.push(n.substring(P,_)),P=_+e,_=n.indexOf(x,P);else{if(-1===g)break;if(f.push(n.substring(P,g)),w(g+s),o&&(R(),A))return E();if(D&&h.length>=D)return E(!0)}else for(p=P,P++;;){if(-1===(p=n.indexOf(S,p+1)))return i||u.push({type:"Quotes",code:"MissingQuotes",message:"Quoted field unterminated",row:h.length,index:P}),C();if(p===r-1)return C(n.substring(P,p).replace(m,S));if(S!==F||n[p+1]!==F){if(S===F||0===p||n[p-1]!==F){var v=b(_);if(n[p+1+v]===x){f.push(n.substring(P,p).replace(m,S)),P=p+1+v+e,_=n.indexOf(x,P),g=n.indexOf(I,P);break}var k=b(g);if(n.substr(p+1+k,s)===I){if(f.push(n.substring(P,p).replace(m,S)),w(p+1+k+s),_=n.indexOf(x,P),o&&(R(),A))return E();if(D&&h.length>=D)return E(!0);break}u.push({type:"Quotes",code:"InvalidQuotes",message:"Trailing quote on quoted field is malformed",row:h.length,index:P}),p++}}else p++}return C();function y(e){h.push(e),d=P}function b(e){var t=0;if(-1!==e){var i=n.substring(p+1,e);i&&""===i.trim()&&(t=i.length)}return t}function C(e){return i||(void 0===e&&(e=n.substr(P)),f.push(e),P=r,y(f),o&&R()),E()}function w(e){P=e,y(f),f=[],g=n.indexOf(I,P)}function E(e){return{data:h,errors:u,meta:{delimiter:x,linebreak:I,aborted:A,truncated:!!e,cursor:d+(t||0)}}}function R(){T(E()),h=[],u=[]}},this.abort=function(){A=!0},this.getCharIndex=function(){return P}}function k(e){var t=e.data,i=h[t.workerId],n=!1;if(t.error)i.userError(t.error,t.file);else if(t.results&&t.results.data){var r={abort:function(){n=!0,y(t.workerId,{data:[],errors:[],meta:{aborted:!0}})},pause:b,resume:b};if(z(i.userStep)){for(var s=0;s<t.results.data.length&&(i.userStep({data:[t.results.data[s]],errors:t.results.errors,meta:t.results.meta},r),!n);s++);delete t.results}else z(i.userChunk)&&(i.userChunk(t.results,r,t.file),delete t.results)}t.finished&&!n&&y(t.workerId,t.results)}function y(e,t){var i=h[e];z(i.userComplete)&&i.userComplete(t),i.terminate(),delete h[e]}function b(){throw"Not implemented."}function C(e){if("object"!=typeof e)return e;var t=e instanceof Array?[]:{};for(var i in e)t[i]=C(e[i]);return t}function w(e,t){return function(){e.apply(t,arguments)}}function z(e){return"function"==typeof e}return o?f.onmessage=function(e){var t=e.data;void 0===g.WORKER_ID&&t&&(g.WORKER_ID=t.workerId);if("string"==typeof t.input)f.postMessage({workerId:g.WORKER_ID,results:g.parse(t.input,t.config),finished:!0});else if(f.File&&t.input instanceof File||t.input instanceof Object){var i=g.parse(t.input,t.config);i&&f.postMessage({workerId:g.WORKER_ID,results:i,finished:!0})}}:g.WORKERS_SUPPORTED&&(e=document.getElementsByTagName("script"),s=e.length?e[e.length-1].src:"",document.body?document.addEventListener("DOMContentLoaded",function(){a=!0},!0):a=!0),(l.prototype=Object.create(c.prototype)).constructor=l,(p.prototype=Object.create(c.prototype)).constructor=p,(_.prototype=Object.create(_.prototype)).constructor=_,(m.prototype=Object.create(c.prototype)).constructor=m,g});