c45454ead209f9b63b61f77e1625979bcb0b6542
1// Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
2// 2007, Petr Baudis <pasky@suse.cz>
3// 2008-2011, Jakub Narebski <jnareb@gmail.com>
4
5/**
6 * @fileOverview Generic JavaScript code (helper functions)
7 * @license GPLv2 or later
8 */
9
10
11/* ============================================================ */
12/* ............................................................ */
13/* Padding */
14
15/**
16 * pad INPUT on the left with STR that is assumed to have visible
17 * width of single character (for example nonbreakable spaces),
18 * to WIDTH characters
19 *
20 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
21 * ('\u00A0' is nonbreakable space)
22 *
23 * @param {Number|String} input: number to pad
24 * @param {Number} width: visible width of output
25 * @param {String} str: string to prefix to string, defaults to '\u00A0'
26 * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
27 */
28function padLeftStr(input, width, str) {
29 var prefix = '';
30 if (typeof str === 'undefined') {
31 ch = '\u00A0'; // using ' ' doesn't work in all browsers
32 }
33
34 width -= input.toString().length;
35 while (width > 0) {
36 prefix += str;
37 width--;
38 }
39 return prefix + input;
40}
41
42/**
43 * Pad INPUT on the left to WIDTH, using given padding character CH,
44 * for example padLeft('a', 3, '_') is '__a'
45 * padLeft(4, 2) is '04' (same as padLeft(4, 2, '0'))
46 *
47 * @param {String} input: input value converted to string.
48 * @param {Number} width: desired length of output.
49 * @param {String} ch: single character to prefix to string, defaults to '0'.
50 *
51 * @returns {String} Modified string, at least SIZE length.
52 */
53function padLeft(input, width, ch) {
54 var s = input + "";
55 if (typeof ch === 'undefined') {
56 ch = '0';
57 }
58
59 while (s.length < width) {
60 s = ch + s;
61 }
62 return s;
63}
64
65
66/* ............................................................ */
67/* Ajax */
68
69/**
70 * Create XMLHttpRequest object in cross-browser way
71 * @returns XMLHttpRequest object, or null
72 */
73function createRequestObject() {
74 try {
75 return new XMLHttpRequest();
76 } catch (e) {}
77 try {
78 return window.createRequest();
79 } catch (e) {}
80 try {
81 return new ActiveXObject("Msxml2.XMLHTTP");
82 } catch (e) {}
83 try {
84 return new ActiveXObject("Microsoft.XMLHTTP");
85 } catch (e) {}
86
87 return null;
88}
89
90
91/* ............................................................ */
92/* time and data */
93
94/**
95 * used to extract hours and minutes from timezone info, e.g '-0900'
96 * @constant
97 */
98var tzRe = /^([+-])([0-9][0-9])([0-9][0-9])$/;
99
100/**
101 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
102 *
103 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
104 * @returns {Number} offset from UTC in seconds for timezone
105 *
106 * @globals tzRe
107 */
108function timezoneOffset(timezoneInfo) {
109 var match = tzRe.exec(timezoneInfo);
110 var tz_sign = (match[1] === '-' ? -1 : +1);
111 var tz_hour = parseInt(match[2],10);
112 var tz_min = parseInt(match[3],10);
113
114 return tz_sign*(((tz_hour*60) + tz_min)*60);
115}
116
117/**
118 * return date in local time formatted in iso-8601 like format
119 * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
120 *
121 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
122 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
123 * @returns {String} date in local time in iso-8601 like format
124 */
125function formatDateISOLocal(epoch, timezoneInfo) {
126 // date corrected by timezone
127 var localDate = new Date(1000 * (epoch +
128 timezoneOffset(timezoneInfo)));
129 var localDateStr = // e.g. '2005-08-07'
130 localDate.getUTCFullYear() + '-' +
131 padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
132 padLeft(localDate.getUTCDate(), 2, '0');
133 var localTimeStr = // e.g. '21:49:46'
134 padLeft(localDate.getUTCHours(), 2, '0') + ':' +
135 padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
136 padLeft(localDate.getUTCSeconds(), 2, '0');
137
138 return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
139}
140
141
142/* ............................................................ */
143/* unquoting/unescaping filenames */
144
145/**#@+
146 * @constant
147 */
148var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
149var octEscRe = /^[0-7]{1,3}$/;
150var maybeQuotedRe = /^\"(.*)\"$/;
151/**#@-*/
152
153/**
154 * unquote maybe C-quoted filename (as used by git, i.e. it is
155 * in double quotes '"' if there is any escape character used)
156 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a a'
157 *
158 * @param {String} str: git-quoted string
159 * @returns {String} Unquoted and unescaped string
160 *
161 * @globals escCodeRe, octEscRe, maybeQuotedRe
162 */
163function unquote(str) {
164 function unq(seq) {
165 var es = {
166 // character escape codes, aka escape sequences (from C)
167 // replacements are to some extent JavaScript specific
168 t: "\t", // tab (HT, TAB)
169 n: "\n", // newline (NL)
170 r: "\r", // return (CR)
171 f: "\f", // form feed (FF)
172 b: "\b", // backspace (BS)
173 a: "\x07", // alarm (bell) (BEL)
174 e: "\x1B", // escape (ESC)
175 v: "\v" // vertical tab (VT)
176 };
177
178 if (seq.search(octEscRe) !== -1) {
179 // octal char sequence
180 return String.fromCharCode(parseInt(seq, 8));
181 } else if (seq in es) {
182 // C escape sequence, aka character escape code
183 return es[seq];
184 }
185 // quoted ordinary character
186 return seq;
187 }
188
189 var match = str.match(maybeQuotedRe);
190 if (match) {
191 str = match[1];
192 // perhaps str = eval('"'+str+'"'); would be enough?
193 str = str.replace(escCodeRe,
194 function (substr, p1, offset, s) { return unq(p1); });
195 }
196 return str;
197}
198
199/* end of common-lib.js */