Browse Source

Fix for floats regex and more thorough test for it

pull/17/head 0.6.1
Matthew Holt 11 years ago
parent
commit
89b570ba46
  1. 5
      jquery.parse.js
  2. 4
      jquery.parse.min.js
  3. 4
      parse.jquery.json
  4. 14
      tests.js

5
jquery.parse.js

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/*
jQuery Parse Plugin
v0.6.0
v0.6.1
https://github.com/mholt/jquery.parse
*/
@ -50,8 +50,7 @@ @@ -50,8 +50,7 @@
var _config = config;
var _errors = [];
var _regex = {
// floats: /^-?\d+(\.\d+)?$/,
floats : /^(\s*)(-?)[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/,
floats: /^\s*-?(\d*\.?\d+|\d+\.?\d*)(e[-+]?\d+)?\s*$/i,
empty: /^\s*$/
}
var _state = emptyState();

4
jquery.parse.min.js vendored

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/*
jQuery Parse Plugin
v0.6.0
v0.6.1
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:/^-?\d+(\.\d+)?$/,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 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);

4
parse.jquery.json

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
{
"name": "parse",
"version": "0.6.0",
"version": "0.6.1",
"title": "jQuery Parse Plugin",
"description": "Parses CSV (or any delimited text) into a usable data structure. This efficient and robust parser gracefully handles malformed input and it can work without jQuery.",
"description": "Parses CSV (or any delimited text) into a usable data structure. This efficient and robust parser gracefully handles malformed input. The parsing function can work without jQuery.",
"keywords": [
"csv",
"parse",

14
tests.js

@ -368,7 +368,7 @@ var tests = [ @@ -368,7 +368,7 @@ var tests = [
]
},
{
input: "F1,F2,F3\n2,-2, 2\n 2. ,.2, .2 \n-2., -2.0, -.4 ",
input: "F1,F2,F3\n2,-2, 2\n 2. ,.2, .2 \n-2., -2.0e-5, -.4 ",
cases: [
{
config: { delimiter: ",", header: true, dynamicTyping: true },
@ -387,12 +387,12 @@ var tests = [ @@ -387,12 +387,12 @@ var tests = [
},
{
"F1": 2,
"F2": .2,
"F3": .2
"F2": 0.2,
"F3": 0.2
},
{
"F1": -2,
"F2": -2,
"F2": -0.00002,
"F3": -0.4
}
]
@ -441,7 +441,13 @@ function render(input, expected, actual, config, count, status) @@ -441,7 +441,13 @@ function render(input, expected, actual, config, count, status)
if (status == passing)
passCount++;
else
{
console.log("TEST " + count +" FAILED.");
console.log(" Expected:", expected);
console.log(" Actual:", actual);
console.log(" Config:", config);
failCount++;
}
var html = '<tr>' +
'<td class="count">'+count+'</td>' +

Loading…
Cancel
Save