5 See <http://mochikit.com/> for documentation, downloads, license, etc.
7 (c) 2005 Bob Ippolito. All rights Reserved.
11 if (typeof(dojo) != 'undefined') {
12 dojo.provide('MochiKit.Format');
15 if (typeof(MochiKit) == 'undefined') {
19 if (typeof(MochiKit.Format) == 'undefined') {
23 MochiKit.Format.NAME = "MochiKit.Format";
24 MochiKit.Format.VERSION = "1.4";
25 MochiKit.Format.__repr__ = function () {
26 return "[" + this.NAME + " " + this.VERSION + "]";
28 MochiKit.Format.toString = function () {
29 return this.__repr__();
32 MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) {
33 return function (num) {
34 num = parseFloat(num);
35 if (typeof(num) == "undefined" || num === null || isNaN(num)) {
38 var curheader = header;
39 var curfooter = footer;
43 curheader = curheader.replace(/-/, "");
45 var me = arguments.callee;
46 var fmt = MochiKit.Format.formatLocale(locale);
49 curfooter = fmt.percent + curfooter;
51 num = MochiKit.Format.roundToFixed(num, precision);
52 var parts = num.split(/\./);
54 var frac = (parts.length == 1) ? "" : parts[1];
56 while (whole.length < leadingZeros) {
60 while (whole.length > separatorAt) {
61 var i = whole.length - separatorAt;
62 //res = res + fmt.separator + whole.substring(i, whole.length);
63 res = fmt.separator + whole.substring(i, whole.length) + res;
64 whole = whole.substring(0, i);
69 while (frac.length < trailingZeros) {
72 res = res + fmt.decimal + frac;
74 return curheader + res + curfooter;
78 /** @id MochiKit.Format.numberFormatter */
79 MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) {
80 // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
81 // | 0 | leading or trailing zeros
82 // | # | just the number
84 // | . | decimal separator
85 // | % | Multiply by 100 and format as percent
86 if (typeof(placeholder) == "undefined") {
89 var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/);
91 throw TypeError("Invalid pattern");
93 var header = pattern.substr(0, match.index);
94 var footer = pattern.substr(match.index + match[0].length);
95 if (header.search(/-/) == -1) {
96 header = header + "-";
99 var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : "";
100 var isPercent = (typeof(match[3]) == "string" && match[3] != "");
101 var tmp = whole.split(/,/);
103 if (typeof(locale) == "undefined") {
106 if (tmp.length == 1) {
109 separatorAt = tmp[1].length;
111 var leadingZeros = whole.length - whole.replace(/0/g, "").length;
112 var trailingZeros = frac.length - frac.replace(/0/g, "").length;
113 var precision = frac.length;
114 var rval = MochiKit.Format._numberFormatter(
115 placeholder, header, footer, locale, isPercent, precision,
116 leadingZeros, separatorAt, trailingZeros
118 var m = MochiKit.Base;
120 var fn = arguments.callee;
121 var args = m.concat(arguments);
122 rval.repr = function () {
126 map(m.repr, args).join(", "),
134 /** @id MochiKit.Format.formatLocale */
135 MochiKit.Format.formatLocale = function (locale) {
136 if (typeof(locale) == "undefined" || locale === null) {
139 if (typeof(locale) == "string") {
140 var rval = MochiKit.Format.LOCALE[locale];
141 if (typeof(rval) == "string") {
142 rval = arguments.callee(rval);
143 MochiKit.Format.LOCALE[locale] = rval;
151 /** @id MochiKit.Format.twoDigitAverage */
152 MochiKit.Format.twoDigitAverage = function (numerator, denominator) {
154 var res = numerator / denominator;
156 return MochiKit.Format.twoDigitFloat(numerator / denominator);
162 /** @id MochiKit.Format.twoDigitFloat */
163 MochiKit.Format.twoDigitFloat = function (someFloat) {
164 var sign = (someFloat < 0 ? '-' : '');
165 var s = Math.floor(Math.abs(someFloat) * 100).toString();
170 while (s.charAt(s.length - 1) == '0') {
171 s = s.substring(0, s.length - 1);
173 return sign + '0.' + s;
175 var head = sign + s.substring(0, s.length - 2);
176 var tail = s.substring(s.length - 2, s.length);
179 } else if (tail.charAt(1) == '0') {
180 return head + '.' + tail.charAt(0);
182 return head + '.' + tail;
186 /** @id MochiKit.Format.lstrip */
187 MochiKit.Format.lstrip = function (str, /* optional */chars) {
189 if (typeof(str) != "string") {
193 return str.replace(/^\s+/, "");
195 return str.replace(new RegExp("^[" + chars + "]+"), "");
199 /** @id MochiKit.Format.rstrip */
200 MochiKit.Format.rstrip = function (str, /* optional */chars) {
202 if (typeof(str) != "string") {
206 return str.replace(/\s+$/, "");
208 return str.replace(new RegExp("[" + chars + "]+$"), "");
212 /** @id MochiKit.Format.strip */
213 MochiKit.Format.strip = function (str, /* optional */chars) {
214 var self = MochiKit.Format;
215 return self.rstrip(self.lstrip(str, chars), chars);
218 /** @id MochiKit.Format.truncToFixed */
219 MochiKit.Format.truncToFixed = function (aNumber, precision) {
220 aNumber = Math.floor(aNumber * Math.pow(10, precision));
221 var res = (aNumber * Math.pow(10, -precision)).toFixed(precision);
222 if (res.charAt(0) == ".") {
228 /** @id MochiKit.Format.roundToFixed */
229 MochiKit.Format.roundToFixed = function (aNumber, precision) {
230 return MochiKit.Format.truncToFixed(
231 aNumber + 0.5 * Math.pow(10, -precision),
236 /** @id MochiKit.Format.percentFormat */
237 MochiKit.Format.percentFormat = function (someFloat) {
238 return MochiKit.Format.twoDigitFloat(100 * someFloat) + '%';
241 MochiKit.Format.EXPORT = [
254 MochiKit.Format.LOCALE = {
255 en_US: {separator: ",", decimal: ".", percent: "%"},
256 de_DE: {separator: ".", decimal: ",", percent: "%"},
257 fr_FR: {separator: " ", decimal: ",", percent: "%"},
261 MochiKit.Format.EXPORT_OK = [];
262 MochiKit.Format.EXPORT_TAGS = {
263 ':all': MochiKit.Format.EXPORT,
264 ':common': MochiKit.Format.EXPORT
267 MochiKit.Format.__new__ = function () {
268 // MochiKit.Base.nameFunctions(this);
269 var base = this.NAME + ".";
271 for (k in this.LOCALE) {
273 if (typeof(o) == "object") {
274 o.repr = function () { return this.NAME; };
275 o.NAME = base + "LOCALE." + k;
280 if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
290 MochiKit.Format.__new__();
292 if (typeof(MochiKit.Base) != "undefined") {
293 MochiKit.Base._exportSymbols(this, MochiKit.Format);
295 (function (globals, module) {
296 if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
297 || (MochiKit.__export__ === false)) {
298 var all = module.EXPORT_TAGS[":all"];
299 for (var i = 0; i < all.length; i++) {
300 globals[all[i]] = module[all[i]];
303 })(this, MochiKit.Format);