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

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