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.

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

mercurial