summaryrefslogtreecommitdiff
path: root/frontend/delta/js/MochiKit/Format.js
Unidiff
Diffstat (limited to 'frontend/delta/js/MochiKit/Format.js') (more/less context) (ignore whitespace changes)
-rw-r--r--frontend/delta/js/MochiKit/Format.js332
1 files changed, 332 insertions, 0 deletions
diff --git a/frontend/delta/js/MochiKit/Format.js b/frontend/delta/js/MochiKit/Format.js
new file mode 100644
index 0000000..0e7af50
--- a/dev/null
+++ b/frontend/delta/js/MochiKit/Format.js
@@ -0,0 +1,332 @@
1/*
2
3Copyright 2008-2013 Clipperz Srl
4
5This file is part of Clipperz, the online password manager.
6For further information about its features and functionalities please
7refer to http://www.clipperz.com.
8
9* Clipperz is free software: you can redistribute it and/or modify it
10 under the terms of the GNU Affero General Public License as published
11 by the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14* Clipperz is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU Affero General Public License for more details.
18
19* You should have received a copy of the GNU Affero General Public
20 License along with Clipperz. If not, see http://www.gnu.org/licenses/.
21
22*/
23
24/***
25
26MochiKit.Format 1.5
27
28See <http://mochikit.com/> for documentation, downloads, license, etc.
29
30(c) 2005 Bob Ippolito. All rights Reserved.
31
32***/
33
34MochiKit.Base.module(MochiKit, 'Format', '1.5', ['Base']);
35
36MochiKit.Format._numberFormatter = function (placeholder, header, footer, locale, isPercent, precision, leadingZeros, separatorAt, trailingZeros) {
37 return function (num) {
38 num = parseFloat(num);
39 if (typeof(num) == "undefined" || num === null || isNaN(num)) {
40 return placeholder;
41 }
42 var curheader = header;
43 var curfooter = footer;
44 if (num < 0) {
45 num = -num;
46 } else {
47 curheader = curheader.replace(/-/, "");
48 }
49 var me = arguments.callee;
50 var fmt = MochiKit.Format.formatLocale(locale);
51 if (isPercent) {
52 num = num * 100.0;
53 curfooter = fmt.percent + curfooter;
54 }
55 num = MochiKit.Format.roundToFixed(num, precision);
56 var parts = num.split(/\./);
57 var whole = parts[0];
58 var frac = (parts.length == 1) ? "" : parts[1];
59 var res = "";
60 while (whole.length < leadingZeros) {
61 whole = "0" + whole;
62 }
63 if (separatorAt) {
64 while (whole.length > separatorAt) {
65 var i = whole.length - separatorAt;
66 //res = res + fmt.separator + whole.substring(i, whole.length);
67 res = fmt.separator + whole.substring(i, whole.length) + res;
68 whole = whole.substring(0, i);
69 }
70 }
71 res = whole + res;
72 if (precision > 0) {
73 while (frac.length < trailingZeros) {
74 frac = frac + "0";
75 }
76 res = res + fmt.decimal + frac;
77 }
78 return curheader + res + curfooter;
79 };
80};
81
82/** @id MochiKit.Format.numberFormatter */
83MochiKit.Format.numberFormatter = function (pattern, placeholder/* = "" */, locale/* = "default" */) {
84 // http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
85 // | 0 | leading or trailing zeros
86 // | # | just the number
87 // | , | separator
88 // | . | decimal separator
89 // | % | Multiply by 100 and format as percent
90 if (typeof(placeholder) == "undefined") {
91 placeholder = "";
92 }
93 var match = pattern.match(/((?:[0#]+,)?[0#]+)(?:\.([0#]+))?(%)?/);
94 if (!match) {
95 throw TypeError("Invalid pattern");
96 }
97 var header = pattern.substr(0, match.index);
98 var footer = pattern.substr(match.index + match[0].length);
99 if (header.search(/-/) == -1) {
100 header = header + "-";
101 }
102 var whole = match[1];
103 var frac = (typeof(match[2]) == "string" && match[2] != "") ? match[2] : "";
104 var isPercent = (typeof(match[3]) == "string" && match[3] != "");
105 var tmp = whole.split(/,/);
106 var separatorAt;
107 if (typeof(locale) == "undefined") {
108 locale = "default";
109 }
110 if (tmp.length == 1) {
111 separatorAt = null;
112 } else {
113 separatorAt = tmp[1].length;
114 }
115 var leadingZeros = whole.length - whole.replace(/0/g, "").length;
116 var trailingZeros = frac.length - frac.replace(/0/g, "").length;
117 var precision = frac.length;
118 var rval = MochiKit.Format._numberFormatter(
119 placeholder, header, footer, locale, isPercent, precision,
120 leadingZeros, separatorAt, trailingZeros
121 );
122 var m = MochiKit.Base;
123 if (m) {
124 var fn = arguments.callee;
125 var args = m.concat(arguments);
126 rval.repr = function () {
127 return [
128 self.NAME,
129 "(",
130 m.map(m.repr, args).join(", "),
131 ")"
132 ].join("");
133 };
134 }
135 return rval;
136};
137
138/** @id MochiKit.Format.formatLocale */
139MochiKit.Format.formatLocale = function (locale) {
140 if (typeof(locale) == "undefined" || locale === null) {
141 locale = "default";
142 }
143 if (typeof(locale) == "string") {
144 var rval = MochiKit.Format.LOCALE[locale];
145 if (typeof(rval) == "string") {
146 rval = arguments.callee(rval);
147 MochiKit.Format.LOCALE[locale] = rval;
148 }
149 return rval;
150 } else {
151 return locale;
152 }
153};
154
155/** @id MochiKit.Format.twoDigitAverage */
156MochiKit.Format.twoDigitAverage = function (numerator, denominator) {
157 if (denominator) {
158 var res = numerator / denominator;
159 if (!isNaN(res)) {
160 return MochiKit.Format.twoDigitFloat(res);
161 }
162 }
163 return "0";
164};
165
166/** @id MochiKit.Format.twoDigitFloat */
167MochiKit.Format.twoDigitFloat = function (aNumber) {
168 var res = MochiKit.Format.roundToFixed(aNumber, 2);
169 if (res.indexOf(".00") > 0) {
170 return res.substring(0, res.length - 3);
171 } else if (res.charAt(res.length - 1) == "0") {
172 return res.substring(0, res.length - 1);
173 } else {
174 return res;
175 }
176};
177
178/** @id MochiKit.Format.lstrip */
179MochiKit.Format.lstrip = 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
191/** @id MochiKit.Format.rstrip */
192MochiKit.Format.rstrip = function (str, /* optional */chars) {
193 str = str + "";
194 if (typeof(str) != "string") {
195 return null;
196 }
197 if (!chars) {
198 return str.replace(/\s+$/, "");
199 } else {
200 return str.replace(new RegExp("[" + chars + "]+$"), "");
201 }
202};
203
204/** @id MochiKit.Format.strip */
205MochiKit.Format.strip = function (str, /* optional */chars) {
206 var self = MochiKit.Format;
207 return self.rstrip(self.lstrip(str, chars), chars);
208};
209
210/** @id MochiKit.Format.truncToFixed */
211MochiKit.Format.truncToFixed = function (aNumber, precision) {
212 var fixed = MochiKit.Format._numberToFixed(aNumber, precision);
213 var fracPos = fixed.indexOf(".");
214 if (fracPos > 0 && fracPos + precision + 1 < fixed.length) {
215 fixed = fixed.substring(0, fracPos + precision + 1);
216 fixed = MochiKit.Format._shiftNumber(fixed, 0);
217 }
218 return fixed;
219};
220
221/** @id MochiKit.Format.roundToFixed */
222MochiKit.Format.roundToFixed = function (aNumber, precision) {
223 var fixed = MochiKit.Format._numberToFixed(aNumber, precision);
224 var fracPos = fixed.indexOf(".");
225 if (fracPos > 0 && fracPos + precision + 1 < fixed.length) {
226 var str = MochiKit.Format._shiftNumber(fixed, precision);
227 str = MochiKit.Format._numberToFixed(Math.round(parseFloat(str)), 0);
228 fixed = MochiKit.Format._shiftNumber(str, -precision);
229 }
230 return fixed;
231};
232
233/**
234 * Converts a number to a fixed format string. This function handles
235 * conversion of exponents by shifting the decimal point to the left
236 * or the right. It also guarantees a specified minimum number of
237 * fractional digits (but no maximum).
238 *
239 * @param {Number} aNumber the number to convert
240 * @param {Number} precision the minimum number of decimal digits
241 *
242 * @return {String} the fixed format number string
243 */
244MochiKit.Format._numberToFixed = function (aNumber, precision) {
245 var str = aNumber.toString();
246 var parts = str.split(/[eE]/);
247 var exp = (parts.length === 1) ? 0 : parseInt(parts[1], 10) || 0;
248 var fixed = MochiKit.Format._shiftNumber(parts[0], exp);
249 parts = fixed.split(/\./);
250 var whole = parts[0];
251 var frac = (parts.length === 1) ? "" : parts[1];
252 while (frac.length < precision) {
253 frac += "0";
254 }
255 if (frac.length > 0) {
256 return whole + "." + frac;
257 } else {
258 return whole;
259 }
260};
261
262/**
263 * Shifts the decimal dot location in a fixed format number string.
264 * This function handles negative values and will add and remove
265 * leading and trailing zeros as needed.
266 *
267 * @param {String} num the fixed format number string
268 * @param {Number} exp the base-10 exponent to apply
269 *
270 * @return {String} the new fixed format number string
271 */
272MochiKit.Format._shiftNumber = function (num, exp) {
273 var pos = num.indexOf(".");
274 if (pos < 0) {
275 pos = num.length;
276 } else {
277 num = num.substring(0, pos) + num.substring(pos + 1);
278 }
279 pos += exp;
280 while (pos <= 0 || (pos <= 1 && num.charAt(0) === "-")) {
281 if (num.charAt(0) === "-") {
282 num = "-0" + num.substring(1);
283 } else {
284 num = "0" + num;
285 }
286 pos++;
287 }
288 while (pos > num.length) {
289 num += "0";
290 }
291 if (pos < num.length) {
292 num = num.substring(0, pos) + "." + num.substring(pos);
293 }
294 while (/^0[^.]/.test(num)) {
295 num = num.substring(1);
296 }
297 while (/^-0[^.]/.test(num)) {
298 num = "-" + num.substring(2);
299 }
300 return num;
301};
302
303/** @id MochiKit.Format.percentFormat */
304MochiKit.Format.percentFormat = function (aNumber) {
305 return MochiKit.Format.twoDigitFloat(100 * aNumber) + '%';
306};
307
308MochiKit.Format.LOCALE = {
309 en_US: {separator: ",", decimal: ".", percent: "%"},
310 de_DE: {separator: ".", decimal: ",", percent: "%"},
311 pt_BR: {separator: ".", decimal: ",", percent: "%"},
312 fr_FR: {separator: " ", decimal: ",", percent: "%"},
313 "default": "en_US",
314 __export__: false
315};
316
317MochiKit.Format.__new__ = function () {
318 MochiKit.Base.nameFunctions(this);
319 var base = this.NAME + ".";
320 var k, v, o;
321 for (k in this.LOCALE) {
322 o = this.LOCALE[k];
323 if (typeof(o) == "object") {
324 o.repr = function () { return this.NAME; };
325 o.NAME = base + "LOCALE." + k;
326 }
327 }
328};
329
330MochiKit.Format.__new__();
331
332MochiKit.Base._exportSymbols(this, MochiKit.Format);