1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mochitest/tests/MochiKit-1.4.2/MochiKit/Format.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,304 @@ 1.4 +/*** 1.5 + 1.6 +MochiKit.Format 1.4.2 1.7 + 1.8 +See <http://mochikit.com/> for documentation, downloads, license, etc. 1.9 + 1.10 +(c) 2005 Bob Ippolito. All rights Reserved. 1.11 + 1.12 +***/ 1.13 + 1.14 +MochiKit.Base._deps('Format', ['Base']); 1.15 + 1.16 +MochiKit.Format.NAME = "MochiKit.Format"; 1.17 +MochiKit.Format.VERSION = "1.4.2"; 1.18 +MochiKit.Format.__repr__ = function () { 1.19 + return "[" + this.NAME + " " + this.VERSION + "]"; 1.20 +}; 1.21 +MochiKit.Format.toString = function () { 1.22 + return this.__repr__(); 1.23 +}; 1.24 + 1.25 +MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) { 1.26 + return function (num) { 1.27 + num = parseFloat(num); 1.28 + if (typeof(num) == "undefined" || num === null || isNaN(num)) { 1.29 + return placeholder; 1.30 + } 1.31 + var curheader = header; 1.32 + var curfooter = footer; 1.33 + if (num < 0) { 1.34 + num = -num; 1.35 + } else { 1.36 + curheader = curheader.replace(/-/, ""); 1.37 + } 1.38 + var me = arguments.callee; 1.39 + var fmt = MochiKit.Format.formatLocale(locale); 1.40 + if (isPercent) { 1.41 + num = num * 100.0; 1.42 + curfooter = fmt.percent + curfooter; 1.43 + } 1.44 + num = MochiKit.Format.roundToFixed(num, precision); 1.45 + var parts = num.split(/\./); 1.46 + var whole = parts[0]; 1.47 + var frac = (parts.length == 1) ? "" : parts[1]; 1.48 + var res = ""; 1.49 + while (whole.length < leadingZeros) { 1.50 + whole = "0" + whole; 1.51 + } 1.52 + if (separatorAt) { 1.53 + while (whole.length > separatorAt) { 1.54 + var i = whole.length - separatorAt; 1.55 + //res = res + fmt.separator + whole.substring(i, whole.length); 1.56 + res = fmt.separator + whole.substring(i, whole.length) + res; 1.57 + whole = whole.substring(0, i); 1.58 + } 1.59 + } 1.60 + res = whole + res; 1.61 + if (precision > 0) { 1.62 + while (frac.length < trailingZeros) { 1.63 + frac = frac + "0"; 1.64 + } 1.65 + res = res + fmt.decimal + frac; 1.66 + } 1.67 + return curheader + res + curfooter; 1.68 + }; 1.69 +}; 1.70 + 1.71 +/** @id MochiKit.Format.numberFormatter */ 1.72 +MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) { 1.73 + // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html 1.74 + // | 0 | leading or trailing zeros 1.75 + // | # | just the number 1.76 + // | , | separator 1.77 + // | . | decimal separator 1.78 + // | % | Multiply by 100 and format as percent 1.79 + if (typeof(placeholder) == "undefined") { 1.80 + placeholder = ""; 1.81 + } 1.82 + var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/); 1.83 + if (!match) { 1.84 + throw TypeError("Invalid pattern"); 1.85 + } 1.86 + var header = pattern.substr(0, match.index); 1.87 + var footer = pattern.substr(match.index + match[0].length); 1.88 + if (header.search(/-/) == -1) { 1.89 + header = header + "-"; 1.90 + } 1.91 + var whole = match[1]; 1.92 + var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : ""; 1.93 + var isPercent = (typeof(match[3]) == "string" && match[3] != ""); 1.94 + var tmp = whole.split(/,/); 1.95 + var separatorAt; 1.96 + if (typeof(locale) == "undefined") { 1.97 + locale = "default"; 1.98 + } 1.99 + if (tmp.length == 1) { 1.100 + separatorAt = null; 1.101 + } else { 1.102 + separatorAt = tmp[1].length; 1.103 + } 1.104 + var leadingZeros = whole.length - whole.replace(/0/g, "").length; 1.105 + var trailingZeros = frac.length - frac.replace(/0/g, "").length; 1.106 + var precision = frac.length; 1.107 + var rval = MochiKit.Format._numberFormatter( 1.108 + placeholder, header, footer, locale, isPercent, precision, 1.109 + leadingZeros, separatorAt, trailingZeros 1.110 + ); 1.111 + var m = MochiKit.Base; 1.112 + if (m) { 1.113 + var fn = arguments.callee; 1.114 + var args = m.concat(arguments); 1.115 + rval.repr = function () { 1.116 + return [ 1.117 + self.NAME, 1.118 + "(", 1.119 + map(m.repr, args).join(", "), 1.120 + ")" 1.121 + ].join(""); 1.122 + }; 1.123 + } 1.124 + return rval; 1.125 +}; 1.126 + 1.127 +/** @id MochiKit.Format.formatLocale */ 1.128 +MochiKit.Format.formatLocale = function (locale) { 1.129 + if (typeof(locale) == "undefined" || locale === null) { 1.130 + locale = "default"; 1.131 + } 1.132 + if (typeof(locale) == "string") { 1.133 + var rval = MochiKit.Format.LOCALE[locale]; 1.134 + if (typeof(rval) == "string") { 1.135 + rval = arguments.callee(rval); 1.136 + MochiKit.Format.LOCALE[locale] = rval; 1.137 + } 1.138 + return rval; 1.139 + } else { 1.140 + return locale; 1.141 + } 1.142 +}; 1.143 + 1.144 +/** @id MochiKit.Format.twoDigitAverage */ 1.145 +MochiKit.Format.twoDigitAverage = function (numerator, denominator) { 1.146 + if (denominator) { 1.147 + var res = numerator / denominator; 1.148 + if (!isNaN(res)) { 1.149 + return MochiKit.Format.twoDigitFloat(res); 1.150 + } 1.151 + } 1.152 + return "0"; 1.153 +}; 1.154 + 1.155 +/** @id MochiKit.Format.twoDigitFloat */ 1.156 +MochiKit.Format.twoDigitFloat = function (aNumber) { 1.157 + var res = roundToFixed(aNumber, 2); 1.158 + if (res.indexOf(".00") > 0) { 1.159 + return res.substring(0, res.length - 3); 1.160 + } else if (res.charAt(res.length - 1) == "0") { 1.161 + return res.substring(0, res.length - 1); 1.162 + } else { 1.163 + return res; 1.164 + } 1.165 +}; 1.166 + 1.167 +/** @id MochiKit.Format.lstrip */ 1.168 +MochiKit.Format.lstrip = function (str, /* optional */chars) { 1.169 + str = str + ""; 1.170 + if (typeof(str) != "string") { 1.171 + return null; 1.172 + } 1.173 + if (!chars) { 1.174 + return str.replace(/^\s+/, ""); 1.175 + } else { 1.176 + return str.replace(new RegExp("^[" + chars + "]+"), ""); 1.177 + } 1.178 +}; 1.179 + 1.180 +/** @id MochiKit.Format.rstrip */ 1.181 +MochiKit.Format.rstrip = function (str, /* optional */chars) { 1.182 + str = str + ""; 1.183 + if (typeof(str) != "string") { 1.184 + return null; 1.185 + } 1.186 + if (!chars) { 1.187 + return str.replace(/\s+$/, ""); 1.188 + } else { 1.189 + return str.replace(new RegExp("[" + chars + "]+$"), ""); 1.190 + } 1.191 +}; 1.192 + 1.193 +/** @id MochiKit.Format.strip */ 1.194 +MochiKit.Format.strip = function (str, /* optional */chars) { 1.195 + var self = MochiKit.Format; 1.196 + return self.rstrip(self.lstrip(str, chars), chars); 1.197 +}; 1.198 + 1.199 +/** @id MochiKit.Format.truncToFixed */ 1.200 +MochiKit.Format.truncToFixed = function (aNumber, precision) { 1.201 + var res = Math.floor(aNumber).toFixed(0); 1.202 + if (aNumber < 0) { 1.203 + res = Math.ceil(aNumber).toFixed(0); 1.204 + if (res.charAt(0) != "-" && precision > 0) { 1.205 + res = "-" + res; 1.206 + } 1.207 + } 1.208 + if (res.indexOf("e") < 0 && precision > 0) { 1.209 + var tail = aNumber.toString(); 1.210 + if (tail.indexOf("e") > 0) { 1.211 + tail = "."; 1.212 + } else if (tail.indexOf(".") < 0) { 1.213 + tail = "."; 1.214 + } else { 1.215 + tail = tail.substring(tail.indexOf(".")); 1.216 + } 1.217 + if (tail.length - 1 > precision) { 1.218 + tail = tail.substring(0, precision + 1); 1.219 + } 1.220 + while (tail.length - 1 < precision) { 1.221 + tail += "0"; 1.222 + } 1.223 + res += tail; 1.224 + } 1.225 + return res; 1.226 +}; 1.227 + 1.228 +/** @id MochiKit.Format.roundToFixed */ 1.229 +MochiKit.Format.roundToFixed = function (aNumber, precision) { 1.230 + var upper = Math.abs(aNumber) + 0.5 * Math.pow(10, -precision); 1.231 + var res = MochiKit.Format.truncToFixed(upper, precision); 1.232 + if (aNumber < 0) { 1.233 + res = "-" + res; 1.234 + } 1.235 + return res; 1.236 +}; 1.237 + 1.238 +/** @id MochiKit.Format.percentFormat */ 1.239 +MochiKit.Format.percentFormat = function (aNumber) { 1.240 + return MochiKit.Format.twoDigitFloat(100 * aNumber) + '%'; 1.241 +}; 1.242 + 1.243 +MochiKit.Format.EXPORT = [ 1.244 + "truncToFixed", 1.245 + "roundToFixed", 1.246 + "numberFormatter", 1.247 + "formatLocale", 1.248 + "twoDigitAverage", 1.249 + "twoDigitFloat", 1.250 + "percentFormat", 1.251 + "lstrip", 1.252 + "rstrip", 1.253 + "strip" 1.254 +]; 1.255 + 1.256 +MochiKit.Format.LOCALE = { 1.257 + en_US: {separator: ",", decimal: ".", percent: "%"}, 1.258 + de_DE: {separator: ".", decimal: ",", percent: "%"}, 1.259 + pt_BR: {separator: ".", decimal: ",", percent: "%"}, 1.260 + fr_FR: {separator: " ", decimal: ",", percent: "%"}, 1.261 + "default": "en_US" 1.262 +}; 1.263 + 1.264 +MochiKit.Format.EXPORT_OK = []; 1.265 +MochiKit.Format.EXPORT_TAGS = { 1.266 + ':all': MochiKit.Format.EXPORT, 1.267 + ':common': MochiKit.Format.EXPORT 1.268 +}; 1.269 + 1.270 +MochiKit.Format.__new__ = function () { 1.271 + // MochiKit.Base.nameFunctions(this); 1.272 + var base = this.NAME + "."; 1.273 + var k, v, o; 1.274 + for (k in this.LOCALE) { 1.275 + o = this.LOCALE[k]; 1.276 + if (typeof(o) == "object") { 1.277 + o.repr = function () { return this.NAME; }; 1.278 + o.NAME = base + "LOCALE." + k; 1.279 + } 1.280 + } 1.281 + for (k in this) { 1.282 + o = this[k]; 1.283 + if (typeof(o) == 'function' && typeof(o.NAME) == 'undefined') { 1.284 + try { 1.285 + o.NAME = base + k; 1.286 + } catch (e) { 1.287 + // pass 1.288 + } 1.289 + } 1.290 + } 1.291 +}; 1.292 + 1.293 +MochiKit.Format.__new__(); 1.294 + 1.295 +if (typeof(MochiKit.Base) != "undefined") { 1.296 + MochiKit.Base._exportSymbols(this, MochiKit.Format); 1.297 +} else { 1.298 + (function (globals, module) { 1.299 + if ((typeof(JSAN) == 'undefined' && typeof(dojo) == 'undefined') 1.300 + || (MochiKit.__export__ === false)) { 1.301 + var all = module.EXPORT_TAGS[":all"]; 1.302 + for (var i = 0; i < all.length; i++) { 1.303 + globals[all[i]] = module[all[i]]; 1.304 + } 1.305 + } 1.306 + })(this, MochiKit.Format); 1.307 +}