michael@0: /*** michael@0: michael@0: MochiKit.Format 1.4.2 michael@0: michael@0: See for documentation, downloads, license, etc. michael@0: michael@0: (c) 2005 Bob Ippolito. All rights Reserved. michael@0: michael@0: ***/ michael@0: michael@0: MochiKit.Base._deps('Format', ['Base']); michael@0: michael@0: MochiKit.Format.NAME = "MochiKit.Format"; michael@0: MochiKit.Format.VERSION = "1.4.2"; michael@0: MochiKit.Format.__repr__ = function () { michael@0: return "[" + this.NAME + " " + this.VERSION + "]"; michael@0: }; michael@0: MochiKit.Format.toString = function () { michael@0: return this.__repr__(); michael@0: }; michael@0: michael@0: MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) { michael@0: return function (num) { michael@0: num = parseFloat(num); michael@0: if (typeof(num) == "undefined" || num === null || isNaN(num)) { michael@0: return placeholder; michael@0: } michael@0: var curheader = header; michael@0: var curfooter = footer; michael@0: if (num < 0) { michael@0: num = -num; michael@0: } else { michael@0: curheader = curheader.replace(/-/, ""); michael@0: } michael@0: var me = arguments.callee; michael@0: var fmt = MochiKit.Format.formatLocale(locale); michael@0: if (isPercent) { michael@0: num = num * 100.0; michael@0: curfooter = fmt.percent + curfooter; michael@0: } michael@0: num = MochiKit.Format.roundToFixed(num, precision); michael@0: var parts = num.split(/\./); michael@0: var whole = parts[0]; michael@0: var frac = (parts.length == 1) ? "" : parts[1]; michael@0: var res = ""; michael@0: while (whole.length < leadingZeros) { michael@0: whole = "0" + whole; michael@0: } michael@0: if (separatorAt) { michael@0: while (whole.length > separatorAt) { michael@0: var i = whole.length - separatorAt; michael@0: //res = res + fmt.separator + whole.substring(i, whole.length); michael@0: res = fmt.separator + whole.substring(i, whole.length) + res; michael@0: whole = whole.substring(0, i); michael@0: } michael@0: } michael@0: res = whole + res; michael@0: if (precision > 0) { michael@0: while (frac.length < trailingZeros) { michael@0: frac = frac + "0"; michael@0: } michael@0: res = res + fmt.decimal + frac; michael@0: } michael@0: return curheader + res + curfooter; michael@0: }; michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.numberFormatter */ michael@0: MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) { michael@0: // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html michael@0: // | 0 | leading or trailing zeros michael@0: // | # | just the number michael@0: // | , | separator michael@0: // | . | decimal separator michael@0: // | % | Multiply by 100 and format as percent michael@0: if (typeof(placeholder) == "undefined") { michael@0: placeholder = ""; michael@0: } michael@0: var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/); michael@0: if (!match) { michael@0: throw TypeError("Invalid pattern"); michael@0: } michael@0: var header = pattern.substr(0, match.index); michael@0: var footer = pattern.substr(match.index + match[0].length); michael@0: if (header.search(/-/) == -1) { michael@0: header = header + "-"; michael@0: } michael@0: var whole = match[1]; michael@0: var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : ""; michael@0: var isPercent = (typeof(match[3]) == "string" && match[3] != ""); michael@0: var tmp = whole.split(/,/); michael@0: var separatorAt; michael@0: if (typeof(locale) == "undefined") { michael@0: locale = "default"; michael@0: } michael@0: if (tmp.length == 1) { michael@0: separatorAt = null; michael@0: } else { michael@0: separatorAt = tmp[1].length; michael@0: } michael@0: var leadingZeros = whole.length - whole.replace(/0/g, "").length; michael@0: var trailingZeros = frac.length - frac.replace(/0/g, "").length; michael@0: var precision = frac.length; michael@0: var rval = MochiKit.Format._numberFormatter( michael@0: placeholder, header, footer, locale, isPercent, precision, michael@0: leadingZeros, separatorAt, trailingZeros michael@0: ); michael@0: var m = MochiKit.Base; michael@0: if (m) { michael@0: var fn = arguments.callee; michael@0: var args = m.concat(arguments); michael@0: rval.repr = function () { michael@0: return [ michael@0: self.NAME, michael@0: "(", michael@0: map(m.repr, args).join(", "), michael@0: ")" michael@0: ].join(""); michael@0: }; michael@0: } michael@0: return rval; michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.formatLocale */ michael@0: MochiKit.Format.formatLocale = function (locale) { michael@0: if (typeof(locale) == "undefined" || locale === null) { michael@0: locale = "default"; michael@0: } michael@0: if (typeof(locale) == "string") { michael@0: var rval = MochiKit.Format.LOCALE[locale]; michael@0: if (typeof(rval) == "string") { michael@0: rval = arguments.callee(rval); michael@0: MochiKit.Format.LOCALE[locale] = rval; michael@0: } michael@0: return rval; michael@0: } else { michael@0: return locale; michael@0: } michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.twoDigitAverage */ michael@0: MochiKit.Format.twoDigitAverage = function (numerator, denominator) { michael@0: if (denominator) { michael@0: var res = numerator / denominator; michael@0: if (!isNaN(res)) { michael@0: return MochiKit.Format.twoDigitFloat(res); michael@0: } michael@0: } michael@0: return "0"; michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.twoDigitFloat */ michael@0: MochiKit.Format.twoDigitFloat = function (aNumber) { michael@0: var res = roundToFixed(aNumber, 2); michael@0: if (res.indexOf(".00") > 0) { michael@0: return res.substring(0, res.length - 3); michael@0: } else if (res.charAt(res.length - 1) == "0") { michael@0: return res.substring(0, res.length - 1); michael@0: } else { michael@0: return res; michael@0: } michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.lstrip */ michael@0: MochiKit.Format.lstrip = function (str, /* optional */chars) { michael@0: str = str + ""; michael@0: if (typeof(str) != "string") { michael@0: return null; michael@0: } michael@0: if (!chars) { michael@0: return str.replace(/^\s+/, ""); michael@0: } else { michael@0: return str.replace(new RegExp("^[" + chars + "]+"), ""); michael@0: } michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.rstrip */ michael@0: MochiKit.Format.rstrip = function (str, /* optional */chars) { michael@0: str = str + ""; michael@0: if (typeof(str) != "string") { michael@0: return null; michael@0: } michael@0: if (!chars) { michael@0: return str.replace(/\s+$/, ""); michael@0: } else { michael@0: return str.replace(new RegExp("[" + chars + "]+$"), ""); michael@0: } michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.strip */ michael@0: MochiKit.Format.strip = function (str, /* optional */chars) { michael@0: var self = MochiKit.Format; michael@0: return self.rstrip(self.lstrip(str, chars), chars); michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.truncToFixed */ michael@0: MochiKit.Format.truncToFixed = function (aNumber, precision) { michael@0: var res = Math.floor(aNumber).toFixed(0); michael@0: if (aNumber < 0) { michael@0: res = Math.ceil(aNumber).toFixed(0); michael@0: if (res.charAt(0) != "-" && precision > 0) { michael@0: res = "-" + res; michael@0: } michael@0: } michael@0: if (res.indexOf("e") < 0 && precision > 0) { michael@0: var tail = aNumber.toString(); michael@0: if (tail.indexOf("e") > 0) { michael@0: tail = "."; michael@0: } else if (tail.indexOf(".") < 0) { michael@0: tail = "."; michael@0: } else { michael@0: tail = tail.substring(tail.indexOf(".")); michael@0: } michael@0: if (tail.length - 1 > precision) { michael@0: tail = tail.substring(0, precision + 1); michael@0: } michael@0: while (tail.length - 1 < precision) { michael@0: tail += "0"; michael@0: } michael@0: res += tail; michael@0: } michael@0: return res; michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.roundToFixed */ michael@0: MochiKit.Format.roundToFixed = function (aNumber, precision) { michael@0: var upper = Math.abs(aNumber) + 0.5 * Math.pow(10, -precision); michael@0: var res = MochiKit.Format.truncToFixed(upper, precision); michael@0: if (aNumber < 0) { michael@0: res = "-" + res; michael@0: } michael@0: return res; michael@0: }; michael@0: michael@0: /** @id MochiKit.Format.percentFormat */ michael@0: MochiKit.Format.percentFormat = function (aNumber) { michael@0: return MochiKit.Format.twoDigitFloat(100 * aNumber) + '%'; michael@0: }; michael@0: michael@0: MochiKit.Format.EXPORT = [ michael@0: "truncToFixed", michael@0: "roundToFixed", michael@0: "numberFormatter", michael@0: "formatLocale", michael@0: "twoDigitAverage", michael@0: "twoDigitFloat", michael@0: "percentFormat", michael@0: "lstrip", michael@0: "rstrip", michael@0: "strip" michael@0: ]; michael@0: michael@0: MochiKit.Format.LOCALE = { michael@0: en_US: {separator: ",", decimal: ".", percent: "%"}, michael@0: de_DE: {separator: ".", decimal: ",", percent: "%"}, michael@0: pt_BR: {separator: ".", decimal: ",", percent: "%"}, michael@0: fr_FR: {separator: " ", decimal: ",", percent: "%"}, michael@0: "default": "en_US" michael@0: }; michael@0: michael@0: MochiKit.Format.EXPORT_OK = []; michael@0: MochiKit.Format.EXPORT_TAGS = { michael@0: ':all': MochiKit.Format.EXPORT, michael@0: ':common': MochiKit.Format.EXPORT michael@0: }; michael@0: michael@0: MochiKit.Format.__new__ = function () { michael@0: // MochiKit.Base.nameFunctions(this); michael@0: var base = this.NAME + "."; michael@0: var k, v, o; michael@0: for (k in this.LOCALE) { michael@0: o = this.LOCALE[k]; michael@0: if (typeof(o) == "object") { michael@0: o.repr = function () { return this.NAME; }; michael@0: o.NAME = base + "LOCALE." + k; michael@0: } michael@0: } michael@0: for (k in this) { michael@0: o = this[k]; michael@0: if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { michael@0: try { michael@0: o.NAME = base + k; michael@0: } catch (e) { michael@0: // pass michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: MochiKit.Format.__new__(); michael@0: michael@0: if (typeof(MochiKit.Base) != "undefined") { michael@0: MochiKit.Base._exportSymbols(this, MochiKit.Format); michael@0: } else { michael@0: (function (globals, module) { michael@0: if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') michael@0: || (MochiKit.__export__ === false)) { michael@0: var all = module.EXPORT_TAGS[":all"]; michael@0: for (var i = 0; i < all.length; i++) { michael@0: globals[all[i]] = module[all[i]]; michael@0: } michael@0: } michael@0: })(this, MochiKit.Format); michael@0: }