Skip to content

Commit

Permalink
improve re use (/g instead of loop)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed May 22, 2009
1 parent 2d915cb commit a3cb095
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions public/javascripts/jquery.numberformatter-1.1.2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "%")
Expand Down
12 changes: 12 additions & 0 deletions test/javascript/numberformatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.