From 5635cc83feaa3f4b02117db47575ae64abad6b38 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 14 Dec 2013 11:56:48 -0700 Subject: [PATCH] Errors now keyed by row; closes #9. --- README.md | 105 +++++++++++++++++++----------------- jquery.parse.js | 23 ++++---- jquery.parse.min.js | 4 +- parse.jquery.json | 2 +- tests.js | 127 +++++++++++++++++++++++++++----------------- 5 files changed, 150 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index 28a5949..8fecb60 100644 --- a/README.md +++ b/README.md @@ -150,8 +150,8 @@ The results will always have this basic structure: ```javascript { - results: // either an object or array of parse results - errors: // an array of parse errors + results: // parse results + errors: // parse errors, keyed by row } ``` @@ -190,7 +190,9 @@ With a header row, each value is keyed to its field name, so the result is an ob } ] }, - "errors": [] + "errors": { + "length": 0 + } } ``` @@ -225,7 +227,9 @@ Without a header row, the result is an array (list) of arrays (rows). "3" ] ], - "errors": [] + "errors": { + "length": 0 + } } ``` @@ -274,24 +278,27 @@ Notice the stray quotes on the second line. This is the output: } ] }, - "errors": [ - { - "type": "FieldMismatch", - "code": "TooFewFields", - "message": "Too few fields; expected 4 fields, parsed 2", - "line": 2, - "row": 0, - "index": 66 - }, - { - "type": "Quotes", - "code": "MissingQuotes", - "message": "Unescaped or mismatched quotes", - "line": 2, - "row": 0, - "index": 66 - } - ] + "errors": { + "0": [ + { + "type": "FieldMismatch", + "code": "TooFewFields", + "message": "Too few fields: expected 4 fields but parsed 2", + "line": 2, + "row": 0, + "index": 66 + }, + { + "type": "Quotes", + "code": "MissingQuotes", + "message": "Unescaped or mismatched quotes", + "line": 2, + "row": 0, + "index": 66 + } + ], + "length": 2 + } } ``` @@ -311,16 +318,19 @@ If the header row is disabled, field counting does not occur because there is no "ABC1234,10.95,4\nMovie,DEF5678,29.99,3" ] ], - "errors": [ - { - "type": "Quotes", - "code": "MissingQuotes", - "message": "Unescaped or mismatched quotes", - "line": 2, - "row": 1, - "index": 66 - } - ] + "errors": { + "1": [ + { + "type": "Quotes", + "code": "MissingQuotes", + "message": "Unescaped or mismatched quotes", + "line": 2, + "row": 1, + "index": 66 + } + ], + "length": 1 + } } ``` @@ -359,24 +369,19 @@ Again, notice the second line, "10,95" instead of "10.95". This field *should* b } ] }, - "errors": [ - { - "type": "FieldMismatch", - "code": "TooManyFields", - "message": "Too many fields: expected 4 fields but parsed 5", - "line": 2, - "row": 0, - "index": 43 - }, - { - "type": "FieldMismatch", - "code": "TooFewFields", - "message": "Too few fields: expected 4 fields but parsed 5", - "line": 2, - "row": 0, - "index": 43 - } - ] + "errors": { + "0": [ + { + "type": "FieldMismatch", + "code": "TooManyFields", + "message": "Too many fields: expected 4 fields but parsed 5", + "line": 2, + "row": 0, + "index": 43 + } + ], + "length": 1 + } } ``` diff --git a/jquery.parse.js b/jquery.parse.js index cc41565..8654305 100644 --- a/jquery.parse.js +++ b/jquery.parse.js @@ -195,11 +195,6 @@ return config; } - function emptyParsed(header) - { - return header ? { fields: [], rows: [] } : [ [] ]; - } - function emptyState() { return { @@ -210,8 +205,8 @@ line: "", ch: "", inQuotes: false, - parsed: emptyParsed(_config.header), - errors: [] + parsed: _config.header ? { fields: [], rows: [] } : [ [] ], + errors: { length: 0 } }; } @@ -398,14 +393,24 @@ function addError(type, code, msg) { - _state.errors.push({ + var row = _config.header + ? _state.parsed.rows.length - 1 + : _state.parsed.length - 1; + + if (typeof _state.errors[row] === 'undefined') + _state.errors[row] = []; + + _state.errors[row].push({ type: type, code: code, message: msg, line: _state.lineNum, - row: _config.header ? _state.parsed.rows.length - 1 : _state.parsed.length - 1, + row: row, index: _state.i }); + + _state.errors.length ++; + return false; } diff --git a/jquery.parse.min.js b/jquery.parse.min.js index 6e56013..f3bd653 100644 --- a/jquery.parse.min.js +++ b/jquery.parse.min.js @@ -1,6 +1,6 @@ /* jQuery Parse Plugin - v0.6.1 + v1.0.0 https://github.com/mholt/jquery.parse */ -;(function(e){"use strict";function n(e){e.delimiter=e.delimiter||t.delimiter;e.header=typeof e.header==="undefined"?t.header:e.header;e.dynamicTyping=typeof e.dynamicTyping==="undefined"?t.dynamicTyping:e.dynamicTyping;if(e.delimiter=='"'||e.delimiter=="\n")e.delimiter=t.delimiter;if(e.delimiter.length>1)e.delimiter=e.delimiter[0];return e}function r(e,t){function a(e){return e?{fields:[],rows:[]}:[[]]}function f(){return{i:0,lineNum:1,field:0,fieldVal:"",line:"",ch:"",inQuotes:false,parsed:a(t.header)}}function l(){var e=u.i>0&&d(u.i-1)||u.i==0;var t=u.i=r.length)return false;var t=r[e];if(t==i.delimiter||t=="\n"||t=="\r"&&e=r.length)return false;if(e0)u.parsed.rows.push({});else u.parsed.push([]);u.lineNum++;u.line="";u.field=0}function b(e){var t=o.floats.test(e);return t?parseFloat(e):e}function w(){if(o.empty.test(u.line)){if(i.header){if(u.lineNum==1){u.parsed.fields=[];u.lineNum--}else u.parsed.rows.splice(u.parsed.rows.length-1,1)}else u.parsed.splice(u.parsed.length-1,1);return true}return false}function E(){if(!i.header)return true;if(u.parsed.rows.length==0)return true;var e=u.parsed.fields.length;var t=0;var n=u.parsed.rows[u.parsed.rows.length-1];for(var r in n)if(n.hasOwnProperty(r))t++;if(te)return S("FieldMismatch","TooManyFields","Too many fields: expected "+e+" fields but parsed "+t);return true}function S(e,t,n){s.push({type:e,code:t,message:n,line:u.lineNum,row:i.header?u.parsed.rows.length-1:u.parsed.length-1,index:u.i});return false}var n=this;var r=e;var i=t;var s=[];var o={floats:/^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,empty:/^\s*$/};var u=f();this.parse=function(e){if(typeof e==="object")n.setConfig(e);else if(typeof e==="string")n.setInput(e);s=[];u=f();for(u.i=0;u.i0&&p(i.i-1)||i.i==0;var t=i.i=n.length)return false;var t=n[e];if(t==r.delimiter||t=="\n"||t=="\r"&&e=n.length)return false;if(e0)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(te)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