Format.js 8.3 KB

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