1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/ajax/mochikit/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 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 +if (typeof(dojo) != 'undefined') { 1.15 + dojo.provide('MochiKit.Format'); 1.16 +} 1.17 + 1.18 +if (typeof(MochiKit) == 'undefined') { 1.19 + MochiKit = {}; 1.20 +} 1.21 + 1.22 +if (typeof(MochiKit.Format) == 'undefined') { 1.23 + MochiKit.Format = {}; 1.24 +} 1.25 + 1.26 +MochiKit.Format.NAME = "MochiKit.Format"; 1.27 +MochiKit.Format.VERSION = "1.4"; 1.28 +MochiKit.Format.__repr__ = function () { 1.29 + return "[" + this.NAME + " " + this.VERSION + "]"; 1.30 +}; 1.31 +MochiKit.Format.toString = function () { 1.32 + return this.__repr__(); 1.33 +}; 1.34 + 1.35 +MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) { 1.36 + return function (num) { 1.37 + num = parseFloat(num); 1.38 + if (typeof(num) == "undefined" || num === null || isNaN(num)) { 1.39 + return placeholder; 1.40 + } 1.41 + var curheader = header; 1.42 + var curfooter = footer; 1.43 + if (num < 0) { 1.44 + num = -num; 1.45 + } else { 1.46 + curheader = curheader.replace(/-/, ""); 1.47 + } 1.48 + var me = arguments.callee; 1.49 + var fmt = MochiKit.Format.formatLocale(locale); 1.50 + if (isPercent) { 1.51 + num = num * 100.0; 1.52 + curfooter = fmt.percent + curfooter; 1.53 + } 1.54 + num = MochiKit.Format.roundToFixed(num, precision); 1.55 + var parts = num.split(/\./); 1.56 + var whole = parts[0]; 1.57 + var frac = (parts.length == 1) ? "" : parts[1]; 1.58 + var res = ""; 1.59 + while (whole.length < leadingZeros) { 1.60 + whole = "0" + whole; 1.61 + } 1.62 + if (separatorAt) { 1.63 + while (whole.length > separatorAt) { 1.64 + var i = whole.length - separatorAt; 1.65 + //res = res + fmt.separator + whole.substring(i, whole.length); 1.66 + res = fmt.separator + whole.substring(i, whole.length) + res; 1.67 + whole = whole.substring(0, i); 1.68 + } 1.69 + } 1.70 + res = whole + res; 1.71 + if (precision > 0) { 1.72 + while (frac.length < trailingZeros) { 1.73 + frac = frac + "0"; 1.74 + } 1.75 + res = res + fmt.decimal + frac; 1.76 + } 1.77 + return curheader + res + curfooter; 1.78 + }; 1.79 +}; 1.80 + 1.81 +/** @id MochiKit.Format.numberFormatter */ 1.82 +MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) { 1.83 + // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html 1.84 + // | 0 | leading or trailing zeros 1.85 + // | # | just the number 1.86 + // | , | separator 1.87 + // | . | decimal separator 1.88 + // | % | Multiply by 100 and format as percent 1.89 + if (typeof(placeholder) == "undefined") { 1.90 + placeholder = ""; 1.91 + } 1.92 + var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/); 1.93 + if (!match) { 1.94 + throw TypeError("Invalid pattern"); 1.95 + } 1.96 + var header = pattern.substr(0, match.index); 1.97 + var footer = pattern.substr(match.index + match[0].length); 1.98 + if (header.search(/-/) == -1) { 1.99 + header = header + "-"; 1.100 + } 1.101 + var whole = match[1]; 1.102 + var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : ""; 1.103 + var isPercent = (typeof(match[3]) == "string" && match[3] != ""); 1.104 + var tmp = whole.split(/,/); 1.105 + var separatorAt; 1.106 + if (typeof(locale) == "undefined") { 1.107 + locale = "default"; 1.108 + } 1.109 + if (tmp.length == 1) { 1.110 + separatorAt = null; 1.111 + } else { 1.112 + separatorAt = tmp[1].length; 1.113 + } 1.114 + var leadingZeros = whole.length - whole.replace(/0/g, "").length; 1.115 + var trailingZeros = frac.length - frac.replace(/0/g, "").length; 1.116 + var precision = frac.length; 1.117 + var rval = MochiKit.Format._numberFormatter( 1.118 + placeholder, header, footer, locale, isPercent, precision, 1.119 + leadingZeros, separatorAt, trailingZeros 1.120 + ); 1.121 + var m = MochiKit.Base; 1.122 + if (m) { 1.123 + var fn = arguments.callee; 1.124 + var args = m.concat(arguments); 1.125 + rval.repr = function () { 1.126 + return [ 1.127 + self.NAME, 1.128 + "(", 1.129 + map(m.repr, args).join(", "), 1.130 + ")" 1.131 + ].join(""); 1.132 + }; 1.133 + } 1.134 + return rval; 1.135 +}; 1.136 + 1.137 +/** @id MochiKit.Format.formatLocale */ 1.138 +MochiKit.Format.formatLocale = function (locale) { 1.139 + if (typeof(locale) == "undefined" || locale === null) { 1.140 + locale = "default"; 1.141 + } 1.142 + if (typeof(locale) == "string") { 1.143 + var rval = MochiKit.Format.LOCALE[locale]; 1.144 + if (typeof(rval) == "string") { 1.145 + rval = arguments.callee(rval); 1.146 + MochiKit.Format.LOCALE[locale] = rval; 1.147 + } 1.148 + return rval; 1.149 + } else { 1.150 + return locale; 1.151 + } 1.152 +}; 1.153 + 1.154 +/** @id MochiKit.Format.twoDigitAverage */ 1.155 +MochiKit.Format.twoDigitAverage = function (numerator, denominator) { 1.156 + if (denominator) { 1.157 + var res = numerator / denominator; 1.158 + if (!isNaN(res)) { 1.159 + return MochiKit.Format.twoDigitFloat(numerator / denominator); 1.160 + } 1.161 + } 1.162 + return "0"; 1.163 +}; 1.164 + 1.165 +/** @id MochiKit.Format.twoDigitFloat */ 1.166 +MochiKit.Format.twoDigitFloat = function (someFloat) { 1.167 + var sign = (someFloat < 0 ? '-' : ''); 1.168 + var s = Math.floor(Math.abs(someFloat) * 100).toString(); 1.169 + if (s == '0') { 1.170 + return s; 1.171 + } 1.172 + if (s.length < 3) { 1.173 + while (s.charAt(s.length - 1) == '0') { 1.174 + s = s.substring(0, s.length - 1); 1.175 + } 1.176 + return sign + '0.' + s; 1.177 + } 1.178 + var head = sign + s.substring(0, s.length - 2); 1.179 + var tail = s.substring(s.length - 2, s.length); 1.180 + if (tail == '00') { 1.181 + return head; 1.182 + } else if (tail.charAt(1) == '0') { 1.183 + return head + '.' + tail.charAt(0); 1.184 + } else { 1.185 + return head + '.' + tail; 1.186 + } 1.187 +}; 1.188 + 1.189 +/** @id MochiKit.Format.lstrip */ 1.190 +MochiKit.Format.lstrip = function (str, /* optional */chars) { 1.191 + str = str + ""; 1.192 + if (typeof(str) != "string") { 1.193 + return null; 1.194 + } 1.195 + if (!chars) { 1.196 + return str.replace(/^\s+/, ""); 1.197 + } else { 1.198 + return str.replace(new RegExp("^[" + chars + "]+"), ""); 1.199 + } 1.200 +}; 1.201 + 1.202 +/** @id MochiKit.Format.rstrip */ 1.203 +MochiKit.Format.rstrip = function (str, /* optional */chars) { 1.204 + str = str + ""; 1.205 + if (typeof(str) != "string") { 1.206 + return null; 1.207 + } 1.208 + if (!chars) { 1.209 + return str.replace(/\s+$/, ""); 1.210 + } else { 1.211 + return str.replace(new RegExp("[" + chars + "]+$"), ""); 1.212 + } 1.213 +}; 1.214 + 1.215 +/** @id MochiKit.Format.strip */ 1.216 +MochiKit.Format.strip = function (str, /* optional */chars) { 1.217 + var self = MochiKit.Format; 1.218 + return self.rstrip(self.lstrip(str, chars), chars); 1.219 +}; 1.220 + 1.221 +/** @id MochiKit.Format.truncToFixed */ 1.222 +MochiKit.Format.truncToFixed = function (aNumber, precision) { 1.223 + aNumber = Math.floor(aNumber * Math.pow(10, precision)); 1.224 + var res = (aNumber * Math.pow(10, -precision)).toFixed(precision); 1.225 + if (res.charAt(0) == ".") { 1.226 + res = "0" + res; 1.227 + } 1.228 + return res; 1.229 +}; 1.230 + 1.231 +/** @id MochiKit.Format.roundToFixed */ 1.232 +MochiKit.Format.roundToFixed = function (aNumber, precision) { 1.233 + return MochiKit.Format.truncToFixed( 1.234 + aNumber + 0.5 * Math.pow(10, -precision), 1.235 + precision 1.236 + ); 1.237 +}; 1.238 + 1.239 +/** @id MochiKit.Format.percentFormat */ 1.240 +MochiKit.Format.percentFormat = function (someFloat) { 1.241 + return MochiKit.Format.twoDigitFloat(100 * someFloat) + '%'; 1.242 +}; 1.243 + 1.244 +MochiKit.Format.EXPORT = [ 1.245 + "truncToFixed", 1.246 + "roundToFixed", 1.247 + "numberFormatter", 1.248 + "formatLocale", 1.249 + "twoDigitAverage", 1.250 + "twoDigitFloat", 1.251 + "percentFormat", 1.252 + "lstrip", 1.253 + "rstrip", 1.254 + "strip" 1.255 +]; 1.256 + 1.257 +MochiKit.Format.LOCALE = { 1.258 + en_US: {separator: ",", decimal: ".", percent: "%"}, 1.259 + de_DE: {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 +}