]>
Lady’s Gitweb - Gitweb/blob - static/js/lib/common-lib.js
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 number N with nonbreakable spaces on the left, to WIDTH characters
17 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
18 * ('\u00A0' is nonbreakable space)
20 * @param {Number|String} input: number to pad
21 * @param {Number} width: visible width of output
22 * @param {String} str: string to prefix to string, e.g. '\u00A0'
23 * @returns {String} INPUT prefixed with (WIDTH - INPUT.length) x STR
25 function padLeftStr(input
, width
, str
) {
28 width
-= input
.toString().length
;
33 return prefix
+ input
;
37 * Pad INPUT on the left to SIZE width, using given padding character CH,
38 * for example padLeft('a', 3, '_') is '__a'.
40 * @param {String} input: input value converted to string.
41 * @param {Number} width: desired length of output.
42 * @param {String} ch: single character to prefix to string.
44 * @returns {String} Modified string, at least SIZE length.
46 function padLeft(input
, width
, ch
) {
48 while (s
.length
< width
) {
55 /* ............................................................ */
59 * Create XMLHttpRequest object in cross-browser way
60 * @returns XMLHttpRequest object, or null
62 function createRequestObject() {
64 return new XMLHttpRequest();
67 return window
.createRequest();
70 return new ActiveXObject("Msxml2.XMLHTTP");
73 return new ActiveXObject("Microsoft.XMLHTTP");
80 /* ............................................................ */
84 * used to extract hours and minutes from timezone info, e.g '-0900'
87 var tzRe
= /^([+-])([0-9][0-9])([0-9][0-9])$/;
90 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
92 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
93 * @returns {Number} offset from UTC in seconds for timezone
97 function timezoneOffset(timezoneInfo
) {
98 var match
= tzRe
.exec(timezoneInfo
);
99 var tz_sign
= (match
[1] === '-' ? -1 : +1);
100 var tz_hour
= parseInt(match
[2],10);
101 var tz_min
= parseInt(match
[3],10);
103 return tz_sign
*(((tz_hour
*60) + tz_min
)*60);
107 * return date in local time formatted in iso-8601 like format
108 * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
110 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
111 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
112 * @returns {String} date in local time in iso-8601 like format
114 function formatDateISOLocal(epoch
, timezoneInfo
) {
115 // date corrected by timezone
116 var localDate
= new Date(1000 * (epoch
+
117 timezoneOffset(timezoneInfo
)));
118 var localDateStr
= // e.g. '2005-08-07'
119 localDate
.getUTCFullYear() + '-' +
120 padLeft(localDate
.getUTCMonth()+1, 2, '0') + '-' +
121 padLeft(localDate
.getUTCDate(), 2, '0');
122 var localTimeStr
= // e.g. '21:49:46'
123 padLeft(localDate
.getUTCHours(), 2, '0') + ':' +
124 padLeft(localDate
.getUTCMinutes(), 2, '0') + ':' +
125 padLeft(localDate
.getUTCSeconds(), 2, '0');
127 return localDateStr
+ ' ' + localTimeStr
+ ' ' + timezoneInfo
;
131 /* ............................................................ */
132 /* unquoting/unescaping filenames */
137 var escCodeRe
= /\\([^0-7]|[0-7]{1,3})/g;
138 var octEscRe
= /^[0-7]{1,3}$/;
139 var maybeQuotedRe
= /^\"(.*)\"$/;
143 * unquote maybe git-quoted filename
144 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a a'
146 * @param {String} str: git-quoted string
147 * @returns {String} Unquoted and unescaped string
149 * @globals escCodeRe, octEscRe, maybeQuotedRe
151 function unquote(str
) {
154 // character escape codes, aka escape sequences (from C)
155 // replacements are to some extent JavaScript specific
156 t: "\t", // tab (HT, TAB)
157 n: "\n", // newline (NL)
158 r: "\r", // return (CR)
159 f: "\f", // form feed (FF)
160 b: "\b", // backspace (BS)
161 a: "\x07", // alarm (bell) (BEL)
162 e: "\x1B", // escape (ESC)
163 v: "\v" // vertical tab (VT)
166 if (seq
.search(octEscRe
) !== -1) {
167 // octal char sequence
168 return String
.fromCharCode(parseInt(seq
, 8));
169 } else if (seq
in es
) {
170 // C escape sequence, aka character escape code
173 // quoted ordinary character
177 var match
= str
.match(maybeQuotedRe
);
180 // perhaps str = eval('"'+str+'"'); would be enough?
181 str
= str
.replace(escCodeRe
,
182 function (substr
, p1
, offset
, s
) { return unq(p1
); });
187 /* end of common-lib.js */
This page took 0.344306 seconds and 5 git commands to generate.