diff --git a/README.md b/README.md index 455b1ba..012cad5 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,68 @@ -# jQuery Format - -The jQuery Format plugin enables the formatting and parsing of dates and numbers. It's a -client-side alternative of the popular -[SimpleDateFormat](http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html) -and [NumberFormat](http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html) APIs. - -## Usage - -Formatting dates and numbers is as easy as the following: - - $.format.date(new Date(), 'MMMM dd, yyyy KK:mm:ss:SSS a'); - $.format.number(7456.2, '#,##0.00#'); - -Parsing is very similar to the formatting but works with strings as a first parameter: - - $.format.date('1.5.2009', 'dd.MM.yyyy'); - $.format.number('1.231.231.212,3241'); - -The second format parameter is always optional and by default the plugin uses the formats -of the en_US locale. The locale can be globally configured using the following: - - $.format.locale({ - date: { - format: 'dddd, MMMM dd, yyyy h:mm:ss tt', - monthsFull: ['January','February','March','April','May','June','July','August','September','October','November','December'], - monthsShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], - daysFull: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], - daysShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'], - timeFormat: 'h:mm tt', - shortDateFormat: 'M/d/yyyy', - longDateFormat: 'dddd, MMMM dd, yyyy' - }, - number: { - format: '#,##0.0#', - groupingSeparator: ',', - decimalSeparator: '.' - } - }); - -## Changes - -### 11/24/2011 - jQuery Format 1.2 - -- Issue 8: Parsing string -- Issue 5: Decimal rounding -- Fixed issue with fraction formatting. - -### 09/26/2010 - jQuery Format 1.1 - -- Issue 3: Issue on rounding. -- Issue 2: A semicolon is missing line 275. -- Issue 1: Issue with number format #.## (or more). - -### 12/23/2009 - jQuery Format 1.0 - +# jQuery Format + +The jQuery Format plugin enables the formatting and parsing of dates and numbers. It's a +client-side alternative of the popular +[SimpleDateFormat](http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html) +and [NumberFormat](http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html) APIs. + +## Usage + +Formatting dates and numbers is as easy as the following: + + $.format.date(new Date(), 'MMMM dd, yyyy KK:mm:ss:SSS a'); + $.format.number(7456.2, '#,##0.00#'); + $.format.bool(true); + +Parsing is very similar to the formatting but works with strings as a first parameter: + + $.format.date('1.5.2009', 'dd.MM.yyyy'); + $.format.number('1.231.231.212,3241'); + $.format.bool('True'); + +The second format parameter is always optional and by default the plugin uses the formats +of the en_US locale. The locale can be globally configured using the following: + + $.format.locale({ + date: { + format: 'dddd, MMMM dd, yyyy h:mm:ss tt', + monthsFull: ['January','February','March','April','May','June','July','August','September','October','November','December'], + monthsShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], + daysFull: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], + daysShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'], + timeFormat: 'h:mm tt', + shortDateFormat: 'M/d/yyyy', + longDateFormat: 'dddd, MMMM dd, yyyy' + }, + number: { + format: '#,##0.0#', + groupingSeparator: ',', + decimalSeparator: '.' + }, + bool: { + trueString: ['true', 'yes'], + falseString: ['false', 'no'], + trueValue: 'Yes', + falseValue: 'No' + } + }); + +## Changes +### 11/20/2012 - jQuery Format 1.3 + +- Added support for boolean formatting + +### 11/24/2011 - jQuery Format 1.2 + +- Issue 8: Parsing string +- Issue 5: Decimal rounding +- Fixed issue with fraction formatting. + +### 09/26/2010 - jQuery Format 1.1 + +- Issue 3: Issue on rounding. +- Issue 2: A semicolon is missing line 275. +- Issue 1: Issue with number format #.## (or more). + +### 12/23/2009 - jQuery Format 1.0 + Initial release. \ No newline at end of file diff --git a/src/jquery.format.js b/src/jquery.format.js index 4c99a3a..3c01342 100644 --- a/src/jquery.format.js +++ b/src/jquery.format.js @@ -1,526 +1,12 @@ -/* - * jQuery Format Plugin v${version} - * http://www.asual.com/jquery/format/ - * - * Copyright (c) 2009-2011 Rostislav Hristov - * Uses code by Matt Kruse - * Dual licensed under the MIT or GPL Version 2 licenses. - * http://jquery.org/license - * - * Date: ${timestamp} - */ -(function ($) { - - $.format = (function () { - - var UNDEFINED = 'undefined', - TRUE = true, - FALSE = false, - _locale = { - date: { - format: 'MMM dd, yyyy h:mm:ss a', - monthsFull: ['January','February','March','April','May','June', - 'July','August','September','October','November','December'], - monthsShort: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], - daysFull: ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'], - daysShort: ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'], - shortDateFormat: 'M/d/yyyy h:mm a', - longDateFormat: 'EEEE, MMMM dd, yyyy h:mm:ss a' - }, - number: { - format: '#,##0.0#', - groupingSeparator: ',', - decimalSeparator: '.' - } - }; - - return { - - locale: function(value) { - a = {a: 6}; - if (value) { - for (var p in value) { - for (var v in value[p]) { - _locale[p][v] = value[p][v]; - } - } - } - return _locale; - }, - - date: function(value, format) { - - var i = 0, - j = 0, - l = 0, - c = '', - token = '', - x, - y; - - if (typeof value == 'string') { - - var getNumber = function (str, p, minlength, maxlength) { - for (var x = maxlength; x >= minlength; x--) { - var token = str.substring(p, p + x); - if (token.length >= minlength && /^\d+$/.test(token)) { - return token; - } - } - return null; - }; - - if (typeof format == UNDEFINED) { - format = _locale.date.format; - } - - var _strict = false, - pos = 0, - now = new Date(0, 0, 0, 0, 0, 0, 0), - year = now.getYear(), - month = now.getMonth() + 1, - date = 1, - hh = now.getHours(), - mm = now.getMinutes(), - ss = now.getSeconds(), - SSS = now.getMilliseconds(), - ampm = '', - monthName, - dayName; - - while (i < format.length) { - token = ''; - c = format.charAt(i); - while ((format.charAt(i) == c) && (i < format.length)) { - token += format.charAt(i++); - } - if (token.indexOf('MMMM') > - 1 && token.length > 4) { - token = 'MMMM'; - } - if (token.indexOf('EEEE') > - 1 && token.length > 4) { - token = 'EEEE'; - } - if (token == 'yyyy' || token == 'yy' || token == 'y') { - if (token == 'yyyy') { - x = 4; - y = 4; - } - if (token == 'yy') { - x = 2; - y = 2; - } - if (token == 'y') { - x = 2; - y = 4; - } - year = getNumber(value, pos, x, y); - if (year === null) { - return 0; - } - pos += year.length; - if (year.length == 2) { - year = parseInt(year, 10); - if (year > 70) { - year = 1900 + year; - } else { - year = 2000 + year; - } - } - } else if (token == 'MMMM'){ - month = 0; - for (j = 0, l = _locale.date.monthsFull.length; j < l; j++) { - monthName = _locale.date.monthsFull[j]; - if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) { - month = j + 1; - pos += monthName.length; - break; - } - } - if ((month < 1) || (month > 12)){ - return 0; - } - } else if (token == 'MMM'){ - month = 0; - for (j = 0, l = _locale.date.monthsShort.length; j < l; j++) { - monthName = _locale.date.monthsShort[j]; - if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) { - month = j + 1; - pos += monthName.length; - break; - } - } - if ((month < 1) || (month > 12)){ - return 0; - } - } else if (token == 'EEEE'){ - for (j = 0, l = _locale.date.daysFull.length; j < l; j++) { - dayName = _locale.date.daysFull[j]; - if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) { - pos += dayName.length; - break; - } - } - } else if (token == 'EEE'){ - for (j = 0, l =_locale.date.daysShort.length; j < l; j++) { - dayName = _locale.date.daysShort[j]; - if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) { - pos += dayName.length; - break; - } - } - } else if (token == 'MM' || token == 'M') { - month = getNumber(value, pos, _strict ? token.length : 1, 2); - if (month === null || (month < 1) || (month > 12)){ - return 0; - } - pos += month.length; - } else if (token == 'dd' || token == 'd') { - date = getNumber(value, pos, _strict ? token.length : 1, 2); - if (date === null || (date < 1) || (date > 31)){ - return 0; - } - pos += date.length; - } else if (token == 'hh' || token == 'h') { - hh = getNumber(value, pos, _strict ? token.length : 1, 2); - if (hh === null || (hh < 1) || (hh > 12)) { - return 0; - } - pos += hh.length; - } else if (token == 'HH' || token == 'H') { - hh = getNumber(value, pos, _strict ? token.length : 1, 2); - if(hh === null || (hh < 0) || (hh > 23)){ - return 0; - } - pos += hh.length; - } else if (token == 'KK' || token == 'K') { - hh = getNumber(value, pos, _strict ? token.length : 1, 2); - if (hh === null || (hh < 0) || (hh > 11)){ - return 0; - } - pos += hh.length; - } else if (token == 'kk' || token == 'k') { - hh = getNumber(value, pos, _strict ? token.length : 1, 2); - if (hh === null || (hh < 1) || (hh > 24)){ - return 0; - } - pos += hh.length; - hh--; - } else if (token == 'mm' || token == 'm') { - mm = getNumber(value, pos, _strict ? token.length : 1, 2); - if (mm === null || (mm < 0) || ( mm > 59)) { - return 0; - } - pos += mm.length; - } else if (token == 'ss' || token == 's') { - ss = getNumber(value, pos, _strict ? token.length : 1, 2); - if (ss === null || (ss < 0) || (ss > 59)){ - return 0; - } - pos += ss.length; - } else if (token == 'SSS' || token == 'SS' || token == 'S') { - SSS = getNumber(value, pos, _strict ? token.length : 1, 3); - if (SSS === null || (SSS < 0) || (SSS > 999)){ - return 0; - } - pos += SSS.length; - } else if (token == 'a') { - var ap = value.substring(pos, pos + 2).toLowerCase(); - if (ap == 'am') { - ampm = 'AM'; - } else if (ap == 'pm') { - ampm = 'PM'; - } else { - return 0; - } - pos += 2; - } else { - if (token != value.substring(pos, pos + token.length)) { - return 0; - } else { - pos += token.length; - } - } - } - if (pos != value.length) { - return 0; - } - if (month == 2) { - if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) { - if (date > 29) { - return 0; - } - } else { - if (date > 28) { - return 0; - } - } - } - if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) { - if (date > 30) { - return 0; - } - } - if (hh < 12 && ampm == 'PM') { - hh = hh - 0 + 12; - } else if (hh > 11 && ampm == 'AM') { - hh -= 12; - } - - return (new Date(year, month - 1, date, hh, mm, ss, SSS)); - - } else { - - var formatNumber = function (n, s) { - if (typeof s == UNDEFINED || s == 2) { - return (n >= 0 && n < 10 ? '0' : '') + n; - } else { - if (n >= 0 && n < 10) { - return '00' + n; - } - if (n >= 10 && n <100) { - return '0' + n; - } - return n; - } - }; - - if (typeof format == UNDEFINED) { - format = _locale.date.format; - } - - y = value.getYear(); - if (y < 1000) { - y = String(y + 1900); - } - - var M = value.getMonth() + 1, - d = value.getDate(), - E = value.getDay(), - H = value.getHours(), - m = value.getMinutes(), - s = value.getSeconds(), - S = value.getMilliseconds(); - - value = { - y: y, - yyyy: y, - yy: String(y).substring(2, 4), - M: M, - MM: formatNumber(M), - MMM: _locale.date.monthsShort[M-1], - MMMM: _locale.date.monthsFull[M-1], - d: d, - dd: formatNumber(d), - EEE: _locale.date.daysShort[E], - EEEE: _locale.date.daysFull[E], - H: H, - HH: formatNumber(H) - }; - - if (H === 0) { - value.h = 12; - } else if (H > 12) { - value.h = H - 12; - } else { - value.h = H; - } - - value.hh = formatNumber(value.h); - value.k = H !== 0 ? H : 24; - value.kk = formatNumber(value.k); - - if (H > 11) { - value.K = H - 12; - } else { - value.K = H; - } - - value.KK = formatNumber(value.K); - - if (H > 11) { - value.a = 'PM'; - } else { - value.a = 'AM'; - } - - value.m = m; - value.mm = formatNumber(m); - value.s = s; - value.ss = formatNumber(s); - value.S = S; - value.SS = formatNumber(S); - value.SSS = formatNumber(S, 3); - - var result = ''; - - i = 0; - c = ''; - token = ''; - s = false; - - while (i < format.length) { - token = ''; - c = format.charAt(i); - if (c == '\'') { - i++; - if (format.charAt(i) == c) { - result = result + c; - i++; - } else { - s = !s; - } - } else { - while (format.charAt(i) == c) { - token += format.charAt(i++); - } - if (token.indexOf('MMMM') != -1 && token.length > 4) { - token = 'MMMM'; - } - if (token.indexOf('EEEE') != -1 && token.length > 4) { - token = 'EEEE'; - } - if (typeof value[token] != UNDEFINED && !s) { - result = result + value[token]; - } else { - result = result + token; - } - } - } - return result; - } - }, - - number: function(value, format) { - - var groupingSeparator, - groupingIndex, - decimalSeparator, - decimalIndex, - roundFactor, - result, - i; - - if (typeof value == 'string') { - - groupingSeparator = _locale.number.groupingSeparator; - decimalSeparator = _locale.number.decimalSeparator; - decimalIndex = value.indexOf(decimalSeparator); - - roundFactor = 1; - - if (decimalIndex != -1) { - roundFactor = Math.pow(10, value.length - decimalIndex - 1); - } - - value = value.replace(new RegExp('[' + groupingSeparator + ']', 'g'), ''); - value = value.replace(new RegExp('[' + decimalSeparator + ']'), '.'); - - return Math.round(value*roundFactor)/roundFactor; - - } else { - - if (typeof format == UNDEFINED || format.length < 1) { - format = _locale.number.format; - } - - groupingSeparator = ','; - groupingIndex = format.lastIndexOf(groupingSeparator); - decimalSeparator = '.'; - decimalIndex = format.indexOf(decimalSeparator); - - var integer = '', - fraction = '', - negative = value < 0, - minFraction = format.substr(decimalIndex + 1).replace(/#/g, '').length, - maxFraction = format.substr(decimalIndex + 1).length, - powFraction = 10; - - value = Math.abs(value); - - if (decimalIndex != -1) { - fraction = _locale.number.decimalSeparator; - if (maxFraction > 0) { - roundFactor = 1000; - powFraction = Math.pow(powFraction, maxFraction); - var tempRound = Math.round(parseInt(value * powFraction * roundFactor - - Math.round(value) * powFraction * roundFactor, 10) / roundFactor), - tempFraction = String(tempRound < 0 ? Math.round(parseInt(value * powFraction * roundFactor - - parseInt(value, 10) * powFraction * roundFactor, 10) / roundFactor) : tempRound), - parts = value.toString().split('.'); - if (typeof parts[1] != UNDEFINED) { - for (i = 0; i < maxFraction; i++) { - if (parts[1].substr(i, 1) == '0' && i < maxFraction - 1 && - (tempFraction.length != maxFraction || tempFraction.substr(0, 1) == '0')) { - tempFraction = '0' + tempFraction; - } else { - break; - } - } - } - for (i = 0; i < (maxFraction - fraction.length); i++) { - tempFraction += '0'; - } - var symbol, - formattedFraction = ''; - for (i = 0; i < tempFraction.length; i++) { - symbol = tempFraction.substr(i, 1); - if (i >= minFraction && symbol == '0' && /^0*$/.test(tempFraction.substr(i+1))) { - break; - } - formattedFraction += symbol; - } - fraction += formattedFraction; - } - if (fraction == _locale.number.decimalSeparator) { - fraction = ''; - } - } - - if (decimalIndex !== 0) { - if (fraction != '') { - integer = String(parseInt(Math.round(value * powFraction) / powFraction, 10)); - } else { - integer = String(Math.round(value)); - } - var grouping = _locale.number.groupingSeparator, - groupingSize = 0; - if (groupingIndex != -1) { - if (decimalIndex != -1) { - groupingSize = decimalIndex - groupingIndex; - } else { - groupingSize = format.length - groupingIndex; - } - groupingSize--; - } - if (groupingSize > 0) { - var count = 0, - formattedInteger = ''; - i = integer.length; - while (i--) { - if (count !== 0 && count % groupingSize === 0) { - formattedInteger = grouping + formattedInteger; - } - formattedInteger = integer.substr(i, 1) + formattedInteger; - count++; - } - integer = formattedInteger; - } - var maxInteger, maxRegExp = /#|,/g; - if (decimalIndex != -1) { - maxInteger = format.substr(0, decimalIndex).replace(maxRegExp, '').length; - } else { - maxInteger = format.replace(maxRegExp, '').length; - } - var tempInteger = integer.length; - for (i = tempInteger; i < maxInteger; i++) { - integer = '0' + integer; - } - } - result = integer + fraction; - return (negative ? '-' : '') + result; - } - } - }; - })(); - -}(jQuery)); \ No newline at end of file +/* + * jQuery Format Plugin v${version} + * http://www.asual.com/jquery/format/ + * + * Copyright (c) 2009-2011 Rostislav Hristov + * Uses code by Matt Kruse + * Dual licensed under the MIT or GPL Version 2 licenses. + * http://jquery.org/license + * + * Date: ${timestamp} + */ +(function(v){var m={date:{format:"MMM dd, yyyy h:mm:ss a",monthsFull:"January February March April May June July August September October November December".split(" "),monthsShort:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),daysFull:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),daysShort:"Sun Mon Tue Wed Thu Fri Sat".split(" "),shortDateFormat:"M/d/yyyy h:mm a",longDateFormat:"EEEE, MMMM dd, yyyy h:mm:ss a"},number:{format:"#,##0.0#",groupingSeparator:",", decimalSeparator:"."},bool:{trueString:["true","yes"],falseString:["false","no"],trueValue:"Yes",falseValue:"No"}};v.format={locale:function(b){a={a:6};if(b)for(var k in b)for(var g in b[k])m[k][g]=b[k][g];return m},bool:function(b){"string"===typeof b&&(b=b.toLowerCase(),v.inArray(b,m.bool.trueString)?b=!0:v.inArray(b,m.bool.falseString)&&(b=!1));return b?m.bool.trueValue:m.bool.falseValue},date:function(b,k){var g=0,h=0,c=0,c=h="",n,e;if("string"==typeof b){var q=function(b,c,d,e){for(;e>=d;e--){var f= b.substring(c,c+e);if(f.length>=d&&/^\d+$/.test(f))return f}return null};"undefined"==typeof k&&(k=m.date.format);for(var d=0,l=new Date(0,0,0,0,0,0,0),j=l.getYear(),f=l.getMonth()+1,p=1,i=l.getHours(),t=l.getMinutes(),u=l.getSeconds(),l=l.getMilliseconds(),s="",r;gf||12f||12f||12p||31i||12i||23i||11i||24t||59u||59l||999i&&"PM"==s?i=i-0+12:11b?"0":"")+b:0<=b&&10>b?"00"+b:10<=b&&100>b?"0"+b:b};"undefined"==typeof k&&(k=m.date.format);e=b.getYear();1E3>e&&(e=String(e+1900));q=b.getMonth()+1;d=b.getDate();j=b.getDay();f=b.getHours();p=b.getMinutes();n=b.getSeconds();i=b.getMilliseconds();b={y:e,yyyy:e,yy:String(e).substring(2,4),M:q,MM:g(q),MMM:m.date.monthsShort[q-1],MMMM:m.date.monthsFull[q-1],d:d,dd:g(d),EEE:m.date.daysShort[j],EEEE:m.date.daysFull[j],H:f,HH:g(f)};b.h=0===f?12:12k.length)k=m.number.format;h=k.lastIndexOf(",");n=k.indexOf(".");c=g="";var q=0>b,d=k.substr(n+1).replace(/#/g,"").length,l=k.substr(n+1).length,j=10,b=Math.abs(b);if(-1!=n){c=m.number.decimalSeparator;if(0f?Math.round(parseInt(b*j* e-parseInt(b,10)*j*e,10)/e):f),p=b.toString().split(".");if("undefined"!=typeof p[1])for(e=0;e=d&&"0"==l&&/^0*$/.test(f.substr(e+1)))break;p+=l}c+=p}c==m.number.decimalSeparator&&(c="")}if(0!==n){g=""!=c?String(parseInt(Math.round(b*j)/j,10)):String(Math.round(b));d=m.number.groupingSeparator;j=0;-1!=h&&(j=-1!=n?n-h:k.length- h,j--);if(0=y;w--){var x=r.substring(v,v+w);if(x.length>=y&&/^\d+$/.test(x))return x}return null};if(typeof i=="undefined")i=o.date.format;var d=0,n=new Date(0,0,0,0,0,0,0),j=n.getYear(),f=n.getMonth()+1,p=1,k=n.getHours(),t=n.getMinutes(), -u=n.getSeconds();n=n.getMilliseconds();for(var s="",q;g-1&&c.length>4)c="MMMM";if(c.indexOf("EEEE")>-1&&c.length>4)c="EEEE";if(c=="yyyy"||c=="yy"||c=="y"){if(c=="yyyy")e=l=4;if(c=="yy")e=l=2;if(c=="y"){l=2;e=4}j=m(b,d,l,e);if(j===null)return 0;d+=j.length;if(j.length==2){j=parseInt(j,10);j=j>70?1900+j:2E3+j}}else if(c=="MMMM"){h=f=0;for(c=o.date.monthsFull.length;h12)return 0}else if(c=="MMM"){h=f=0;for(c=o.date.monthsShort.length;h12)return 0}else if(c=="EEEE"){h=0;for(c=o.date.daysFull.length;h12)return 0;d+=f.length}else if(c=="dd"||c=="d"){p=m(b,d,1,2);if(p===null||p<1||p>31)return 0;d+=p.length}else if(c=="hh"||c=="h"){k=m(b,d,1,2);if(k===null||k<1||k>12)return 0;d+=k.length}else if(c=="HH"||c=="H"){k=m(b,d,1,2);if(k===null||k<0||k>23)return 0;d+=k.length}else if(c=="KK"||c=="K"){k=m(b,d,1,2);if(k===null||k<0||k>11)return 0; -d+=k.length}else if(c=="kk"||c=="k"){k=m(b,d,1,2);if(k===null||k<1||k>24)return 0;d+=k.length;k--}else if(c=="mm"||c=="m"){t=m(b,d,1,2);if(t===null||t<0||t>59)return 0;d+=t.length}else if(c=="ss"||c=="s"){u=m(b,d,1,2);if(u===null||u<0||u>59)return 0;d+=u.length}else if(c=="SSS"||c=="SS"||c=="S"){n=m(b,d,1,3);if(n===null||n<0||n>999)return 0;d+=n.length}else if(c=="a"){s=b.substring(d,d+2).toLowerCase();if(s=="am")s="AM";else if(s=="pm")s="PM";else return 0;d+=2}else if(c!=b.substring(d,d+c.length))return 0; -else d+=c.length}if(d!=b.length)return 0;if(f==2)if(j%4===0&&j%100!==0||j%400===0){if(p>29)return 0}else if(p>28)return 0;if(f==4||f==6||f==9||f==11)if(p>30)return 0;if(k<12&&s=="PM")k=k-0+12;else if(k>11&&s=="AM")k-=12;return new Date(j,f-1,p,k,t,u,n)}else{g=function(r,v){if(typeof v=="undefined"||v==2)return(r>=0&&r<10?"0":"")+r;else{if(r>=0&&r<10)return"00"+r;if(r>=10&&r<100)return"0"+r;return r}};if(typeof i=="undefined")i=o.date.format;e=b.getYear();if(e<1E3)e=String(e+1900);m=b.getMonth()+1; -d=b.getDate();j=b.getDay();f=b.getHours();p=b.getMinutes();l=b.getSeconds();k=b.getMilliseconds();b={y:e,yyyy:e,yy:String(e).substring(2,4),M:m,MM:g(m),MMM:o.date.monthsShort[m-1],MMMM:o.date.monthsFull[m-1],d:d,dd:g(d),EEE:o.date.daysShort[j],EEEE:o.date.daysFull[j],H:f,HH:g(f)};b.h=f===0?12:f>12?f-12:f;b.hh=g(b.h);b.k=f!==0?f:24;b.kk=g(b.k);b.K=f>11?f-12:f;b.KK=g(b.K);b.a=f>11?"PM":"AM";b.m=p;b.mm=g(p);b.s=l;b.ss=g(l);b.S=k;b.SS=g(k);b.SSS=g(k,3);e="";g=0;c=h="";for(l=false;g4)c="MMMM";if(c.indexOf("EEEE")!=-1&&c.length>4)c="EEEE";e+=typeof b[c]!="undefined"&&!l?b[c]:c}}return e}},number:function(b,i){var g,h,c,l,e;if(typeof b=="string"){g=o.number.groupingSeparator;c=o.number.decimalSeparator;l=b.indexOf(c);e=1;if(l!=-1)e=Math.pow(10,b.length-l-1);b=b.replace(new RegExp("["+g+"]","g"),"");b=b.replace(new RegExp("["+c+"]"),"."); -return Math.round(b*e)/e}else{if(typeof i=="undefined"||i.length<1)i=o.number.format;g=",";h=i.lastIndexOf(g);c=".";l=i.indexOf(c);var m=g="";c=b<0;var d=i.substr(l+1).replace(/#/g,"").length,n=i.substr(l+1).length,j=10;b=Math.abs(b);if(l!=-1){m=o.number.decimalSeparator;if(n>0){e=1E3;j=Math.pow(j,n);var f=Math.round(parseInt(b*j*e-Math.round(b)*j*e,10)/e);f=String(f<0?Math.round(parseInt(b*j*e-parseInt(b,10)*j*e,10)/e):f);var p=b.toString().split(".");if(typeof p[1]!="undefined")for(e=0;e=d&&n=="0"&&/^0*$/.test(f.substr(e+1)))break;p+=n}m+=p}if(m==o.number.decimalSeparator)m=""}if(l!==0){g=m!=""?String(parseInt(Math.round(b*j)/j,10)):String(Math.round(b));b=o.number.groupingSeparator;d=0;if(h!=-1){d=l!=-1?l-h:i.length-h;d--}if(d>0){h=0;j="";for(e=g.length;e--;){if(h!==0&&h%d===0)j=b+j;j=g.substr(e,1)+j;h++}g=j}h=/#|,/g;i=l!=-1?i.substr(0, -l).replace(h,"").length:i.replace(h,"").length;for(e=g.length;e -1) { + value = true; + } + else if ($.inArray(value, _locale.bool.falseString) > -1) { + value = false; + } + } + + if (value) { + return _locale.bool.trueValue; + } + else { + return _locale.bool.falseValue; + } + }, + + date: function(value, format) { + + var i = 0, + j = 0, + l = 0, + c = '', + token = '', + x, + y; + + if (typeof value == 'string') { + + var getNumber = function (str, p, minlength, maxlength) { + for (var x = maxlength; x >= minlength; x--) { + var token = str.substring(p, p + x); + if (token.length >= minlength && /^\d+$/.test(token)) { + return token; + } + } + return null; + }; + + if (typeof format == UNDEFINED) { + format = _locale.date.format; + } + + var _strict = false, + pos = 0, + now = new Date(0, 0, 0, 0, 0, 0, 0), + year = now.getYear(), + month = now.getMonth() + 1, + date = 1, + hh = now.getHours(), + mm = now.getMinutes(), + ss = now.getSeconds(), + SSS = now.getMilliseconds(), + ampm = '', + monthName, + dayName; + + while (i < format.length) { + token = ''; + c = format.charAt(i); + while ((format.charAt(i) == c) && (i < format.length)) { + token += format.charAt(i++); + } + if (token.indexOf('MMMM') > - 1 && token.length > 4) { + token = 'MMMM'; + } + if (token.indexOf('EEEE') > - 1 && token.length > 4) { + token = 'EEEE'; + } + if (token == 'yyyy' || token == 'yy' || token == 'y') { + if (token == 'yyyy') { + x = 4; + y = 4; + } + if (token == 'yy') { + x = 2; + y = 2; + } + if (token == 'y') { + x = 2; + y = 4; + } + year = getNumber(value, pos, x, y); + if (year === null) { + return 0; + } + pos += year.length; + if (year.length == 2) { + year = parseInt(year, 10); + if (year > 70) { + year = 1900 + year; + } else { + year = 2000 + year; + } + } + } else if (token == 'MMMM'){ + month = 0; + for (j = 0, l = _locale.date.monthsFull.length; j < l; j++) { + monthName = _locale.date.monthsFull[j]; + if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) { + month = j + 1; + pos += monthName.length; + break; + } + } + if ((month < 1) || (month > 12)){ + return 0; + } + } else if (token == 'MMM'){ + month = 0; + for (j = 0, l = _locale.date.monthsShort.length; j < l; j++) { + monthName = _locale.date.monthsShort[j]; + if (value.substring(pos, pos + monthName.length).toLowerCase() == monthName.toLowerCase()) { + month = j + 1; + pos += monthName.length; + break; + } + } + if ((month < 1) || (month > 12)){ + return 0; + } + } else if (token == 'EEEE'){ + for (j = 0, l = _locale.date.daysFull.length; j < l; j++) { + dayName = _locale.date.daysFull[j]; + if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) { + pos += dayName.length; + break; + } + } + } else if (token == 'EEE'){ + for (j = 0, l =_locale.date.daysShort.length; j < l; j++) { + dayName = _locale.date.daysShort[j]; + if (value.substring(pos, pos + dayName.length).toLowerCase() == dayName.toLowerCase()) { + pos += dayName.length; + break; + } + } + } else if (token == 'MM' || token == 'M') { + month = getNumber(value, pos, _strict ? token.length : 1, 2); + if (month === null || (month < 1) || (month > 12)){ + return 0; + } + pos += month.length; + } else if (token == 'dd' || token == 'd') { + date = getNumber(value, pos, _strict ? token.length : 1, 2); + if (date === null || (date < 1) || (date > 31)){ + return 0; + } + pos += date.length; + } else if (token == 'hh' || token == 'h') { + hh = getNumber(value, pos, _strict ? token.length : 1, 2); + if (hh === null || (hh < 1) || (hh > 12)) { + return 0; + } + pos += hh.length; + } else if (token == 'HH' || token == 'H') { + hh = getNumber(value, pos, _strict ? token.length : 1, 2); + if(hh === null || (hh < 0) || (hh > 23)){ + return 0; + } + pos += hh.length; + } else if (token == 'KK' || token == 'K') { + hh = getNumber(value, pos, _strict ? token.length : 1, 2); + if (hh === null || (hh < 0) || (hh > 11)){ + return 0; + } + pos += hh.length; + } else if (token == 'kk' || token == 'k') { + hh = getNumber(value, pos, _strict ? token.length : 1, 2); + if (hh === null || (hh < 1) || (hh > 24)){ + return 0; + } + pos += hh.length; + hh--; + } else if (token == 'mm' || token == 'm') { + mm = getNumber(value, pos, _strict ? token.length : 1, 2); + if (mm === null || (mm < 0) || ( mm > 59)) { + return 0; + } + pos += mm.length; + } else if (token == 'ss' || token == 's') { + ss = getNumber(value, pos, _strict ? token.length : 1, 2); + if (ss === null || (ss < 0) || (ss > 59)){ + return 0; + } + pos += ss.length; + } else if (token == 'SSS' || token == 'SS' || token == 'S') { + SSS = getNumber(value, pos, _strict ? token.length : 1, 3); + if (SSS === null || (SSS < 0) || (SSS > 999)){ + return 0; + } + pos += SSS.length; + } else if (token == 'a') { + var ap = value.substring(pos, pos + 2).toLowerCase(); + if (ap == 'am') { + ampm = 'AM'; + } else if (ap == 'pm') { + ampm = 'PM'; + } else { + return 0; + } + pos += 2; + } else { + if (token != value.substring(pos, pos + token.length)) { + return 0; + } else { + pos += token.length; + } + } + } + if (pos != value.length) { + return 0; + } + if (month == 2) { + if (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) { + if (date > 29) { + return 0; + } + } else { + if (date > 28) { + return 0; + } + } + } + if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) { + if (date > 30) { + return 0; + } + } + if (hh < 12 && ampm == 'PM') { + hh = hh - 0 + 12; + } else if (hh > 11 && ampm == 'AM') { + hh -= 12; + } + + return (new Date(year, month - 1, date, hh, mm, ss, SSS)); + + } else { + + var formatNumber = function (n, s) { + if (typeof s == UNDEFINED || s == 2) { + return (n >= 0 && n < 10 ? '0' : '') + n; + } else { + if (n >= 0 && n < 10) { + return '00' + n; + } + if (n >= 10 && n <100) { + return '0' + n; + } + return n; + } + }; + + if (typeof format == UNDEFINED) { + format = _locale.date.format; + } + + y = value.getYear(); + if (y < 1000) { + y = String(y + 1900); + } + + var M = value.getMonth() + 1, + d = value.getDate(), + E = value.getDay(), + H = value.getHours(), + m = value.getMinutes(), + s = value.getSeconds(), + S = value.getMilliseconds(); + + value = { + y: y, + yyyy: y, + yy: String(y).substring(2, 4), + M: M, + MM: formatNumber(M), + MMM: _locale.date.monthsShort[M-1], + MMMM: _locale.date.monthsFull[M-1], + d: d, + dd: formatNumber(d), + EEE: _locale.date.daysShort[E], + EEEE: _locale.date.daysFull[E], + H: H, + HH: formatNumber(H) + }; + + if (H === 0) { + value.h = 12; + } else if (H > 12) { + value.h = H - 12; + } else { + value.h = H; + } + + value.hh = formatNumber(value.h); + value.k = H !== 0 ? H : 24; + value.kk = formatNumber(value.k); + + if (H > 11) { + value.K = H - 12; + } else { + value.K = H; + } + + value.KK = formatNumber(value.K); + + if (H > 11) { + value.a = 'PM'; + } else { + value.a = 'AM'; + } + + value.m = m; + value.mm = formatNumber(m); + value.s = s; + value.ss = formatNumber(s); + value.S = S; + value.SS = formatNumber(S); + value.SSS = formatNumber(S, 3); + + var result = ''; + + i = 0; + c = ''; + token = ''; + s = false; + + while (i < format.length) { + token = ''; + c = format.charAt(i); + if (c == '\'') { + i++; + if (format.charAt(i) == c) { + result = result + c; + i++; + } else { + s = !s; + } + } else { + while (format.charAt(i) == c) { + token += format.charAt(i++); + } + if (token.indexOf('MMMM') != -1 && token.length > 4) { + token = 'MMMM'; + } + if (token.indexOf('EEEE') != -1 && token.length > 4) { + token = 'EEEE'; + } + if (typeof value[token] != UNDEFINED && !s) { + result = result + value[token]; + } else { + result = result + token; + } + } + } + return result; + } + }, + + number: function(value, format) { + + var groupingSeparator, + groupingIndex, + decimalSeparator, + decimalIndex, + roundFactor, + result, + i; + + if (typeof value == 'string') { + + groupingSeparator = _locale.number.groupingSeparator; + decimalSeparator = _locale.number.decimalSeparator; + decimalIndex = value.indexOf(decimalSeparator); + + roundFactor = 1; + + if (decimalIndex != -1) { + roundFactor = Math.pow(10, value.length - decimalIndex - 1); + } + + value = value.replace(new RegExp('[' + groupingSeparator + ']', 'g'), ''); + value = value.replace(new RegExp('[' + decimalSeparator + ']'), '.'); + + return Math.round(value*roundFactor)/roundFactor; + + } else { + + if (typeof format == UNDEFINED || format.length < 1) { + format = _locale.number.format; + } + + groupingSeparator = ','; + groupingIndex = format.lastIndexOf(groupingSeparator); + decimalSeparator = '.'; + decimalIndex = format.indexOf(decimalSeparator); + + var integer = '', + fraction = '', + negative = value < 0, + minFraction = format.substr(decimalIndex + 1).replace(/#/g, '').length, + maxFraction = format.substr(decimalIndex + 1).length, + powFraction = 10; + + value = Math.abs(value); + + if (decimalIndex != -1) { + fraction = _locale.number.decimalSeparator; + if (maxFraction > 0) { + roundFactor = 1000; + powFraction = Math.pow(powFraction, maxFraction); + var tempRound = Math.round(parseInt(value * powFraction * roundFactor - + Math.round(value) * powFraction * roundFactor, 10) / roundFactor), + tempFraction = String(tempRound < 0 ? Math.round(parseInt(value * powFraction * roundFactor - + parseInt(value, 10) * powFraction * roundFactor, 10) / roundFactor) : tempRound), + parts = value.toString().split('.'); + if (typeof parts[1] != UNDEFINED) { + for (i = 0; i < maxFraction; i++) { + if (parts[1].substr(i, 1) == '0' && i < maxFraction - 1 && + (tempFraction.length != maxFraction || tempFraction.substr(0, 1) == '0')) { + tempFraction = '0' + tempFraction; + } else { + break; + } + } + } + for (i = 0; i < (maxFraction - fraction.length); i++) { + tempFraction += '0'; + } + var symbol, + formattedFraction = ''; + for (i = 0; i < tempFraction.length; i++) { + symbol = tempFraction.substr(i, 1); + if (i >= minFraction && symbol == '0' && /^0*$/.test(tempFraction.substr(i+1))) { + break; + } + formattedFraction += symbol; + } + fraction += formattedFraction; + } + if (fraction == _locale.number.decimalSeparator) { + fraction = ''; + } + } + + if (decimalIndex !== 0) { + if (fraction != '') { + integer = String(parseInt(Math.round(value * powFraction) / powFraction, 10)); + } else { + integer = String(Math.round(value)); + } + var grouping = _locale.number.groupingSeparator, + groupingSize = 0; + if (groupingIndex != -1) { + if (decimalIndex != -1) { + groupingSize = decimalIndex - groupingIndex; + } else { + groupingSize = format.length - groupingIndex; + } + groupingSize--; + } + if (groupingSize > 0) { + var count = 0, + formattedInteger = ''; + i = integer.length; + while (i--) { + if (count !== 0 && count % groupingSize === 0) { + formattedInteger = grouping + formattedInteger; + } + formattedInteger = integer.substr(i, 1) + formattedInteger; + count++; + } + integer = formattedInteger; + } + var maxInteger, maxRegExp = /#|,/g; + if (decimalIndex != -1) { + maxInteger = format.substr(0, decimalIndex).replace(maxRegExp, '').length; + } else { + maxInteger = format.replace(maxRegExp, '').length; + } + var tempInteger = integer.length; + for (i = tempInteger; i < maxInteger; i++) { + integer = '0' + integer; + } + } + result = integer + fraction; + return (negative ? '-' : '') + result; + } + } + }; + })(); + +}(jQuery)); \ No newline at end of file diff --git a/test/test.js b/test/test.js index cdf3d03..e6ea026 100644 --- a/test/test.js +++ b/test/test.js @@ -1,160 +1,177 @@ -module("Dates"); - -test("Basic requirements", function() { - - var d = new Date(); - var f = 'MMMM dd, yyyy KK:mm:ss:SSS a'; - var df = $.format.date(d, f); - equals($.format.date(df, f).getTime(), d.getTime()); - - f = 'dd.MM.yyyy'; - df = $.format.date('1.5.2009', f); - d = new Date(df); - equals($.format.date(d, f), '01.05.2009'); - - d = new Date(); - d.setHours(0); - d.setMinutes(0); - f = 'h:mm a'; - equals($.format.date(d, f), '12:00 AM'); - - d = new Date(); - d.setHours(0); - d.setMinutes(0); - f = 'k:mm a'; - equals($.format.date(d, f), '24:00 AM'); - - d = new Date(); - d.setHours(12); - d.setMinutes(0); - f = 'k:mm a'; - equals($.format.date(d, f), '12:00 PM'); - - d = new Date(); - d.setFullYear(2012); - d.setMonth(1); - d.setDate(22); - d.setHours(0); - d.setMinutes(0); - d.setSeconds(0); - equals($.format.date('2012-02-22', 'yyyy-MM-dd').toString(), d.toString()); - - $.format.locale({ - date: { - format: 'EEEE, \'o\'\'clock\' dd\' de \'MMMM\' de \'yyyy H:mm:ss', - monthsFull: ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'], - monthsShort: ['ene','feb','mar','abr','may','jun','jul','ago','sep','oct','nov','dic'], - daysFull: ['domingo','lunes','martes','mi�rcoles','jueves','viernes','s�bado'], - daysShort: ['dom','lun','mar','mi�','jue','vie','s�b'], - timeFormat: 'H:mm:ss', - shortDateFormat: 'dd/MM/yyyy', - longDateFormat: 'EEEE, dd\' de \'MMMM\' de \'yyyy' - } - }); - - d = new Date(); - d.setYear(1976); - d.setMonth(4); - d.setDate(31); - d.setHours(3); - d.setMinutes(20); - d.setSeconds(43); - d.setMilliseconds(0); - - equals($.format.date(d), 'lunes, o\'clock 31 de mayo de 1976 3:20:43'); - -}); - -module("Number"); - -test("Basic requirements", function() { - - equals($.format.number(0.123, '#0.0000'), '0.1230'); - equals($.format.number(7456.2, '#,##0.0#'), '7,456.2'); - equals($.format.number(7456.351, '#,##0.#'), '7,456.4'); - equals($.format.number(123.4, '#,##0.00#'), '123.40'); - equals($.format.number(12.32410, '#,##0.0000#'), '12.3241'); - equals($.format.number(0.123213, '#,##0.00###'), '0.12321'); - equals($.format.number(2101.234, '#,###'), '2,101'); - equals($.format.number(101.7, '#,###'), '102'); - equals($.format.number(0.123, '.0000'), '.1230'); - equals($.format.number(5.155, '.0#'), '.16'); - equals($.format.number(540.23, '###'), '540'); - equals($.format.number(540.23, '###.'), '540'); - equals($.format.number(540, '###.##'), '540'); - equals($.format.number(19.03), '19.03'); - equals($.format.number(5.0303, '00.0000'), '05.0303'); - equals($.format.number(5.0099, '#,##0.00'), '5.01'); - equals($.format.number(5.1009, '#,##0.00'), '5.10'); - equals($.format.number(5.1001, '#,##0.000'), '5.100'); - equals($.format.number(1, "#,##0.00"), '1.00'); - equals($.format.number(1.2, "#,##0.#"), '1.2'); - - equals($.format.number(23540.23, '#,###.##'), '23,540.23'); - equals($.format.number(23540.23, '#,###'), '23,540'); - equals($.format.number(23540.23, '#####.000'), '23540.230'); - equals($.format.number(3.14, '#00.#'), '03.1'); - equals($.format.number(10.9, '#,##0.0#'), '10.9'); - equals($.format.number(2.096, '##0.00'), '2.10'); - equals($.format.number(2.046, '##0.00'), '2.05'); - equals($.format.number(2.044, '##0.00'), '2.04'); - equals($.format.number(2.196, '##0.00'), '2.20'); - - equals($.format.number(1.02, "#.##"), '1.02'); - equals($.format.number(1.05, '##0.00'), '1.05'); - equals($.format.number(1.12, "#.##"), '1.12'); - equals($.format.number(1.999, "#.##"), '2'); - equals($.format.number(-2000.10, '#,###.00'), '-2,000.10'); - - equals($.format.number('.1230'), 0.123); - equals($.format.number('86.02'), 86.02); - equals($.format.number('03.14'), 3.14); - equals($.format.number('64.21'), 64.21); - - $.format.locale({ - number: { - groupingSeparator: '.', - decimalSeparator: ',' - } - }); - - equals($.format.number(123.4, '#,##0.000'), '123,400'); - equals($.format.number(12.32410, '#,##0.0000#'), '12,3241'); - equals($.format.number(2123.4, '#,##0.000'), '2.123,400'); - equals($.format.number(1231231212.32410, '#,##0.0000#'), '1.231.231.212,3241'); - equals($.format.number(-999, "#,###"), '-999'); - - equals($.format.number('1.231.231.212,3241'), 1231231212.3241); - equals($.format.number('18,00.5'), 18.005); - equals($.format.number('71,49'), 71.49); - - equals($.format.number(99.9999, '#,##0.0'), '100,0'); - equals($.format.number(99.9999, '#,##0.000'), '100,000'); - equals($.format.number(99.9999, '#,##0.0000'), '99,9999'); - - equals($.format.number(-99.9999, '#,##0.0'), '-100,0'); - equals($.format.number(-99.9999, '#,##0.000'), '-100,000'); - equals($.format.number(-99.9999, '#,##0.0000'), '-99,9999'); - - equals($.format.number(99.8999, '#,##0.00'), '99,90'); - equals($.format.number(99.9444, '#,##0.00'), '99,94'); - equals($.format.number(99.9454, '#,##0.00'), '99,95'); - equals($.format.number(99.9944, '#,##0.00'), '99,99'); - - $.format.locale({ - number: { - groupingSeparator: '\'', - decimalSeparator: '.' - } - }); - - equals($.format.number(123.4, '#,##0.000'), '123.400'); - equals($.format.number(12.32410, '#,##0.0000#'), '12.3241'); - equals($.format.number(2123.4, '#,##0.000'), "2'123.400"); - equals($.format.number(1231231212.32410, '#,##0.0000#'), "1'231'231'212.3241"); - equals($.format.number(1.2, "#,##0.#"), '1.2'); - - equals($.format.number("1'231'231'212.3241"), 1231231212.3241); - equals($.format.number("18.00'5"), 18.005); - +module("Boolean"); + +test("Basic requirements", function() { + equals($.format.bool(true), 'Yes'); + equals($.format.bool(false), 'No'); + equals($.format.bool('True'), 'Yes'); + equals($.format.bool('false'), 'No'); + equals($.format.bool('Yes'), 'Yes'); + equals($.format.bool('no'), 'No'); + equals($.format.bool('something'), 'Yes'); + equals($.format.bool(''), 'No'); + equals($.format.bool(null), 'No'); + equals($.format.bool(undefined), 'No'); + equals($.format.bool(0), 'No'); + equals($.format.bool(1), 'Yes'); +}); + +module("Dates"); + +test("Basic requirements", function() { + + var d = new Date(); + var f = 'MMMM dd, yyyy KK:mm:ss:SSS a'; + var df = $.format.date(d, f); + equals($.format.date(df, f).getTime(), d.getTime()); + + f = 'dd.MM.yyyy'; + df = $.format.date('1.5.2009', f); + d = new Date(df); + equals($.format.date(d, f), '01.05.2009'); + + d = new Date(); + d.setHours(0); + d.setMinutes(0); + f = 'h:mm a'; + equals($.format.date(d, f), '12:00 AM'); + + d = new Date(); + d.setHours(0); + d.setMinutes(0); + f = 'k:mm a'; + equals($.format.date(d, f), '24:00 AM'); + + d = new Date(); + d.setHours(12); + d.setMinutes(0); + f = 'k:mm a'; + equals($.format.date(d, f), '12:00 PM'); + + d = new Date(); + d.setFullYear(2012); + d.setMonth(1); + d.setDate(22); + d.setHours(0); + d.setMinutes(0); + d.setSeconds(0); + equals($.format.date('2012-02-22', 'yyyy-MM-dd').toString(), d.toString()); + + $.format.locale({ + date: { + format: 'EEEE, \'o\'\'clock\' dd\' de \'MMMM\' de \'yyyy H:mm:ss', + monthsFull: ['enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre'], + monthsShort: ['ene','feb','mar','abr','may','jun','jul','ago','sep','oct','nov','dic'], + daysFull: ['domingo','lunes','martes','mi�rcoles','jueves','viernes','s�bado'], + daysShort: ['dom','lun','mar','mi�','jue','vie','s�b'], + timeFormat: 'H:mm:ss', + shortDateFormat: 'dd/MM/yyyy', + longDateFormat: 'EEEE, dd\' de \'MMMM\' de \'yyyy' + } + }); + + d = new Date(); + d.setYear(1976); + d.setMonth(4); + d.setDate(31); + d.setHours(3); + d.setMinutes(20); + d.setSeconds(43); + d.setMilliseconds(0); + + equals($.format.date(d), 'lunes, o\'clock 31 de mayo de 1976 3:20:43'); + +}); + +module("Number"); + +test("Basic requirements", function() { + + equals($.format.number(0.123, '#0.0000'), '0.1230'); + equals($.format.number(7456.2, '#,##0.0#'), '7,456.2'); + equals($.format.number(7456.351, '#,##0.#'), '7,456.4'); + equals($.format.number(123.4, '#,##0.00#'), '123.40'); + equals($.format.number(12.32410, '#,##0.0000#'), '12.3241'); + equals($.format.number(0.123213, '#,##0.00###'), '0.12321'); + equals($.format.number(2101.234, '#,###'), '2,101'); + equals($.format.number(101.7, '#,###'), '102'); + equals($.format.number(0.123, '.0000'), '.1230'); + equals($.format.number(5.155, '.0#'), '.16'); + equals($.format.number(540.23, '###'), '540'); + equals($.format.number(540.23, '###.'), '540'); + equals($.format.number(540, '###.##'), '540'); + equals($.format.number(19.03), '19.03'); + equals($.format.number(5.0303, '00.0000'), '05.0303'); + equals($.format.number(5.0099, '#,##0.00'), '5.01'); + equals($.format.number(5.1009, '#,##0.00'), '5.10'); + equals($.format.number(5.1001, '#,##0.000'), '5.100'); + equals($.format.number(1, "#,##0.00"), '1.00'); + equals($.format.number(1.2, "#,##0.#"), '1.2'); + + equals($.format.number(23540.23, '#,###.##'), '23,540.23'); + equals($.format.number(23540.23, '#,###'), '23,540'); + equals($.format.number(23540.23, '#####.000'), '23540.230'); + equals($.format.number(3.14, '#00.#'), '03.1'); + equals($.format.number(10.9, '#,##0.0#'), '10.9'); + equals($.format.number(2.096, '##0.00'), '2.10'); + equals($.format.number(2.046, '##0.00'), '2.05'); + equals($.format.number(2.044, '##0.00'), '2.04'); + equals($.format.number(2.196, '##0.00'), '2.20'); + + equals($.format.number(1.02, "#.##"), '1.02'); + equals($.format.number(1.05, '##0.00'), '1.05'); + equals($.format.number(1.12, "#.##"), '1.12'); + equals($.format.number(1.999, "#.##"), '2'); + equals($.format.number(-2000.10, '#,###.00'), '-2,000.10'); + + equals($.format.number('.1230'), 0.123); + equals($.format.number('86.02'), 86.02); + equals($.format.number('03.14'), 3.14); + equals($.format.number('64.21'), 64.21); + + $.format.locale({ + number: { + groupingSeparator: '.', + decimalSeparator: ',' + } + }); + + equals($.format.number(123.4, '#,##0.000'), '123,400'); + equals($.format.number(12.32410, '#,##0.0000#'), '12,3241'); + equals($.format.number(2123.4, '#,##0.000'), '2.123,400'); + equals($.format.number(1231231212.32410, '#,##0.0000#'), '1.231.231.212,3241'); + equals($.format.number(-999, "#,###"), '-999'); + + equals($.format.number('1.231.231.212,3241'), 1231231212.3241); + equals($.format.number('18,00.5'), 18.005); + equals($.format.number('71,49'), 71.49); + + equals($.format.number(99.9999, '#,##0.0'), '100,0'); + equals($.format.number(99.9999, '#,##0.000'), '100,000'); + equals($.format.number(99.9999, '#,##0.0000'), '99,9999'); + + equals($.format.number(-99.9999, '#,##0.0'), '-100,0'); + equals($.format.number(-99.9999, '#,##0.000'), '-100,000'); + equals($.format.number(-99.9999, '#,##0.0000'), '-99,9999'); + + equals($.format.number(99.8999, '#,##0.00'), '99,90'); + equals($.format.number(99.9444, '#,##0.00'), '99,94'); + equals($.format.number(99.9454, '#,##0.00'), '99,95'); + equals($.format.number(99.9944, '#,##0.00'), '99,99'); + + $.format.locale({ + number: { + groupingSeparator: '\'', + decimalSeparator: '.' + } + }); + + equals($.format.number(123.4, '#,##0.000'), '123.400'); + equals($.format.number(12.32410, '#,##0.0000#'), '12.3241'); + equals($.format.number(2123.4, '#,##0.000'), "2'123.400"); + equals($.format.number(1231231212.32410, '#,##0.0000#'), "1'231'231'212.3241"); + equals($.format.number(1.2, "#,##0.#"), '1.2'); + + equals($.format.number("1'231'231'212.3241"), 1231231212.3241); + equals($.format.number("18.00'5"), 18.005); + }); \ No newline at end of file