Browse Source

Errors now keyed by row; closes #9.

pull/17/head 1.0.0
Matthew Holt 11 years ago
parent
commit
5635cc83fe
  1. 43
      README.md
  2. 23
      jquery.parse.js
  3. 4
      jquery.parse.min.js
  4. 2
      parse.jquery.json
  5. 61
      tests.js

43
README.md

@ -150,8 +150,8 @@ The results will always have this basic structure: @@ -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 @@ -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). @@ -225,7 +227,9 @@ Without a header row, the result is an array (list) of arrays (rows).
"3"
]
],
"errors": []
"errors": {
"length": 0
}
}
```
@ -274,11 +278,12 @@ Notice the stray quotes on the second line. This is the output: @@ -274,11 +278,12 @@ Notice the stray quotes on the second line. This is the output:
}
]
},
"errors": [
"errors": {
"0": [
{
"type": "FieldMismatch",
"code": "TooFewFields",
"message": "Too few fields; expected 4 fields, parsed 2",
"message": "Too few fields: expected 4 fields but parsed 2",
"line": 2,
"row": 0,
"index": 66
@ -291,7 +296,9 @@ Notice the stray quotes on the second line. This is the output: @@ -291,7 +296,9 @@ Notice the stray quotes on the second line. This is the output:
"row": 0,
"index": 66
}
]
],
"length": 2
}
}
```
@ -311,7 +318,8 @@ If the header row is disabled, field counting does not occur because there is no @@ -311,7 +318,8 @@ 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": [
"errors": {
"1": [
{
"type": "Quotes",
"code": "MissingQuotes",
@ -320,7 +328,9 @@ If the header row is disabled, field counting does not occur because there is no @@ -320,7 +328,9 @@ If the header row is disabled, field counting does not occur because there is no
"row": 1,
"index": 66
}
]
],
"length": 1
}
}
```
@ -359,7 +369,8 @@ Again, notice the second line, "10,95" instead of "10.95". This field *should* b @@ -359,7 +369,8 @@ Again, notice the second line, "10,95" instead of "10.95". This field *should* b
}
]
},
"errors": [
"errors": {
"0": [
{
"type": "FieldMismatch",
"code": "TooManyFields",
@ -367,16 +378,10 @@ Again, notice the second line, "10,95" instead of "10.95". This field *should* b @@ -367,16 +378,10 @@ Again, notice the second line, "10,95" instead of "10.95". This field *should* b
"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
}
]
],
"length": 1
}
}
```

23
jquery.parse.js

@ -195,11 +195,6 @@ @@ -195,11 +195,6 @@
return config;
}
function emptyParsed(header)
{
return header ? { fields: [], rows: [] } : [ [] ];
}
function emptyState()
{
return {
@ -210,8 +205,8 @@ @@ -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 @@ @@ -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;
}

4
jquery.parse.min.js vendored

@ -1,6 +1,6 @@ @@ -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-1&&d(u.i+1)||u.i==r.length-1;var n=u.i<r.length-1&&r[u.i+1]=='"';if(u.inQuotes&&n){u.fieldVal+='"';u.i++}else if(e||t){u.inQuotes=!u.inQuotes}else{S("Quotes","UnexpectedQuotes","Unexpected quotes")}}function c(){h()}function h(){u.fieldVal+=u.ch}function p(){if(u.ch==i.delimiter)m();else if(u.ch=="\r"&&u.i<r.length-1&&r[u.i+1]=="\n"){y();u.i++}else if(u.ch=="\n")y();else h()}function d(e){if(e>=r.length)return false;var t=r[e];if(t==i.delimiter||t=="\n"||t=="\r"&&e<r.length-1&&r[e+1]=="\n")return true;else return false}function v(e){if(e>=r.length)return false;if(e<r.length-1)return r[e]=="\n"||r[e]=="\r"&&r[e+1]=="\n";else return r[e]=="\n"}function m(){if(i.header){if(u.lineNum==1)u.parsed.fields.push(u.fieldVal);else{var e=u.parsed.rows[u.parsed.rows.length-1];var t=u.parsed.fields[u.field];if(t){if(i.dynamicTyping)u.fieldVal=b(u.fieldVal);e[t]=u.fieldVal}else{if(typeof e.__parsed_extra==="undefined")e.__parsed_extra=[];e.__parsed_extra.push(u.fieldVal)}}}else{if(i.dynamicTyping)u.fieldVal=b(u.fieldVal);u.parsed[u.parsed.length-1].push(u.fieldVal)}u.fieldVal="";u.field++}function g(){m();var e=w();if(!e&&i.header)E()}function y(){g();if(i.header&&u.lineNum>0)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(t<e)return S("FieldMismatch","TooFewFields","Too few fields: expected "+e+" fields but parsed "+t);else if(t>e)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.i<r.length;u.i++){u.ch=r[u.i];u.line+=u.ch;if(u.ch=='"')l();else if(u.inQuotes)c();else p()}g();if(u.inQuotes)S("Quotes","MissingQuotes","Unescaped or mismatched quotes");return n.getParsed()};this.getDelimiter=function(){return t.delimiter};this.setDelimiter=function(e){var t=",";e=e?e=='"'||e=="\n"?t:e:t;i.delimiter=e[0]};this.setConfig=function(e){if(typeof e.header!=="undefined"&&e.header!=t.header||typeof e.delimiter!=="undefined"&&e.delimiter!=t.delimiter){u.parsed=a(e.header)}i=e};this.getInput=function(){return r};this.setInput=function(e){r=e};this.getParsed=function(){return u.parsed};this.getErrors=function(){return s}}var t={delimiter:",",header:true,dynamicTyping:false};e.parse=function(e,t){t=n(t);var i=new r(e,t);return{results:i.parse(),errors:i.getErrors()}}})(jQuery);
;(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);

