From 192ec92781090b98eeb69d2031616cd1c791cdf1 Mon Sep 17 00:00:00 2001 From: Ilya Verbitskiy Date: Mon, 19 Nov 2012 21:11:42 +0100 Subject: [PATCH 1/5] Added simple formatting for boolean values. --- src/jquery.format.js | 1063 +++++++++++++++++---------------- test/jquery.format-1.3.min.js | 35 +- test/test.js | 325 +++++----- 3 files changed, 716 insertions(+), 707 deletions(-) diff --git a/src/jquery.format.js b/src/jquery.format.js index 4c99a3a..d55e7e7 100644 --- a/src/jquery.format.js +++ b/src/jquery.format.js @@ -1,526 +1,539 @@ -/* - * 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 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: '.' + }, + bool: { + trueValue: 'Yes', + falseValue: 'No' + } + }; + + 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; + }, + + bool: function(value) { + 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/jquery.format-1.3.min.js b/test/jquery.format-1.3.min.js index f1f33e0..5f906e5 100644 --- a/test/jquery.format-1.3.min.js +++ b/test/jquery.format-1.3.min.js @@ -1,23 +1,12 @@ -/* - * jQuery Format Plugin v1.3 - * 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: 2012-09-28 00:16:07 +0300 (Fri, 28 Sep 2012) - */ -(function(z){z.format=function(){var o={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(b){a={a:6};if(b)for(var i in b)for(var g in b[i])o[i][g]=b[i][g];return o},date:function(b,i){var g=0,h=0,c=0;c=h="";var l,e;if(typeof b=="string"){var m=function(r,v,y,w){for(w=w;w>=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=d;e--){var f=b.substring(c,c+e);if(f.length>=d&&/^\d+$/.test(f))return f}return null};"undefined"==typeof k&&(k=n.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=n.date.format);e=c.getYear();1E3>e&&(e=String(e+1900));q=c.getMonth()+1; d=c.getDate();j=c.getDay();f=c.getHours();p=c.getMinutes();m=c.getSeconds();i=c.getMilliseconds();c={y:e,yyyy:e,yy:String(e).substring(2,4),M:q,MM:g(q),MMM:n.date.monthsShort[q-1],MMMM:n.date.monthsFull[q-1],d:d,dd:g(d),EEE:n.date.daysShort[j],EEEE:n.date.daysFull[j],H:f,HH:g(f)};c.h=0===f?12:12k.length)k=n.number.format;h=k.lastIndexOf(",");m=k.indexOf(".");b=g="";var q=0>c,d=k.substr(m+1).replace(/#/g,"").length,l=k.substr(m+1).length,j=10,c=Math.abs(c);if(-1!=m){b=n.number.decimalSeparator;if(0f?Math.round(parseInt(c*j*e-parseInt(c,10)*j*e,10)/e):f),p=c.toString().split(".");if("undefined"!=typeof p[1])for(e=0;e=d&&"0"==l&&/^0*$/.test(f.substr(e+1)))break;p+=l}b+=p}b==n.number.decimalSeparator&&(b="")}if(0!==m){g=""!=b?String(parseInt(Math.round(c*j)/j,10)):String(Math.round(c));d=n.number.groupingSeparator;j=0;-1!=h&&(j=-1!=m?m-h:k.length-h,j--);if(0 Date: Tue, 20 Nov 2012 21:22:43 +0100 Subject: [PATCH 2/5] Boolean format supports string values. --- README.md | 113 +++++++++++++++++----------------- src/jquery.format.js | 12 ++++ test/jquery.format-1.3.min.js | 6 +- test/test.js | 10 +++ 4 files changed, 82 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 455b1ba..e54b11d 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,58 @@ -# 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'); + +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 + Initial release. \ No newline at end of file diff --git a/src/jquery.format.js b/src/jquery.format.js index d55e7e7..8468524 100644 --- a/src/jquery.format.js +++ b/src/jquery.format.js @@ -33,6 +33,8 @@ decimalSeparator: '.' }, bool: { + trueString: ['true', 'yes'], + falseString: ['false', 'no'], trueValue: 'Yes', falseValue: 'No' } @@ -53,6 +55,16 @@ }, bool: function(value) { + if (typeof value === 'string') { + value = value.toLowerCase(); + if (_locale.bool.trueString.indexOf(value) > -1) { + value = true; + } + else if (_locale.bool.falseString.indexOf(value) > -1) { + value = false; + } + } + if (value) { return _locale.bool.trueValue; } diff --git a/test/jquery.format-1.3.min.js b/test/jquery.format-1.3.min.js index 5f906e5..48243f3 100644 --- a/test/jquery.format-1.3.min.js +++ b/test/jquery.format-1.3.min.js @@ -1,5 +1,5 @@ /* - * jQuery Format Plugin v1.3 + * jQuery Format Plugin v${version} * http://www.asual.com/jquery/format/ * * Copyright (c) 2009-2011 Rostislav Hristov @@ -7,6 +7,6 @@ * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license * - * Date: 2012-09-28 00:16:07 +0300 (Fri, 28 Sep 2012) + * Date: ${timestamp} */ -(function(v){var n={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:{trueValue:"Yes",falseValue:"No"}};v.format={locale:function(c){a={a:6};if(c)for(var k in c)for(var g in c[k])n[k][g]=c[k][g];return n},bool:function(c){return c?n.bool.trueValue:n.bool.falseValue},date:function(c,k){var g=0,h=0,b=0,b=h="",m,e;if("string"==typeof c){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=n.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=n.date.format);e=c.getYear();1E3>e&&(e=String(e+1900));q=c.getMonth()+1; d=c.getDate();j=c.getDay();f=c.getHours();p=c.getMinutes();m=c.getSeconds();i=c.getMilliseconds();c={y:e,yyyy:e,yy:String(e).substring(2,4),M:q,MM:g(q),MMM:n.date.monthsShort[q-1],MMMM:n.date.monthsFull[q-1],d:d,dd:g(d),EEE:n.date.daysShort[j],EEEE:n.date.daysFull[j],H:f,HH:g(f)};c.h=0===f?12:12k.length)k=n.number.format;h=k.lastIndexOf(",");m=k.indexOf(".");b=g="";var q=0>c,d=k.substr(m+1).replace(/#/g,"").length,l=k.substr(m+1).length,j=10,c=Math.abs(c);if(-1!=m){b=n.number.decimalSeparator;if(0f?Math.round(parseInt(c*j*e-parseInt(c,10)*j*e,10)/e):f),p=c.toString().split(".");if("undefined"!=typeof p[1])for(e=0;e=d&&"0"==l&&/^0*$/.test(f.substr(e+1)))break;p+=l}b+=p}b==n.number.decimalSeparator&&(b="")}if(0!==m){g=""!=b?String(parseInt(Math.round(c*j)/j,10)):String(Math.round(c));d=n.number.groupingSeparator;j=0;-1!=h&&(j=-1!=m?m-h:k.length-h,j--);if(0=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 Date: Tue, 20 Nov 2012 21:28:24 +0100 Subject: [PATCH 3/5] Updated readme --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e54b11d..012cad5 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Parsing is very similar to the formatting but works with strings as a first para $.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: @@ -36,10 +37,19 @@ of the en_US locale. The locale can be globally configured using the following: 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 From 6bda70722b37f8183e5b2334e6c93b40df9c4aae Mon Sep 17 00:00:00 2001 From: Ilya Verbitskiy Date: Tue, 20 Nov 2012 21:43:05 +0100 Subject: [PATCH 4/5] Fixed IE6 compatibility issue --- src/jquery.format.js | 17 +++++++++++++++-- test/jquery.format-1.3.min.js | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/jquery.format.js b/src/jquery.format.js index 8468524..67b9d99 100644 --- a/src/jquery.format.js +++ b/src/jquery.format.js @@ -13,6 +13,19 @@ $.format = (function () { + // The function is used instead of Array.prototype.indexOf to make the code + // compatible with IE 6/7 + var contains = function(array, value) { + var i; + for (i in array) { + if (array[i] === value) { + return true; + } + } + + return false; + }; + var UNDEFINED = 'undefined', TRUE = true, FALSE = false, @@ -57,10 +70,10 @@ bool: function(value) { if (typeof value === 'string') { value = value.toLowerCase(); - if (_locale.bool.trueString.indexOf(value) > -1) { + if (contains(_locale.bool.trueString, value)) { value = true; } - else if (_locale.bool.falseString.indexOf(value) > -1) { + else if (contains(_locale.bool.falseString, value)) { value = false; } } diff --git a/test/jquery.format-1.3.min.js b/test/jquery.format-1.3.min.js index 48243f3..b78c20a 100644 --- a/test/jquery.format-1.3.min.js +++ b/test/jquery.format-1.3.min.js @@ -9,4 +9,4 @@ * * 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(),-1=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=d;e--){var f=b.substring(c,c+e);if(f.length>=d&&/^\d+$/.test(f))return f}return null};"undefined"==typeof j&&(j=m.date.format);for(var d=0,l=new Date(0,0,0,0,0,0,0),k=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 j&&(j=m.date.format);e=b.getYear();1E3>e&&(e=String(e+1900));q=b.getMonth()+1;d=b.getDate();k=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[k],EEEE:m.date.daysFull[k],H:f,HH:g(f)};b.h=0===f?12: 12j.length)j=m.number.format;h=j.lastIndexOf(",");n=j.indexOf(".");c=g="";var q=0>b,d=j.substr(n+1).replace(/#/g,"").length,l=j.substr(n+1).length,k=10,b=Math.abs(b);if(-1!=n){c=m.number.decimalSeparator;if(0f?Math.round(parseInt(b*k*e-parseInt(b,10)*k*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*k)/k,10)):String(Math.round(b));d=m.number.groupingSeparator; k=0;-1!=h&&(k=-1!=n?n-h:j.length-h,k--);if(0 Date: Tue, 20 Nov 2012 23:57:56 +0100 Subject: [PATCH 5/5] Code cleanup --- src/jquery.format.js | 554 +--------------------------------- test/jquery.format-1.3.min.js | 539 ++++++++++++++++++++++++++++++++- 2 files changed, 539 insertions(+), 554 deletions(-) diff --git a/src/jquery.format.js b/src/jquery.format.js index 67b9d99..3c01342 100644 --- a/src/jquery.format.js +++ b/src/jquery.format.js @@ -9,556 +9,4 @@ * * Date: ${timestamp} */ -(function ($) { - - $.format = (function () { - - // The function is used instead of Array.prototype.indexOf to make the code - // compatible with IE 6/7 - var contains = function(array, value) { - var i; - for (i in array) { - if (array[i] === value) { - return true; - } - } - - return false; - }; - - 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: '.' - }, - bool: { - trueString: ['true', 'yes'], - falseString: ['false', 'no'], - trueValue: 'Yes', - falseValue: 'No' - } - }; - - 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; - }, - - bool: function(value) { - if (typeof value === 'string') { - value = value.toLowerCase(); - if (contains(_locale.bool.trueString, value)) { - value = true; - } - else if (contains(_locale.bool.falseString, value)) { - 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 +(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=d;e--){var f=b.substring(c,c+e);if(f.length>=d&&/^\d+$/.test(f))return f}return null};"undefined"==typeof j&&(j=m.date.format);for(var d=0,l=new Date(0,0,0,0,0,0,0),k=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 j&&(j=m.date.format);e=b.getYear();1E3>e&&(e=String(e+1900));q=b.getMonth()+1;d=b.getDate();k=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[k],EEEE:m.date.daysFull[k],H:f,HH:g(f)};b.h=0===f?12: 12j.length)j=m.number.format;h=j.lastIndexOf(",");n=j.indexOf(".");c=g="";var q=0>b,d=j.substr(n+1).replace(/#/g,"").length,l=j.substr(n+1).length,k=10,b=Math.abs(b);if(-1!=n){c=m.number.decimalSeparator;if(0f?Math.round(parseInt(b*k*e-parseInt(b,10)*k*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*k)/k,10)):String(Math.round(b));d=m.number.groupingSeparator; k=0;-1!=h&&(k=-1!=n?n-h:j.length-h,k--);if(0 -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