Permalink
Browse files

breaking change

  
 - treat "12abc9" as 12, not 129

refactoring

 - call previous refactoring (x2)
 - assign variable inline
 - use regular expressions to normalize data
  • Loading branch information...
1 parent 4d43308 commit cdd70ac42a80b270c0155a600c3138c0b6eada59 @stuarthalloway committed May 22, 2009
Showing with 38 additions and 29 deletions.
  1. +15 −26 public/javascripts/jquery.numberformatter-1.1.2.js
  2. +23 −3 test/javascript/numberformatter_spec.js
@@ -164,30 +164,19 @@
var array = [];
this.each(function(){
- var text = new String(jQuery(this).text());
- if (jQuery(this).is(":input"))
- text = new String(jQuery(this).val());
-
- // now we need to convert it into a number
- while (text.indexOf(group)>-1)
- text = text.replace(group,'');
- text = text.replace(dec,".").replace(neg,"-");
- var validText = "";
- var hasPercent = false;
- if (text.charAt(text.length-1)=="%")
- hasPercent = true;
- for (var i=0; i<text.length; i++)
- {
- if (valid.indexOf(text.charAt(i))>-1)
- validText = validText + text.charAt(i);
- }
- var number = new Number(validText);
- if (hasPercent)
- {
- number = number / 100;
- number = number.toFixed(validText.length-1);
- }
- array.push(number);
+ var text = new String(jQuery(this).valOrText());
+ text = text.replace(new RegExp('[' + group + ']', "g"), "")
+ .replace(dec, ".")
+ .replace(neg, "-");
+ var hasPercent = (text.charAt(text.length-1)=="%");
+ var number = parseFloat(text, 10);
+ if (hasPercent)
+ {
+ number = number / 100;
+ number = number.toFixed(text.length-1);
+ }
+ array.push(number);
+
});
return array;
@@ -214,9 +203,9 @@
// now we need to convert it into a number
// technical debt: what happens to numbers with more than one decimal or negative sign?
- var number = new Number(text.replace(new RegExp(group, "g"), "")
+ var number = parseFloat(text.replace(new RegExp('[' + group + ']', "g"), "")
.replace(dec,".")
- .replace(neg,"-"));
+ .replace(neg,"-"), 10);
// special case for percentages
if (options.suffix == "%")
@@ -1,5 +1,4 @@
require("spec_helper.js");
-require("../../public/javascripts/jquery-1.3.2.js");
require("../../public/javascripts/jquery.numberformatter-1.1.2.js");
Screw.Unit(function(){
@@ -20,8 +19,23 @@ Screw.Unit(function(){
});
it("extracts a number with a specified group delimiter", function(){
- $("#value").text("111.222.333");
- expect($("#value").parse({locale: "de"})).to(equal, [111222333]);
+ $("#value").text("444.555.666");
+ expect($("#value").parse({locale: "de"})).to(equal, [444555666]);
+ });
+
+ it("knows all the valid number characters", function(){
+ $("#value").text("-123,456.789");
+ expect($("#value").parse()).to(equal, [-123456.789]);
+ });
+
+ it("ignores junk at the end", function(){
+ $("#value").text("36XL");
+ expect($("#value").parse()[0]).to(equal, 36);
+ });
+
+ it("ignores everything after the first non-number character", function(){
+ $("#value").text("14 to 16");
+ expect($("#value").parse()[0]).to(equal, 14);
});
});
@@ -97,6 +111,12 @@ Screw.Unit(function(){
expect($("#value").text()).to(equal, "123456789");
});
+ it("handles numbers that already contain non-us formatting", function(){
+ $("#value").text("987.654.321");
+ $("#value").format({locale: "de"});
+ expect($("#value").text()).to(equal, "987.654.321,00");
+ });
+
it("handles numbers that already contain *bad* formatting", function(){
$("#value").text("1,2,3,4,5");
$("#value").format({format: "#"});

0 comments on commit cdd70ac

Please sign in to comment.