From f5eab684ec4799468a1e64ed1fa66fe7353f9857 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 15 Jul 2014 16:06:16 -0600 Subject: [PATCH] Let's stamp version 3.0 on it and see what happens --- README.md | 4 ++-- bower.json | 6 +++++- papaparse.js | 2 +- papaparse.min.js | 6 ++++++ parse.jquery.json | 45 +++++++++++++++++++++++++++++++++++++++++++++ tests/test-cases.js | 22 +++++++++++++++++----- 6 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 papaparse.min.js create mode 100644 parse.jquery.json diff --git a/README.md b/README.md index 070cd15..1eaa710 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Visit **[PapaParse.com/demo.html](http://papaparse.com/demo.html)** to try Papa! Get Started ----------- -Use [papaparse.min.js](https://github.com/mholt/jquery.parse/blob/master/papaparse.min.js) for production. +Use [papaparse.min.js](https://github.com/mholt/PapaParse/blob/master/papaparse.min.js) for production. For usage instructions, see the [homepage](http://papaparse.com) and, for more detail, the [documentation](http://papaparse.com/docs.html). @@ -46,7 +46,7 @@ Papa Parse is under test (especially its core Parser). Download this repository Contributing ------------ -To discuss a new feature or ask a question, open an issue. To fix a bug, submit a pull request to be credited with the [contributors](https://github.com/mholt/jquery.parse/graphs/contributors)! Remember, a pull request, *with test*, is best. (Especially all changes to the Parser component should be validated with tests.) You may also discuss on Twitter with [#PapaParse](https://twitter.com/search?q=%23PapaParse&src=typd&f=realtime) or directly to me, [@mholt6](https://twitter.com/mholt6). +To discuss a new feature or ask a question, open an issue. To fix a bug, submit a pull request to be credited with the [contributors](https://github.com/mholt/PapaParse/graphs/contributors)! Remember, a pull request, *with test*, is best. (Especially all changes to the Parser component should be validated with tests.) You may also discuss on Twitter with [#PapaParse](https://twitter.com/search?q=%23PapaParse&src=typd&f=realtime) or directly to me, [@mholt6](https://twitter.com/mholt6). diff --git a/bower.json b/bower.json index a9bfa02..7207138 100644 --- a/bower.json +++ b/bower.json @@ -20,7 +20,11 @@ "pipe", "file", "filereader", - "stream" + "stream", + "workers", + "ajax", + "threading", + "multi-threaded" ], "license": "MIT", "ignore": [ diff --git a/papaparse.js b/papaparse.js index 2252a35..1ee7b28 100644 --- a/papaparse.js +++ b/papaparse.js @@ -1,6 +1,6 @@ /* Papa Parse - v3.0.0 alpha - MAY BE UNSTABLE - STILL IN DEVELOPMENT + v3.0.0 https://github.com/mholt/PapaParse */ (function(global) diff --git a/papaparse.min.js b/papaparse.min.js new file mode 100644 index 0000000..332d8f5 --- /dev/null +++ b/papaparse.min.js @@ -0,0 +1,6 @@ +/* + Papa Parse + v3.0.0 + https://github.com/mholt/PapaParse +*/ +;(function(e){"use strict";function u(e,n){var r=t?n:g(n);var i=r.worker&&Papa.WORKERS_SUPPORTED;if(i){var s=d();s.userStep=r.step;s.userComplete=r.complete;s.userError=r.error;r.step=b(r.step);r.complete=b(r.complete);r.error=b(r.error);delete r.worker;s.postMessage({input:e,config:r,workerId:s.id})}else{if(typeof e==="string"){if(r.download){var o=new f(r);o.stream(e)}else{var u=new c(r);var a=u.parse(e);if(b(r.complete))r.complete(a);return a}}else if(e instanceof File){if(r.step){var o=new l(r);o.stream(e)}else{var u=new c(r);if(t){var h=new FileReaderSync;var p=h.readAsText(e,r.encoding);return u.parse(p)}else{h=new FileReader;h.onload=function(e){var t=new c(r);var n=t.parse(e.target.result);if(b(r.complete))r.complete(n)};h.readAsText(e,r.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")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;s0)n+=o;n+=c(e[s])}if(t.length>0)n+=u}for(var a=0;a0)n+=o;var h=r&&i?e[l]:l;n+=c(t[a][h])}if(a-1||t.charAt(0)==" "||t.charAt(t.length-1)==" ";return n?'"'+t+'"':t}function h(e,t){for(var n=0;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){n=n||{};if(!n.chunkSize)n.chunkSize=1024*1024*5;var r=0,i=0;var s="";var o="";var u,a;var f=new c(y(n));this.stream=function(l){function c(){u=new XMLHttpRequest;if(!t){u.onload=h;u.onerror=p}u.open("GET",l,!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)p();else r+=n.chunkSize}function h(){if(u.readyState!=4)return;if(u.status<200||u.status>=400){p();return}s+=o+u.responseText;o="";var i=!n.step||r>d(u);if(!i){var l=s.lastIndexOf("\n");if(l<0)l=s.lastIndexOf("\r");if(l>-1){o=s.substring(l+1);s=s.substring(0,l)}else{a();return}}var c=f.parse(s);s="";if(t){e.postMessage({results:c,workerId:Papa.WORKER_ID,finished:i})}if(i&&b(n.complete))n.complete(c);else if(c&&c.meta.aborted&&b(n.complete))n.complete(c);else if(!i)a()}function p(){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 d(e){var t=e.getResponseHeader("Content-Range");return parseInt(t.substr(t.lastIndexOf("/")+1))}if(t){a=function(){c();h()}}else{a=function(){c()}}a()}}function l(n){n=n||{};if(!n.chunkSize)n.chunkSize=1024*1024*10;var r=0;var i="";var s="";var o,u,a;var f=new c(y(n));this.stream=function(u){function l(){if(r=u.size;if(!a){var c=i.lastIndexOf("\n");if(c<0)c=i.lastIndexOf("\r");if(c>-1){s=i.substring(c+1);i=i.substring(0,c)}else{l();return}}var h=f.parse(i);i="";if(t){e.postMessage({results:h,workerId:Papa.WORKER_ID,finished:a})}if(a&&b(n.complete))n.complete(undefined,u);else if(h.meta.aborted&&b(n.complete))n.complete(h,u);else if(!h.meta.paused)l()}function p(){if(b(n.error))n.error(o.error,u);else if(t&&n.error){e.postMessage({workerId:Papa.WORKER_ID,error:o.error,file:u,finished:false})}}var a=u.slice||u.webkitSlice||u.mozSlice;o=new FileReader;o.onload=h;o.onerror=p;l()}}function c(e){function s(){if(i&&n){c("Delimiter","UndetectableDelimiter","Unable to auto-detect delimiting character; defaulted to comma");n=false}if(o())u();return a()}function o(){return e.header&&r.length==0}function u(){if(!i)return;for(var e=0;o()&&e=r.length){if(!n["__parsed_extra"])n["__parsed_extra"]=[];n["__parsed_extra"].push(i.data[t][s])}n[r[s]]=i.data[t][s]}}if(e.header){i.data[t]=n;if(s>r.length)c("FieldMismatch","TooManyFields","Too many fields: expected "+r.length+" fields but parsed "+s,t);else if(s1){a+=Math.abs(p-s);s=p}}f/=l.data.length;if((typeof i==="undefined"||a1.99){i=a;r=u}}e.delimiter=r;return{successful:!!r,bestDelimiter:r}}function l(e){var n=t.test(e);return n?parseFloat(e):e}function c(e,t,n,r){i.errors.push({type:e,code:t,message:n,row:r})}var t=/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i;var n;var r=[];var i={data:[],errors:[],meta:{}};e=y(e);this.parse=function(t){n=false;if(!e.delimiter){var r=f(t);if(r.successful)e.delimiter=r.bestDelimiter;else{n=true;e.delimiter=","}i.meta.delimiter=e.delimiter}if(b(e.step)){var u=e.step;e.step=function(e,t){i=e;if(o())s();else u(s(),t)}}i=(new h(e)).parse(t);return s()}}function h(e){function w(){while(l0&&v>=a)break;if(y)return S();if(f=='"')x();else if(c)T();else N();E()}return S()}function E(){l++;f=r[l]}function S(){if(g)F("Abort","ParseAbort","Parsing was aborted by the user's step function");if(c)F("Quotes","MissingQuotes","Unescaped or mismatched quotes");M();if(!b(o))return R()}function x(){if(B()&&!H())c=!c;else{L();if(c&&H())l++;else F("Quotes","UnexpectedQuotes","Unexpected quotes")}}function T(){L()}function N(){if(f==i)A();else if(D()){O();E()}else if(P())O();else if(C())k();else L()}function C(){if(!s)return false;var e=l==0||P(l-1)||D(l-2);return e&&r[l]===s}function k(){while(!D()&&!P()&&l-1&&(t==i||t=="\r"||t=="\n")}function F(e,t,n){d.push({type:e,code:t,message:n,line:h,row:v,index:l})}function I(e){r=e;c=false;h=1;l=0;q();p=[[""]];f=r[l]}function q(){p=[];d=[];v=0;m=0}function R(){return{data:p,errors:d,meta:{lines:h,delimiter:i,aborted:g}}}var t=this;var n=/^\s*$/;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;var y=false;e=e||{};i=e.delimiter;s=e.comments;o=e.step;a=e.preview;if(typeof i!=="string"||i.length!=1||Papa.BAD_DELIMITERS.indexOf(i)>-1)i=",";if(s===true)s="#";else if(typeof s!=="string"||s.length!=1||Papa.BAD_DELIMITERS.indexOf(s)>-1||s==i)s=false;this.parse=function(e){if(typeof e!=="string")throw"Input must be a string";I(e);return w()};this.abort=function(){g=true}}function p(){var e="worker"+String(Math.random()).substr(2);document.write('');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.results&&t.results.data&&b(n.userStep)){for(var i=0;i-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.encoding!=="string")t.encoding=s.encoding;if(typeof t.worker!=="boolean")t.worker=s.worker;if(typeof t.download!=="boolean")t.download=s.download;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,download: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.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){if(b(t.complete))t.complete();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=1.6.0" + } +} \ No newline at end of file diff --git a/tests/test-cases.js b/tests/test-cases.js index 88871b8..4624ed4 100644 --- a/tests/test-cases.js +++ b/tests/test-cases.js @@ -161,12 +161,12 @@ var TESTS = [ } }, { - input: 'aDELIMbDELIMc', + input: 'a,b,c', config: { delimiter: "DELIM" }, description: "Bad delimiter", notes: "Should silently default to comma", expected: { - data: [['aDELIMbDELIMc']], + data: [['a', 'b', 'c']], errors: [] } }, @@ -218,7 +218,7 @@ var TESTS = [ }, { input: '#commented line\r\n', - config: { comments: true }, + config: { comments: true, delimiter: ',' }, description: "Input with only a commented line (comments: true)", expected: { data: [], @@ -227,6 +227,7 @@ var TESTS = [ }, { input: '#commented line', + config: { delimiter: ',' }, description: "Input with comment without comments enabled", expected: { data: [['#commented line']], @@ -235,6 +236,7 @@ var TESTS = [ }, { input: 'a\r\n b\r\nc', + config: { delimiter: ',' }, description: "Input without comments with line starting with whitespace", notes: "\" \" == false, but \" \" !== false, so === comparison is required", expected: { @@ -313,7 +315,11 @@ var TESTS = [ description: "Empty input string", expected: { data: [], - errors: [] + errors: [{ + "type": "Delimiter", + "code": "UndetectableDelimiter", + "message": "Unable to auto-detect delimiting character; defaulted to comma" + }] } }, { @@ -329,7 +335,13 @@ var TESTS = [ description: "Input is just a string (a single field)", expected: { data: [['Abc def']], - errors: [] + errors: [ + { + "type": "Delimiter", + "code": "UndetectableDelimiter", + "message": "Unable to auto-detect delimiting character; defaulted to comma" + } + ] } }, {