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

/*
jQuery Parse Plugin
v1.0.0
https://github.com/mholt/jquery.parse
*/
;(function(e){"use strict";function t(e){return typeof e==="function"}function n(e){return typeof e!=="undefined"}function r(e){function u(e){if(typeof e.delimiter!=="string"||e.delimiter.length!=1)e.delimiter=s.delimiter;if(e.delimiter=='"'||e.delimiter=="\n")e.delimiter=s.delimiter;if(typeof e.header!=="boolean")e.header=s.header;if(typeof e.dynamicTyping!=="boolean")e.dynamicTyping=s.dynamicTyping;return e}function a(){return{i:0,lineNum:1,field:0,fieldVal:"",line:"",ch:"",inQuotes:false,parsed:r.header?{fields:[],rows:[]}:[[]],errors:{length:0}}}function f(){var e=i.i>0&&p(i.i-1)||i.i==0;var t=i.i<n.length-1&&p(i.i+1)||i.i==n.length-1;var r=i.i<n.length-1&&n[i.i+1]=='"';if(i.inQuotes&&r){i.fieldVal+='"';i.i++}else if(e||t)i.inQuotes=!i.inQuotes;else E("Quotes","UnexpectedQuotes","Unexpected quotes")}function l(){c()}function c(){i.fieldVal+=i.ch}function h(){if(i.ch==r.delimiter)v();else if(i.ch=="\r"&&i.i<n.length-1&&n[i.i+1]=="\n"){m();i.i++}else if(i.ch=="\n")m();else c()}function p(e){if(e>=n.length)return false;var t=n[e];if(t==r.delimiter||t=="\n"||t=="\r"&&e<n.length-1&&n[e+1]=="\n")return true;else return false}function d(e){if(e>=n.length)return false;if(e<n.length-1)return n[e]=="\n"||n[e]=="\r"&&n[e+1]=="\n";else return n[e]=="\n"}function v(){if(r.header){if(i.lineNum==1)i.parsed.fields.push(i.fieldVal);else{var e=i.parsed.rows[i.parsed.rows.length-1];var t=i.parsed.fields[i.field];if(t){if(r.dynamicTyping)i.fieldVal=y(i.fieldVal);e[t]=i.fieldVal}else{if(typeof e.__parsed_extra==="undefined")e.__parsed_extra=[];e.__parsed_extra.push(i.fieldVal)}}}else{if(r.dynamicTyping)i.fieldVal=y(i.fieldVal);i.parsed[i.parsed.length-1].push(i.fieldVal)}i.fieldVal="";i.field++}function m(){g();if(r.header&&i.lineNum>0)i.parsed.rows.push({});else i.parsed.push([]);i.lineNum++;i.line="";i.field=0}function g(){v();var e=b();if(!e&&r.header)w()}function y(e){var t=o.floats.test(e);return t?parseFloat(e):e}function b(){if(o.empty.test(i.line)){if(r.header){if(i.lineNum==1){i.parsed.fields=[];i.lineNum--}else i.parsed.rows.splice(i.parsed.rows.length-1,1)}else i.parsed.splice(i.parsed.length-1,1);return true}return false}function w(){if(!r.header)return true;if(i.parsed.rows.length==0)return true;var e=i.parsed.fields.length;var t=0;var n=i.parsed.rows[i.parsed.rows.length-1];for(var s in n)if(n.hasOwnProperty(s))t++;if(t<e)return E("FieldMismatch","TooFewFields","Too few fields: expected "+e+" fields but parsed "+t);else if(t>e)return E("FieldMismatch","TooManyFields","Too many fields: expected "+e+" fields but parsed "+t);return true}function E(e,t,n){var s=r.header?i.parsed.rows.length-1:i.parsed.length-1;if(typeof i.errors[s]==="undefined")i.errors[s]=[];i.errors[s].push({type:e,code:t,message:n,line:i.lineNum,row:s,index:i.i});i.errors.length++;return false}function S(){return{results:i.parsed,errors:i.errors}}function x(e){i=a();n=e}var t=this;var n="";var r={};var i=a();var s={delimiter:",",header:true,dynamicTyping:true};var o={floats:/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,empty:/^\s*$/};this.setOptions=function(e){e=u(e);r={delimiter:e.delimiter,header:e.header,dynamicTyping:e.dynamicTyping}};this.getOptions=function(){return{delimiter:r.delimiter,header:r.header,dynamicTyping:r.dynamicTyping}};this.parse=function(e){if(typeof e!=="string")return S();x(e);for(i.i=0;i.i<n.length;i.i++){i.ch=n[i.i];i.line+=i.ch;if(i.ch=='"')f();else if(i.inQuotes)l();else h()}g();if(i.inQuotes)E("Quotes","MissingQuotes","Unescaped or mismatched quotes");return S()};this.setOptions(e)}e.fn.parse=function(r){function i(e,n,i){if(t(r.error))r.error({name:e},n,i)}var s=n(r.config)?r.config:{};this.each(function(o){var u=e(this).prop("tagName").toUpperCase()=="INPUT"&&e(this).attr("type")=="file"&&window.FileReader;if(!u)return true;var a={delimiter:s.delimiter,header:s.header,dynamicTyping:s.dynamicTyping};if(!this.files||this.files.length==0){i("NoFileError",undefined,this);return true}for(var f=0;f<this.files.length;f++){var l=this.files[f];if(l.type.indexOf("text")<0){i("TypeMismatchError",l,this);continue}if(t(r.before)){var c=r.before(l,this);if(typeof c==="object"){if(n(c.delimiter))a.delimiter=c.delimiter;if(n(c.header))a.header=c.header;if(n(c.dynamicTyping))a.dynamicTyping=c.dynamicTyping}else if(c==="skip")continue;else if(c===false){i("AbortError",l,this);return false}}var h=new FileReader;if(t(r.error))h.onerror=function(){r.error(h.error,l,this)};var p=this;h.onload=function(n){var i=n.target.result;var s=e.parse(i,a);if(t(r.complete))r.complete(s,l,p,n)};h.readAsText(l)}});return this};e.parse=function(e,t){var n=new r(t);return n.parse(e)}})(jQuery);