testing/mochitest/tests/MochiKit-1.4.2/MochiKit/Format.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /***
michael@0 2
michael@0 3 MochiKit.Format 1.4.2
michael@0 4
michael@0 5 See <http://mochikit.com/> for documentation, downloads, license, etc.
michael@0 6
michael@0 7 (c) 2005 Bob Ippolito. All rights Reserved.
michael@0 8
michael@0 9 ***/
michael@0 10
michael@0 11 MochiKit.Base._deps('Format', ['Base']);
michael@0 12
michael@0 13 MochiKit.Format.NAME = "MochiKit.Format";
michael@0 14 MochiKit.Format.VERSION = "1.4.2";
michael@0 15 MochiKit.Format.__repr__ = function () {
michael@0 16 return "[" + this.NAME + " " + this.VERSION + "]";
michael@0 17 };
michael@0 18 MochiKit.Format.toString = function () {
michael@0 19 return this.__repr__();
michael@0 20 };
michael@0 21
michael@0 22 MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) {
michael@0 23 return function (num) {
michael@0 24 num = parseFloat(num);
michael@0 25 if (typeof(num) == "undefined" || num === null || isNaN(num)) {
michael@0 26 return placeholder;
michael@0 27 }
michael@0 28 var curheader = header;
michael@0 29 var curfooter = footer;
michael@0 30 if (num < 0) {
michael@0 31 num = -num;
michael@0 32 } else {
michael@0 33 curheader = curheader.replace(/-/, "");
michael@0 34 }
michael@0 35 var me = arguments.callee;
michael@0 36 var fmt = MochiKit.Format.formatLocale(locale);
michael@0 37 if (isPercent) {
michael@0 38 num = num * 100.0;
michael@0 39 curfooter = fmt.percent + curfooter;
michael@0 40 }
michael@0 41 num = MochiKit.Format.roundToFixed(num, precision);
michael@0 42 var parts = num.split(/\./);
michael@0 43 var whole = parts[0];
michael@0 44 var frac = (parts.length == 1) ? "" : parts[1];
michael@0 45 var res = "";
michael@0 46 while (whole.length < leadingZeros) {
michael@0 47 whole = "0" + whole;
michael@0 48 }
michael@0 49 if (separatorAt) {
michael@0 50 while (whole.length > separatorAt) {
michael@0 51 var i = whole.length - separatorAt;
michael@0 52 //res = res + fmt.separator + whole.substring(i, whole.length);
michael@0 53 res = fmt.separator + whole.substring(i, whole.length) + res;
michael@0 54 whole = whole.substring(0, i);
michael@0 55 }
michael@0 56 }
michael@0 57 res = whole + res;
michael@0 58 if (precision > 0) {
michael@0 59 while (frac.length < trailingZeros) {
michael@0 60 frac = frac + "0";
michael@0 61 }
michael@0 62 res = res + fmt.decimal + frac;
michael@0 63 }
michael@0 64 return curheader + res + curfooter;
michael@0 65 };
michael@0 66 };
michael@0 67
michael@0 68 /** @id MochiKit.Format.numberFormatter */
michael@0 69 MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) {
michael@0 70 // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
michael@0 71 // | 0 | leading or trailing zeros
michael@0 72 // | # | just the number
michael@0 73 // | , | separator
michael@0 74 // | . | decimal separator
michael@0 75 // | % | Multiply by 100 and format as percent
michael@0 76 if (typeof(placeholder) == "undefined") {
michael@0 77 placeholder = "";
michael@0 78 }
michael@0 79 var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/);
michael@0 80 if (!match) {
michael@0 81 throw TypeError("Invalid pattern");
michael@0 82 }
michael@0 83 var header = pattern.substr(0, match.index);
michael@0 84 var footer = pattern.substr(match.index + match[0].length);
michael@0 85 if (header.search(/-/) == -1) {
michael@0 86 header = header + "-";
michael@0 87 }
michael@0 88 var whole = match[1];
michael@0 89 var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : "";
michael@0 90 var isPercent = (typeof(match[3]) == "string" && match[3] != "");
michael@0 91 var tmp = whole.split(/,/);
michael@0 92 var separatorAt;
michael@0 93 if (typeof(locale) == "undefined") {
michael@0 94 locale = "default";
michael@0 95 }
michael@0 96 if (tmp.length == 1) {
michael@0 97 separatorAt = null;
michael@0 98 } else {
michael@0 99 separatorAt = tmp[1].length;
michael@0 100 }
michael@0 101 var leadingZeros = whole.length - whole.replace(/0/g, "").length;
michael@0 102 var trailingZeros = frac.length - frac.replace(/0/g, "").length;
michael@0 103 var precision = frac.length;
michael@0 104 var rval = MochiKit.Format._numberFormatter(
michael@0 105 placeholder, header, footer, locale, isPercent, precision,
michael@0 106 leadingZeros, separatorAt, trailingZeros
michael@0 107 );
michael@0 108 var m = MochiKit.Base;
michael@0 109 if (m) {
michael@0 110 var fn = arguments.callee;
michael@0 111 var args = m.concat(arguments);
michael@0 112 rval.repr = function () {
michael@0 113 return [
michael@0 114 self.NAME,
michael@0 115 "(",
michael@0 116 map(m.repr, args).join(", "),
michael@0 117 ")"
michael@0 118 ].join("");
michael@0 119 };
michael@0 120 }
michael@0 121 return rval;
michael@0 122 };
michael@0 123
michael@0 124 /** @id MochiKit.Format.formatLocale */
michael@0 125 MochiKit.Format.formatLocale = function (locale) {
michael@0 126 if (typeof(locale) == "undefined" || locale === null) {
michael@0 127 locale = "default";
michael@0 128 }
michael@0 129 if (typeof(locale) == "string") {
michael@0 130 var rval = MochiKit.Format.LOCALE[locale];
michael@0 131 if (typeof(rval) == "string") {
michael@0 132 rval = arguments.callee(rval);
michael@0 133 MochiKit.Format.LOCALE[locale] = rval;
michael@0 134 }
michael@0 135 return rval;
michael@0 136 } else {
michael@0 137 return locale;
michael@0 138 }
michael@0 139 };
michael@0 140
michael@0 141 /** @id MochiKit.Format.twoDigitAverage */
michael@0 142 MochiKit.Format.twoDigitAverage = function (numerator, denominator) {
michael@0 143 if (denominator) {
michael@0 144 var res = numerator / denominator;
michael@0 145 if (!isNaN(res)) {
michael@0 146 return MochiKit.Format.twoDigitFloat(res);
michael@0 147 }
michael@0 148 }
michael@0 149 return "0";
michael@0 150 };
michael@0 151
michael@0 152 /** @id MochiKit.Format.twoDigitFloat */
michael@0 153 MochiKit.Format.twoDigitFloat = function (aNumber) {
michael@0 154 var res = roundToFixed(aNumber, 2);
michael@0 155 if (res.indexOf(".00") > 0) {
michael@0 156 return res.substring(0, res.length - 3);
michael@0 157 } else if (res.charAt(res.length - 1) == "0") {
michael@0 158 return res.substring(0, res.length - 1);
michael@0 159 } else {
michael@0 160 return res;
michael@0 161 }
michael@0 162 };
michael@0 163
michael@0 164 /** @id MochiKit.Format.lstrip */
michael@0 165 MochiKit.Format.lstrip = function (str, /* optional */chars) {
michael@0 166 str = str + "";
michael@0 167 if (typeof(str) != "string") {
michael@0 168 return null;
michael@0 169 }
michael@0 170 if (!chars) {
michael@0 171 return str.replace(/^\s+/, "");
michael@0 172 } else {
michael@0 173 return str.replace(new RegExp("^[" + chars + "]+"), "");
michael@0 174 }
michael@0 175 };
michael@0 176
michael@0 177 /** @id MochiKit.Format.rstrip */
michael@0 178 MochiKit.Format.rstrip = function (str, /* optional */chars) {
michael@0 179 str = str + "";
michael@0 180 if (typeof(str) != "string") {
michael@0 181 return null;
michael@0 182 }
michael@0 183 if (!chars) {
michael@0 184 return str.replace(/\s+$/, "");
michael@0 185 } else {
michael@0 186 return str.replace(new RegExp("[" + chars + "]+$"), "");
michael@0 187 }
michael@0 188 };
michael@0 189
michael@0 190 /** @id MochiKit.Format.strip */
michael@0 191 MochiKit.Format.strip = function (str, /* optional */chars) {
michael@0 192 var self = MochiKit.Format;
michael@0 193 return self.rstrip(self.lstrip(str, chars), chars);
michael@0 194 };
michael@0 195
michael@0 196 /** @id MochiKit.Format.truncToFixed */
michael@0 197 MochiKit.Format.truncToFixed = function (aNumber, precision) {
michael@0 198 var res = Math.floor(aNumber).toFixed(0);
michael@0 199 if (aNumber < 0) {
michael@0 200 res = Math.ceil(aNumber).toFixed(0);
michael@0 201 if (res.charAt(0) != "-" && precision > 0) {
michael@0 202 res = "-" + res;
michael@0 203 }
michael@0 204 }
michael@0 205 if (res.indexOf("e") < 0 && precision > 0) {
michael@0 206 var tail = aNumber.toString();
michael@0 207 if (tail.indexOf("e") > 0) {
michael@0 208 tail = ".";
michael@0 209 } else if (tail.indexOf(".") < 0) {
michael@0 210 tail = ".";
michael@0 211 } else {
michael@0 212 tail = tail.substring(tail.indexOf("."));
michael@0 213 }
michael@0 214 if (tail.length - 1 > precision) {
michael@0 215 tail = tail.substring(0, precision + 1);
michael@0 216 }
michael@0 217 while (tail.length - 1 < precision) {
michael@0 218 tail += "0";
michael@0 219 }
michael@0 220 res += tail;
michael@0 221 }
michael@0 222 return res;
michael@0 223 };
michael@0 224
michael@0 225 /** @id MochiKit.Format.roundToFixed */
michael@0 226 MochiKit.Format.roundToFixed = function (aNumber, precision) {
michael@0 227 var upper = Math.abs(aNumber) + 0.5 * Math.pow(10, -precision);
michael@0 228 var res = MochiKit.Format.truncToFixed(upper, precision);
michael@0 229 if (aNumber < 0) {
michael@0 230 res = "-" + res;
michael@0 231 }
michael@0 232 return res;
michael@0 233 };
michael@0 234
michael@0 235 /** @id MochiKit.Format.percentFormat */
michael@0 236 MochiKit.Format.percentFormat = function (aNumber) {
michael@0 237 return MochiKit.Format.twoDigitFloat(100 * aNumber) + '%';
michael@0 238 };
michael@0 239
michael@0 240 MochiKit.Format.EXPORT = [
michael@0 241 "truncToFixed",
michael@0 242 "roundToFixed",
michael@0 243 "numberFormatter",
michael@0 244 "formatLocale",
michael@0 245 "twoDigitAverage",
michael@0 246 "twoDigitFloat",
michael@0 247 "percentFormat",
michael@0 248 "lstrip",
michael@0 249 "rstrip",
michael@0 250 "strip"
michael@0 251 ];
michael@0 252
michael@0 253 MochiKit.Format.LOCALE = {
michael@0 254 en_US: {separator: ",", decimal: ".", percent: "%"},
michael@0 255 de_DE: {separator: ".", decimal: ",", percent: "%"},
michael@0 256 pt_BR: {separator: ".", decimal: ",", percent: "%"},
michael@0 257 fr_FR: {separator: " ", decimal: ",", percent: "%"},
michael@0 258 "default": "en_US"
michael@0 259 };
michael@0 260
michael@0 261 MochiKit.Format.EXPORT_OK = [];
michael@0 262 MochiKit.Format.EXPORT_TAGS = {
michael@0 263 ':all': MochiKit.Format.EXPORT,
michael@0 264 ':common': MochiKit.Format.EXPORT
michael@0 265 };
michael@0 266
michael@0 267 MochiKit.Format.__new__ = function () {
michael@0 268 // MochiKit.Base.nameFunctions(this);
michael@0 269 var base = this.NAME + ".";
michael@0 270 var k, v, o;
michael@0 271 for (k in this.LOCALE) {
michael@0 272 o = this.LOCALE[k];
michael@0 273 if (typeof(o) == "object") {
michael@0 274 o.repr = function () { return this.NAME; };
michael@0 275 o.NAME = base + "LOCALE." + k;
michael@0 276 }
michael@0 277 }
michael@0 278 for (k in this) {
michael@0 279 o = this[k];
michael@0 280 if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') {
michael@0 281 try {
michael@0 282 o.NAME = base + k;
michael@0 283 } catch (e) {
michael@0 284 // pass
michael@0 285 }
michael@0 286 }
michael@0 287 }
michael@0 288 };
michael@0 289
michael@0 290 MochiKit.Format.__new__();
michael@0 291
michael@0 292 if (typeof(MochiKit.Base) != "undefined") {
michael@0 293 MochiKit.Base._exportSymbols(this, MochiKit.Format);
michael@0 294 } else {
michael@0 295 (function (globals, module) {
michael@0 296 if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined')
michael@0 297 || (MochiKit.__export__ === false)) {
michael@0 298 var all = module.EXPORT_TAGS[":all"];
michael@0 299 for (var i = 0; i < all.length; i++) {
michael@0 300 globals[all[i]] = module[all[i]];
michael@0 301 }
michael@0 302 }
michael@0 303 })(this, MochiKit.Format);
michael@0 304 }

mercurial