testing/mochitest/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
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 if (typeof(dojo) != 'undefined') {
michael@0 12 dojo.provide('MochiKit.Format');
michael@0 13 }
michael@0 14
michael@0 15 if (typeof(MochiKit) == 'undefined') {
michael@0 16 MochiKit = {};
michael@0 17 }
michael@0 18
michael@0 19 if (typeof(MochiKit.Format) == 'undefined') {
michael@0 20 MochiKit.Format = {};
michael@0 21 }
michael@0 22
michael@0 23 MochiKit.Format.NAME = "MochiKit.Format";
michael@0 24 MochiKit.Format.VERSION = "1.4";
michael@0 25 MochiKit.Format.__repr__ = function () {
michael@0 26 return "[" + this.NAME + " " + this.VERSION + "]";
michael@0 27 };
michael@0 28 MochiKit.Format.toString = function () {
michael@0 29 return this.__repr__();
michael@0 30 };
michael@0 31
michael@0 32 MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) {
michael@0 33 return function (num) {
michael@0 34 num = parseFloat(num);
michael@0 35 if (typeof(num) == "undefined" || num === null || isNaN(num)) {
michael@0 36 return placeholder;
michael@0 37 }
michael@0 38 var curheader = header;
michael@0 39 var curfooter = footer;
michael@0 40 if (num < 0) {
michael@0 41 num = -num;
michael@0 42 } else {
michael@0 43 curheader = curheader.replace(/-/, "");
michael@0 44 }
michael@0 45 var me = arguments.callee;
michael@0 46 var fmt = MochiKit.Format.formatLocale(locale);
michael@0 47 if (isPercent) {
michael@0 48 num = num * 100.0;
michael@0 49 curfooter = fmt.percent + curfooter;
michael@0 50 }
michael@0 51 num = MochiKit.Format.roundToFixed(num, precision);
michael@0 52 var parts = num.split(/\./);
michael@0 53 var whole = parts[0];
michael@0 54 var frac = (parts.length == 1) ? "" : parts[1];
michael@0 55 var res = "";
michael@0 56 while (whole.length < leadingZeros) {
michael@0 57 whole = "0" + whole;
michael@0 58 }
michael@0 59 if (separatorAt) {
michael@0 60 while (whole.length > separatorAt) {
michael@0 61 var i = whole.length - separatorAt;
michael@0 62 //res = res + fmt.separator + whole.substring(i, whole.length);
michael@0 63 res = fmt.separator + whole.substring(i, whole.length) + res;
michael@0 64 whole = whole.substring(0, i);
michael@0 65 }
michael@0 66 }
michael@0 67 res = whole + res;
michael@0 68 if (precision > 0) {
michael@0 69 while (frac.length < trailingZeros) {
michael@0 70 frac = frac + "0";
michael@0 71 }
michael@0 72 res = res + fmt.decimal + frac;
michael@0 73 }
michael@0 74 return curheader + res + curfooter;
michael@0 75 };
michael@0 76 };
michael@0 77
michael@0 78 /** @id MochiKit.Format.numberFormatter */
michael@0 79 MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) {
michael@0 80 // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
michael@0 81 // | 0 | leading or trailing zeros
michael@0 82 // | # | just the number
michael@0 83 // | , | separator
michael@0 84 // | . | decimal separator
michael@0 85 // | % | Multiply by 100 and format as percent
michael@0 86 if (typeof(placeholder) == "undefined") {
michael@0 87 placeholder = "";
michael@0 88 }
michael@0 89 var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/);
michael@0 90 if (!match) {
michael@0 91 throw TypeError("Invalid pattern");
michael@0 92 }
michael@0 93 var header = pattern.substr(0, match.index);
michael@0 94 var footer = pattern.substr(match.index + match[0].length);
michael@0 95 if (header.search(/-/) == -1) {
michael@0 96 header = header + "-";
michael@0 97 }
michael@0 98 var whole = match[1];
michael@0 99 var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : "";
michael@0 100 var isPercent = (typeof(match[3]) == "string" && match[3] != "");
michael@0 101 var tmp = whole.split(/,/);
michael@0 102 var separatorAt;
michael@0 103 if (typeof(locale) == "undefined") {
michael@0 104 locale = "default";
michael@0 105 }
michael@0 106 if (tmp.length == 1) {
michael@0 107 separatorAt = null;
michael@0 108 } else {
michael@0 109 separatorAt = tmp[1].length;
michael@0 110 }
michael@0 111 var leadingZeros = whole.length - whole.replace(/0/g, "").length;
michael@0 112 var trailingZeros = frac.length - frac.replace(/0/g, "").length;
michael@0 113 var precision = frac.length;
michael@0 114 var rval = MochiKit.Format._numberFormatter(
michael@0 115 placeholder, header, footer, locale, isPercent, precision,
michael@0 116 leadingZeros, separatorAt, trailingZeros
michael@0 117 );
michael@0 118 var m = MochiKit.Base;
michael@0 119 if (m) {
michael@0 120 var fn = arguments.callee;
michael@0 121 var args = m.concat(arguments);
michael@0 122 rval.repr = function () {
michael@0 123 return [
michael@0 124 self.NAME,
michael@0 125 "(",
michael@0 126 map(m.repr, args).join(", "),
michael@0 127 ")"
michael@0 128 ].join("");
michael@0 129 };
michael@0 130 }
michael@0 131 return rval;
michael@0 132 };
michael@0 133
michael@0 134 /** @id MochiKit.Format.formatLocale */
michael@0 135 MochiKit.Format.formatLocale = function (locale) {
michael@0 136 if (typeof(locale) == "undefined" || locale === null) {
michael@0 137 locale = "default";
michael@0 138 }
michael@0 139 if (typeof(locale) == "string") {
michael@0 140 var rval = MochiKit.Format.LOCALE[locale];
michael@0 141 if (typeof(rval) == "string") {
michael@0 142 rval = arguments.callee(rval);
michael@0 143 MochiKit.Format.LOCALE[locale] = rval;
michael@0 144 }
michael@0 145 return rval;
michael@0 146 } else {
michael@0 147 return locale;
michael@0 148 }
michael@0 149 };
michael@0 150
michael@0 151 /** @id MochiKit.Format.twoDigitAverage */
michael@0 152 MochiKit.Format.twoDigitAverage = function (numerator, denominator) {
michael@0 153 if (denominator) {
michael@0 154 var res = numerator / denominator;
michael@0 155 if (!isNaN(res)) {
michael@0 156 return MochiKit.Format.twoDigitFloat(numerator / denominator);
michael@0 157 }
michael@0 158 }
michael@0 159 return "0";
michael@0 160 };
michael@0 161
michael@0 162 /** @id MochiKit.Format.twoDigitFloat */
michael@0 163 MochiKit.Format.twoDigitFloat = function (someFloat) {
michael@0 164 var sign = (someFloat < 0 ? '-' : '');
michael@0 165 var s = Math.floor(Math.abs(someFloat) * 100).toString();
michael@0 166 if (s == '0') {
michael@0 167 return s;
michael@0 168 }
michael@0 169 if (s.length < 3) {
michael@0 170 while (s.charAt(s.length - 1) == '0') {
michael@0 171 s = s.substring(0, s.length - 1);
michael@0 172 }
michael@0 173 return sign + '0.' + s;
michael@0 174 }
michael@0 175 var head = sign + s.substring(0, s.length - 2);
michael@0 176 var tail = s.substring(s.length - 2, s.length);
michael@0 177 if (tail == '00') {
michael@0 178 return head;
michael@0 179 } else if (tail.charAt(1) == '0') {
michael@0 180 return head + '.' + tail.charAt(0);
michael@0 181 } else {
michael@0 182 return head + '.' + tail;
michael@0 183 }
michael@0 184 };
michael@0 185
michael@0 186 /** @id MochiKit.Format.lstrip */
michael@0 187 MochiKit.Format.lstrip = function (str, /* optional */chars) {
michael@0 188 str = str + "";
michael@0 189 if (typeof(str) != "string") {
michael@0 190 return null;
michael@0 191 }
michael@0 192 if (!chars) {
michael@0 193 return str.replace(/^\s+/, "");
michael@0 194 } else {
michael@0 195 return str.replace(new RegExp("^[" + chars + "]+"), "");
michael@0 196 }
michael@0 197 };
michael@0 198
michael@0 199 /** @id MochiKit.Format.rstrip */
michael@0 200 MochiKit.Format.rstrip = function (str, /* optional */chars) {
michael@0 201 str = str + "";
michael@0 202 if (typeof(str) != "string") {
michael@0 203 return null;
michael@0 204 }
michael@0 205 if (!chars) {
michael@0 206 return str.replace(/\s+$/, "");
michael@0 207 } else {
michael@0 208 return str.replace(new RegExp("[" + chars + "]+$"), "");
michael@0 209 }
michael@0 210 };
michael@0 211
michael@0 212 /** @id MochiKit.Format.strip */
michael@0 213 MochiKit.Format.strip = function (str, /* optional */chars) {
michael@0 214 var self = MochiKit.Format;
michael@0 215 return self.rstrip(self.lstrip(str, chars), chars);
michael@0 216 };
michael@0 217
michael@0 218 /** @id MochiKit.Format.truncToFixed */
michael@0 219 MochiKit.Format.truncToFixed = function (aNumber, precision) {
michael@0 220 aNumber = Math.floor(aNumber * Math.pow(10, precision));
michael@0 221 var res = (aNumber * Math.pow(10, -precision)).toFixed(precision);
michael@0 222 if (res.charAt(0) == ".") {
michael@0 223 res = "0" + res;
michael@0 224 }
michael@0 225 return res;
michael@0 226 };
michael@0 227
michael@0 228 /** @id MochiKit.Format.roundToFixed */
michael@0 229 MochiKit.Format.roundToFixed = function (aNumber, precision) {
michael@0 230 return MochiKit.Format.truncToFixed(
michael@0 231 aNumber + 0.5 * Math.pow(10, -precision),
michael@0 232 precision
michael@0 233 );
michael@0 234 };
michael@0 235
michael@0 236 /** @id MochiKit.Format.percentFormat */
michael@0 237 MochiKit.Format.percentFormat = function (someFloat) {
michael@0 238 return MochiKit.Format.twoDigitFloat(100 * someFloat) + '%';
michael@0 239 };
michael@0 240
michael@0 241 MochiKit.Format.EXPORT = [
michael@0 242 "truncToFixed",
michael@0 243 "roundToFixed",
michael@0 244 "numberFormatter",
michael@0 245 "formatLocale",
michael@0 246 "twoDigitAverage",
michael@0 247 "twoDigitFloat",
michael@0 248 "percentFormat",
michael@0 249 "lstrip",
michael@0 250 "rstrip",
michael@0 251 "strip"
michael@0 252 ];
michael@0 253
michael@0 254 MochiKit.Format.LOCALE = {
michael@0 255 en_US: {separator: ",", decimal: ".", percent: "%"},
michael@0 256 de_DE: {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