Permalink
Browse files

+ formatting for currency and percent values

  • Loading branch information...
1 parent 44b88f7 commit 5546f388426d018f39820dcc32ddece920061fd4 @lchngr lchngr committed Mar 4, 2013
Showing with 44 additions and 2 deletions.
  1. +14 −2 lib/mondo.js
  2. +30 −0 test/format.js
View
@@ -45,6 +45,14 @@ Mondo.time = Mondo.t = function(time) {
return this.format(time,'t');
};
+Mondo.currency = Mondo.c = function(amount) {
+ return this.format(amount,'c');
+};
+
+Mondo.percent = Mondo.p = function(number) {
+ return this.format(number,'p');
+};
+
Mondo.parseInt = function(value, radix, culture) {
return this.globalize.parseInt(value, radix, culture)
};
@@ -86,7 +94,7 @@ Mondo.pluralize = function(key, options) {
Mondo.translate = function(key) {
var message = this.globalize.culture().messages;
var path = key.split('.');
- for(part in path) {
+ for(var part in path) {
message = message[path[part]];
if(!message) return '';
}
@@ -101,8 +109,12 @@ Mondo.culture = function(selector) {
return this.globalize.culture(selector);
};
+Mondo.addTranslation = function(cultureName, translations) {
+ this.addCultureInfo(cultureName, { messages: translations });
+};
+
Mondo.addCultureInfo = function(cultureName, extendCultureName, info) {
this.globalize.addCultureInfo(cultureName, extendCultureName, info);
-}
+};
}(this));
View
@@ -43,4 +43,34 @@ describe('formatting', function() {
});
});
+
+ describe('currency function', function() {
+
+ it('should format the given amount', function() {
+ var c = Mondo.currency(123.5);
+ c.should.be.a('string');
+ c.should.equal('Fr. 123.50');
+ });
+
+ it('c alias function should produce the same output', function() {
+ var c1 = Mondo.currency(123.5);
+ var c2 = Mondo.c(123.5);
+ c2.should.equal(c1);
+ });
+ });
+
+ describe('percent function', function() {
+
+ it('should format the given number', function() {
+ var p = Mondo.percent(0.2);
+ p.should.be.a('string');
+ p.should.equal('20.00%');
+ });
+
+ it('c alias function should produce the same output', function() {
+ var p1 = Mondo.percent(0.2);
+ var p2 = Mondo.p(0.2);
+ p2.should.equal(p1);
+ });
+ });
});

0 comments on commit 5546f38

Please sign in to comment.