From 48f2c4f9ee8665c24642106f3535c25dc8d0292b Mon Sep 17 00:00:00 2001 From: rolux Date: Wed, 14 Nov 2012 15:10:04 +0100 Subject: [PATCH] add Ox.formatCount --- source/Ox/js/Format.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/source/Ox/js/Format.js b/source/Ox/js/Format.js index 440acc4a..81b29351 100644 --- a/source/Ox/js/Format.js +++ b/source/Ox/js/Format.js @@ -16,6 +16,20 @@ Ox.formatArea = function(number, decimals) { ) + ' ' + k + 'm\u00B2'; }; +/*@ +Ox.formatCount Returns a string like "2 items", "1 item" or "no items". + > Ox.formatCount(0, 'item') + 'no items' + > Ox.formatCount(1, 'item') + '1 item' + > Ox.formatCount(1000, 'country', 'countries') + '1,000 countries' +@*/ +Ox.formatCount = function(number, singular, plural) { + return (number === 0 ? 'no' : Ox.formatNumber(number)) + ' ' + + ((number === 1) ? singular : plural || singular + 's'); +}; + /*@ Ox.formatCurrency Formats a number with a currency symbol > Ox.formatCurrency(1000, '$', 2)