Permalink
Browse files

improve re use (/g instead of loop)

  • Loading branch information...
1 parent 2d915cb commit a3cb0957b50c61385551d729e04deed2e420cc21 @stuarthalloway committed May 22, 2009
Showing with 16 additions and 3 deletions.
  1. +4 −3 public/javascripts/jquery.numberformatter-1.1.2.js
  2. +12 −0 test/javascript/numberformatter_spec.js
@@ -226,9 +226,10 @@
var text = new String(jQuery(this).valOrText());
// now we need to convert it into a number
- while (text.indexOf(group)>-1)
- text = text.replace(group,'');
- var number = new Number(text.replace(dec,".").replace(neg,"-"));
+ // technical debt: what happens to numbers with more than one decimal or negative sign?
+ var number = new Number(text.replace(new RegExp(group, "g"), "")
+ .replace(dec,".")
+ .replace(neg,"-"));
// special case for percentages
if (options.suffix == "%")
@@ -69,5 +69,17 @@ Screw.Unit(function(){
expect($("#value").text()).to(equal, "-11.13");
});
+ it("handles numbers that already contain formatting", function(){
+ $("#value").text("123,456,789");
+ $("#value").format({format: "#"});
+ expect($("#value").text()).to(equal, "123456789");
+ });
+
+ it("handles numbers that already contain *bad* formatting", function(){
+ $("#value").text("1,2,3,4,5");
+ $("#value").format({format: "#"});
+ expect($("#value").text()).to(equal, "12345");
+ });
+
});
});

0 comments on commit a3cb095

Please sign in to comment.