2
parse.jquery.json

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
"name": "parse",
"version": "1.0.0",
"title": "jQuery Parse",
"description": "Efficiently parses CSV (character-separated, or delimited text) files or strings into arrays and objects. Gracefully handles errors. Supports multiple file inputs and multiple files per input element.",
"description": "Parses CSV (character-separated, or delimited text) files or strings into arrays and objects efficiently. Gracefully handles errors. Supports multiple file inputs and multiple files per input element.",
"keywords": [
"csv",
"parse",

61
tests.js

@ -20,7 +20,9 @@ var resultSet1 = [ @@ -20,7 +20,9 @@ var resultSet1 = [
}
]
},
"errors": []
"errors": {
"length": 0
}
}
},
{
@ -38,7 +40,9 @@ var resultSet1 = [ @@ -38,7 +40,9 @@ var resultSet1 = [
"V3"
]
],
"errors": []
"errors": {
"length": 0
}
}
},
{
@ -56,7 +60,9 @@ var resultSet1 = [ @@ -56,7 +60,9 @@ var resultSet1 = [
"V3"
]
],
"errors": []
"errors": {
"length": 0
}
}
},
{
@ -76,7 +82,9 @@ var resultSet1 = [ @@ -76,7 +82,9 @@ var resultSet1 = [
}
]
},
"errors": []
"errors": {
"length": 0
}
}
}
];
@ -131,7 +139,9 @@ var tests = [ @@ -131,7 +139,9 @@ var tests = [
}
]
},
"errors": []
"errors": {
"length": 0
}
}
},
{
@ -154,7 +164,9 @@ var tests = [ @@ -154,7 +164,9 @@ var tests = [
"V6"
]
],
"errors": []
"errors": {
"length": 0
}
}
},
{
@ -177,7 +189,9 @@ var tests = [ @@ -177,7 +189,9 @@ var tests = [
"V6"
]
],
"errors": []
"errors": {
"length": 0
}
}
},
{
@ -202,7 +216,9 @@ var tests = [ @@ -202,7 +216,9 @@ var tests = [
}
]
},
"errors": []
"errors": {
"length": 0
}
}
}
]
@ -232,7 +248,9 @@ var tests = [ @@ -232,7 +248,9 @@ var tests = [
}
]
},
"errors": []
"errors": {
"length": 0
}
}
}
]
@ -265,7 +283,8 @@ var tests = [ @@ -265,7 +283,8 @@ var tests = [
}
]
},
"errors": [
"errors": {
"0": [
{
"type": "FieldMismatch",
"code": "TooManyFields",
@ -274,7 +293,9 @@ var tests = [ @@ -274,7 +293,9 @@ var tests = [
"row": 0,
"index": 17
}
]
],
"length": 1
}
}
}
]
@ -307,7 +328,8 @@ var tests = [ @@ -307,7 +328,8 @@ var tests = [
}
]
},
"errors": [
"errors": {
"0": [
{
"type": "FieldMismatch",
"code": "TooManyFields",
@ -316,7 +338,9 @@ var tests = [ @@ -316,7 +338,9 @@ var tests = [
"row": 0,
"index": 25
}
]
],
"length": 1
}
}
}
]
@ -345,7 +369,8 @@ var tests = [ @@ -345,7 +369,8 @@ var tests = [
}
]
},
"errors": [
"errors": {
"1": [
{
"type": "FieldMismatch",
"code": "TooFewFields",
@ -362,7 +387,9 @@ var tests = [ @@ -362,7 +387,9 @@ var tests = [
"row": 1,
"index": 27
}
]
],
"length": 2
}
}
}
]
@ -397,7 +424,9 @@ var tests = [ @@ -397,7 +424,9 @@ var tests = [
}
]
},
"errors": []
"errors": {
"length": 0
}
}
}
]

Loading…
Cancel
Save