Browse files

refactoring

 - use the best tool
 - call out error condition
  • Loading branch information...
1 parent 25b56ad commit 6783d8f50da31e2d4cd45f1f22fa3ba588f32ca1 @stuarthalloway committed May 21, 2009
View
45 public/javascripts/jquery.numberformatter-1.1.2.js
@@ -136,38 +136,17 @@
};
- function parseOptionsFormat(options) {
- var validFormat = "0#-,.";
-
- // 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
- options.prefix = "";
- options.negativeInFront = false;
- for (var i=0; i<options.format.length; i++)
- {
- if (validFormat.indexOf(options.format.charAt(i))==-1)
- options.prefix = options.prefix + options.format.charAt(i);
- else if (i==0 && options.format.charAt(i)=='-')
- {
- options.negativeInFront = true;
- continue;
- }
- else
- break;
- }
- options.suffix = "";
- for (var i=options.format.length-1; i>=0; i--)
- {
- if (validFormat.indexOf(options.format.charAt(i))==-1)
- options.suffix = options.format.charAt(i) + options.suffix;
- else
- break;
- }
-
- options.format = options.format.substring(options.prefix.length);
- options.format = options.format.substring(0, options.format.length - options.suffix.length);
+ // 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.formatNumber = function(number, options) {
@@ -230,7 +209,7 @@
jQuery.fn.format = function(options) {
var options = jQuery.extend({},jQuery.fn.format.defaults, options);
- parseOptionsFormat(options);
+ parseOptionsFormat(options);
var formatData = formatCodes(options.locale.toLowerCase());
View
5 test/javascript/numberformatter_spec.js
@@ -34,10 +34,9 @@ Screw.Unit(function(){
expect($("#value").text()).to(equal, "-$500000.8");
});
- it("does nothing if it finds non-format characters in the middle", function(){
+ it("throws up if it finds non-format characters in the middle", function(){
$("#value").text("767");
- $("#value").format({format: "## AND ##"});
- expect($("#value").text()).to(equal, "767");
+ expect(function () {$("#value").format({format: "## AND ##"})}).to(throw_object, "invalid number format ## AND ##");
});
it("default to not show decimal for whole numbers", function(){
View
18 vendor/plugins/blue-ridge/lib/screw.matchers.js
@@ -68,6 +68,24 @@ Screw.Matchers = (function($) {
return 'expected ' + $.print(actual) + (not ? ' to not ' : ' to ') + 'be less than ' + $.print(expected);
}
},
+
+ // TODO: add to Screw.Unit
+ throw_object: {
+ match: function(object, actual_fn) {
+ actual_fn._last_err = "[no error]";
+ try {
+ actual_fn();
+ return false;
+ } catch (e) {
+ actual_fn._last_err = e;
+ return e === object;
+ }
+ },
+
+ failure_message: function(expected_exc, actual_fn, not) {
+ return 'expected ' + $.print(actual_fn) + (not ? ' to not ' : ' to ') + 'throw ' + $.print(expected_exc) + ' not "' + actual_fn._last_err + '"';
+ }
+ },
be_lte: {
match: function(expected, actual) {

0 comments on commit 6783d8f

Please sign in to comment.