Skip to content
Browse files

normalizing options

  • Loading branch information...
1 parent 2961e9d commit 55cca697a448ee06c09127d822ee37ad79bdc706 @stuarthalloway committed May 22, 2009
Showing with 66 additions and 25 deletions.
  1. +38 −19 public/javascripts/jquery.numberformatter-1.1.2.js
  2. +28 −6 test/javascript/numberformatter_spec.js
View
57 public/javascripts/jquery.numberformatter-1.1.2.js
@@ -123,20 +123,51 @@
}
return str;
};
+
+ nf.countDecimalDigits = function(format) {
+ var match = format.match(/\.[#0]+/)
+ if (match) {
+ return match[0].length - 1;
+ }
+ return 0;
+ };
- jQuery.numberFormatter.formatNumber = function(str, options) {
+ nf.countDigitsPerGroup = function(format) {
+ var match = format.match(/,(.*?)(\.|$)/)
+ if (match) {
+ return match[1].length;
+ }
+ return null;
+ }
+
+ // strip all the invalid characters at the beginning and the end
+ // of the format, and we'll stick them back on at the end
+ // make a special case for the negative sign "-" though, so
+ // we can have formats like -$23.32
+ nf.normalizeOptions = function(options) {
+ var match = /^(-?)([^-0#,.]*)([-0#,.]*)([^-0#,.]*)$/.exec(options.format)
+ if (!match) throw "invalid number format " + options.format;
+ options.negativeInFront = (match[1] == "-");
+ options.prefix = match[2];
+ options.format = match[3];
+ options.suffix = match[4];
+ options.decimalDigits = nf.countDecimalDigits(options.format);
+ options.digitsPerGroup = nf.countDigitsPerGroup(options.format);
+ };
+
+ nf.formatNumber = function(str, options) {
var left_right = str.split(".");
var left = left_right[0];
var right = left_right[1];
- options.decimalsRightOfZero = options.decimalsRightOfZero || 0;
- if (options.grouping) {
- left = s.reverse(s.join(s.partition(s.reverse(left), options.grouping, options.grouping), options.group));
+ options.decimalDigits = options.decimalDigits || 0;
+ if (options.digitsPerGroup) {
+ left = s.reverse(s.join(s.partition(s.reverse(left), options.digitsPerGroup, options.digitsPerGroup), options.group));
}
var result = (left +
"." +
- nf.pad(right.substring(0, options.decimalsRightOfZero), options.decimalsRightOfZero, "0")
+ nf.pad(right.substring(0, options.decimalDigits), options.decimalDigits, "0")
).replace(/\.$/, "");
- if (right.charAt(options.decimalsRightOfZero).match(/[5-9]/)) {
+ if (right.charAt(options.decimalDigits).match(/[5-9]/)) {
//need to round up
result = jQuery.numberFormatter.bump(result);
}
@@ -209,18 +240,6 @@
};
- // strip all the invalid characters at the beginning and the end
- // of the format, and we'll stick them back on at the end
- // make a special case for the negative sign "-" though, so
- // we can have formats like -$23.32
- function parseOptionsFormat(options) {
- var match = /^(-?)([^-0#,.]*)([-0#,.]*)([^-0#,.]*)$/.exec(options.format)
- if (!match) throw "invalid number format " + options.format;
- options.negativeInFront = (match[1] == "-");
- options.prefix = match[2];
- options.format = match[3];
- options.suffix = match[4];
- };
jQuery.fn.parse = function(options) {
@@ -262,7 +281,7 @@
jQuery.fn.format = function(options) {
var options = jQuery.extend({},jQuery.fn.format.defaults, options);
- parseOptionsFormat(options);
+ nf.normalizeOptions(options);
var formatData = formatCodes(options.locale.toLowerCase());
View
34 test/javascript/numberformatter_spec.js
@@ -2,6 +2,28 @@ require("spec_helper.js");
require("../../public/javascripts/jquery.numberformatter-1.1.2.js");
Screw.Unit(function(){
+ describe("numberFormatter.normalizeOptions", function() {
+
+ it("detects required decimal zeros", function() {
+ var options = {format: "##.###0CRAP"}
+ $.numberFormatter.normalizeOptions(options);
+ expect(options.decimalDigits).to(equal, 4);
+ });
+
+ it("detects absence digit groups", function() {
+ var options = {format: "##"}
+ $.numberFormatter.normalizeOptions(options);
+ expect(options.digitsPerGroup).to(equal, null);
+ });
+
+ it("detects presence of digit groups", function() {
+ var options = {format: "#,#####"}
+ $.numberFormatter.normalizeOptions(options);
+ expect(options.digitsPerGroup).to(equal, 5);
+ });
+
+ });
+
describe("numberFormatter.bump", function() {
it("bumps", function() {
@@ -14,26 +36,26 @@ Screw.Unit(function(){
});
- describe("numberFormatter.formatRightOfDecimal", function() {
+ describe("numberFormatter.formatNumber", function() {
it("handles zero format digits", function() {
- expect($.numberFormatter.formatNumber("123.45", {decimalsRightOfZero: 0})).to(equal, "123");
+ expect($.numberFormatter.formatNumber("123.45", {decimalDigits: 0})).to(equal, "123");
});
it("handles a few format digits", function() {
- expect($.numberFormatter.formatNumber("0.0136", {decimalsRightOfZero: 2})).to(equal, "0.01");
+ expect($.numberFormatter.formatNumber("0.0136", {decimalDigits: 2})).to(equal, "0.01");
});
it("handles a lot of format digits", function() {
- expect($.numberFormatter.formatNumber("1.01234567890001", {decimalsRightOfZero: 14})).to(equal, "1.01234567890001");
+ expect($.numberFormatter.formatNumber("1.01234567890001", {decimalDigits: 14})).to(equal, "1.01234567890001");
});
it("handles more format digits than actual digits", function() {
- expect($.numberFormatter.formatNumber("1.5", {decimalsRightOfZero: 8})).to(equal, "1.50000000");
+ expect($.numberFormatter.formatNumber("1.5", {decimalDigits: 8})).to(equal, "1.50000000");
});
it("rounds correctly", function() {
- expect($.numberFormatter.formatNumber("1.875", {decimalsRightOfZero: 2})).to(equal, "1.88");
+ expect($.numberFormatter.formatNumber("1.875", {decimalDigits: 2})).to(equal, "1.88");
});
});

0 comments on commit 55cca69

Please sign in to comment.
Something went wrong with that request. Please try again.