1 // Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
2 // 2007, Petr Baudis <pasky@suse.cz>
3 // 2008-2011, Jakub Narebski <jnareb@gmail.com>
6 * @fileOverview Generic JavaScript code (helper functions)
7 * @license GPLv2 or later
11 /* ============================================================ */
12 /* ............................................................ */
16 * pad INPUT on the left with STR that is assumed to have visible
17 * width of single character (for example nonbreakable spaces),
20 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
21 * ('\u00A0' is nonbreakable space)
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)
28 function padLeftStr(input, width, str) {
30 if (typeof str === 'undefined') {
31 ch = '\u00A0'; // using ' ' doesn't work in all browsers
34 width -= input.toString().length;
39 return prefix + input;
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'))
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'.
51 * @returns {String} Modified string, at least SIZE length.
53 function padLeft(input, width, ch) {
55 if (typeof ch === 'undefined') {
59 while (s.length < width) {
66 /* ............................................................ */
70 * Create XMLHttpRequest object in cross-browser way
71 * @returns XMLHttpRequest object, or null
73 function createRequestObject() {
75 return new XMLHttpRequest();
78 return window.createRequest();
81 return new ActiveXObject("Msxml2.XMLHTTP");
84 return new ActiveXObject("Microsoft.XMLHTTP");
91 /* ............................................................ */
95 * used to extract hours and minutes from timezone info, e.g '-0900'
98 var tzRe = /^([+-])([0-9][0-9])([0-9][0-9])$/;
101 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
103 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
104 * @returns {Number} offset from UTC in seconds for timezone
108 function 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);
114 return tz_sign*(((tz_hour*60) + tz_min)*60);
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'
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
125 function 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');
138 return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
142 /* ............................................................ */
143 /* unquoting/unescaping filenames */
148 var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
149 var octEscRe = /^[0-7]{1,3}$/;
150 var maybeQuotedRe = /^\"(.*)\"$/;
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'
158 * @param {String} str: git-quoted string
159 * @returns {String} Unquoted and unescaped string
161 * @globals escCodeRe, octEscRe, maybeQuotedRe
163 function unquote(str) {
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)
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
185 // quoted ordinary character
189 var match = str.match(maybeQuotedRe);
192 // perhaps str = eval('"'+str+'"'); would be enough?
193 str = str.replace(escCodeRe,
194 function (substr, p1, offset, s) { return unq(p1); });
199 /* end of common-lib.js */