]>
Lady’s Gitweb - Gitweb/blob - static/js/lib/common-lib.js
8cd7804abdc358d2427f556c620e90269bf485b6e47a11f862fd5cd0429d321e
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, e.g. '\u00A0'
26 * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
28 function padLeftStr(input
, width
, str
) {
31 width
-= input
.toString().length
;
36 return prefix
+ input
;
40 * Pad INPUT on the left to WIDTH, using given padding character CH,
41 * for example padLeft('a', 3, '_') is '__a'.
43 * @param {String} input: input value converted to string.
44 * @param {Number} width: desired length of output.
45 * @param {String} ch: single character to prefix to string.
47 * @returns {String} Modified string, at least SIZE length.
49 function padLeft(input
, width
, ch
) {
51 while (s
.length
< width
) {
58 /* ............................................................ */
62 * Create XMLHttpRequest object in cross-browser way
63 * @returns XMLHttpRequest object, or null
65 function createRequestObject() {
67 return new XMLHttpRequest();
70 return window
.createRequest();
73 return new ActiveXObject("Msxml2.XMLHTTP");
76 return new ActiveXObject("Microsoft.XMLHTTP");
83 /* ............................................................ */
87 * used to extract hours and minutes from timezone info, e.g '-0900'
90 var tzRe
= /^([+-])([0-9][0-9])([0-9][0-9])$/;
93 * convert numeric timezone +/-ZZZZ to offset from UTC in seconds
95 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
96 * @returns {Number} offset from UTC in seconds for timezone
100 function timezoneOffset(timezoneInfo
) {
101 var match
= tzRe
.exec(timezoneInfo
);
102 var tz_sign
= (match
[1] === '-' ? -1 : +1);
103 var tz_hour
= parseInt(match
[2],10);
104 var tz_min
= parseInt(match
[3],10);
106 return tz_sign
*(((tz_hour
*60) + tz_min
)*60);
110 * return date in local time formatted in iso-8601 like format
111 * 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
113 * @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
114 * @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
115 * @returns {String} date in local time in iso-8601 like format
117 function formatDateISOLocal(epoch
, timezoneInfo
) {
118 // date corrected by timezone
119 var localDate
= new Date(1000 * (epoch
+
120 timezoneOffset(timezoneInfo
)));
121 var localDateStr
= // e.g. '2005-08-07'
122 localDate
.getUTCFullYear() + '-' +
123 padLeft(localDate
.getUTCMonth()+1, 2, '0') + '-' +
124 padLeft(localDate
.getUTCDate(), 2, '0');
125 var localTimeStr
= // e.g. '21:49:46'
126 padLeft(localDate
.getUTCHours(), 2, '0') + ':' +
127 padLeft(localDate
.getUTCMinutes(), 2, '0') + ':' +
128 padLeft(localDate
.getUTCSeconds(), 2, '0');
130 return localDateStr
+ ' ' + localTimeStr
+ ' ' + timezoneInfo
;
134 /* ............................................................ */
135 /* unquoting/unescaping filenames */
140 var escCodeRe
= /\\([^0-7]|[0-7]{1,3})/g;
141 var octEscRe
= /^[0-7]{1,3}$/;
142 var maybeQuotedRe
= /^\"(.*)\"$/;
146 * unquote maybe C-quoted filename (as used by git, i.e. it is
147 * in double quotes '"' if there is any escape character used)
148 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a a'
150 * @param {String} str: git-quoted string
151 * @returns {String} Unquoted and unescaped string
153 * @globals escCodeRe, octEscRe, maybeQuotedRe
155 function unquote(str
) {
158 // character escape codes, aka escape sequences (from C)
159 // replacements are to some extent JavaScript specific
160 t: "\t", // tab (HT, TAB)
161 n: "\n", // newline (NL)
162 r: "\r", // return (CR)
163 f: "\f", // form feed (FF)
164 b: "\b", // backspace (BS)
165 a: "\x07", // alarm (bell) (BEL)
166 e: "\x1B", // escape (ESC)
167 v: "\v" // vertical tab (VT)
170 if (seq
.search(octEscRe
) !== -1) {
171 // octal char sequence
172 return String
.fromCharCode(parseInt(seq
, 8));
173 } else if (seq
in es
) {
174 // C escape sequence, aka character escape code
177 // quoted ordinary character
181 var match
= str
.match(maybeQuotedRe
);
184 // perhaps str = eval('"'+str+'"'); would be enough?
185 str
= str
.replace(escCodeRe
,
186 function (substr
, p1
, offset
, s
) { return unq(p1
); });
191 /* end of common-lib.js */
This page took 0.51792 seconds and 3 git commands to generate.