author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Sep 2011 09:27:42 +0200 | |
branch | stable |
changeset 7879 | 9aae456abab5 |
parent 6921 | c1fdf590712f |
permissions | -rw-r--r-- |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
1 |
/** |
0 | 2 |
* This file contains extensions for standard javascript types |
3 |
* |
|
4 |
*/ |
|
5 |
||
6 |
ONE_DAY = 86400000; // (in milliseconds) |
|
7 |
// ========== DATE EXTENSIONS ========== /// |
|
8 |
Date.prototype.equals = function(other) { |
|
9 |
/* compare with other date ignoring time differences */ |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
10 |
if (this.getYear() == other.getYear() && this.getMonth() == other.getMonth() && this.getDate() == other.getDate()) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
11 |
return true; |
0 | 12 |
} |
13 |
return false; |
|
2363 | 14 |
}; |
0 | 15 |
|
16 |
Date.prototype.add = function(days) { |
|
2363 | 17 |
var res = new Date(); |
18 |
res.setTime(this.getTime() + (days * ONE_DAY)); |
|
19 |
return res; |
|
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
20 |
}; |
0 | 21 |
|
22 |
Date.prototype.sub = function(days) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
23 |
return this.add( - days); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
24 |
}; |
0 | 25 |
|
26 |
Date.prototype.iadd = function(days) { |
|
27 |
// in-place add |
|
2363 | 28 |
this.setTime(this.getTime() + (days * ONE_DAY)); |
0 | 29 |
// avoid strange rounding problems !! |
30 |
this.setHours(12); |
|
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
31 |
}; |
0 | 32 |
|
33 |
Date.prototype.isub = function(days) { |
|
34 |
// in-place sub |
|
2363 | 35 |
this.setTime(this.getTime() - (days * ONE_DAY)); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
36 |
}; |
0 | 37 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
38 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
39 |
* .. function:: Date.prototype.nextMonth() |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
40 |
* |
0 | 41 |
* returns the first day of the next month |
42 |
*/ |
|
43 |
Date.prototype.nextMonth = function() { |
|
44 |
if (this.getMonth() == 11) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
45 |
var d = new Date(this.getFullYear() + 1, 0, 1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
46 |
return d; |
0 | 47 |
} else { |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
48 |
var d2 = new Date(this.getFullYear(), this.getMonth() + 1, 1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
49 |
return d2; |
0 | 50 |
} |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
51 |
}; |
0 | 52 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
53 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
54 |
* .. function:: Date.prototype.getRealDay() |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
55 |
* |
0 | 56 |
* returns the day of week, 0 being monday, 6 being sunday |
57 |
*/ |
|
58 |
Date.prototype.getRealDay = function() { |
|
59 |
// getDay() returns 0 for Sunday ==> 6 for Saturday |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
60 |
return (this.getDay() + 6) % 7; |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
61 |
}; |
0 | 62 |
|
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
63 |
Date.prototype.strftime = function(fmt) { |
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
64 |
if (this.toLocaleFormat !== undefined) { // browser dependent |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
65 |
return this.toLocaleFormat(fmt); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
66 |
} |
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
67 |
// XXX implement at least a decent fallback implementation |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
68 |
return this.getFullYear() + '/' + (this.getMonth() + 1) + '/' + this.getDate(); |
2008
640a59bf5c68
[javascript] provide a strftime method to Date objects
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
156
diff
changeset
|
69 |
}; |
0 | 70 |
|
71 |
var _DATE_FORMAT_REGXES = { |
|
72 |
'Y': new RegExp('^-?[0-9]+'), |
|
73 |
'd': new RegExp('^[0-9]{1,2}'), |
|
74 |
'm': new RegExp('^[0-9]{1,2}'), |
|
75 |
'H': new RegExp('^[0-9]{1,2}'), |
|
76 |
'M': new RegExp('^[0-9]{1,2}') |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
77 |
}; |
0 | 78 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
79 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
80 |
* .. function:: _parseDate(datestring, format) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
81 |
* |
0 | 82 |
* _parseData does the actual parsing job needed by `strptime` |
83 |
*/ |
|
84 |
function _parseDate(datestring, format) { |
|
85 |
var skip0 = new RegExp('^0*[0-9]+'); |
|
86 |
var parsed = {}; |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
87 |
for (var i1 = 0, i2 = 0; i1 < format.length; i1++, i2++) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
88 |
var c1 = format.charAt(i1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
89 |
var c2 = datestring.charAt(i2); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
90 |
if (c1 == '%') { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
91 |
c1 = format.charAt(++i1); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
92 |
var data = _DATE_FORMAT_REGXES[c1].exec(datestring.substring(i2)); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
93 |
if (!data.length) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
94 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
95 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
96 |
data = data[0]; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
97 |
i2 += data.length - 1; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
98 |
var value = parseInt(data, 10); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
99 |
if (isNaN(value)) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
100 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
101 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
102 |
parsed[c1] = value; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
103 |
continue; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
104 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
105 |
if (c1 != c2) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
106 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
107 |
} |
0 | 108 |
} |
109 |
return parsed; |
|
110 |
} |
|
111 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
112 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
113 |
* .. function:: strptime(datestring, format) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
114 |
* |
0 | 115 |
* basic implementation of strptime. The only recognized formats |
116 |
* defined in _DATE_FORMAT_REGEXES (i.e. %Y, %d, %m, %H, %M) |
|
117 |
*/ |
|
118 |
function strptime(datestring, format) { |
|
119 |
var parsed = _parseDate(datestring, format); |
|
120 |
if (!parsed) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
121 |
return null; |
0 | 122 |
} |
123 |
// create initial date (!!! year=0 means 1900 !!!) |
|
156 | 124 |
var date = new Date(0, 0, 1, 0, 0); |
0 | 125 |
date.setFullYear(0); // reset to year 0 |
126 |
if (parsed.Y) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
127 |
date.setFullYear(parsed.Y); |
0 | 128 |
} |
129 |
if (parsed.m) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
130 |
if (parsed.m < 1 || parsed.m > 12) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
131 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
132 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
133 |
// !!! month indexes start at 0 in javascript !!! |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
134 |
date.setMonth(parsed.m - 1); |
0 | 135 |
} |
136 |
if (parsed.d) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
137 |
if (parsed.m < 1 || parsed.m > 31) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
138 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
139 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
140 |
date.setDate(parsed.d); |
0 | 141 |
} |
142 |
if (parsed.H) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
143 |
if (parsed.H < 0 || parsed.H > 23) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
144 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
145 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
146 |
date.setHours(parsed.H); |
0 | 147 |
} |
148 |
if (parsed.M) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
149 |
if (parsed.M < 0 || parsed.M > 59) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
150 |
return null; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
151 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
152 |
date.setMinutes(parsed.M); |
0 | 153 |
} |
154 |
return date; |
|
155 |
} |
|
156 |
||
157 |
// ========== END OF DATE EXTENSIONS ========== /// |
|
158 |
// ========== STRING EXTENSIONS ========== // |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
159 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
160 |
* .. function:: String.prototype.startswith(prefix) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
161 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
162 |
* python-like startsWith method for js strings |
0 | 163 |
* >>> |
164 |
*/ |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
165 |
String.prototype.startswith = function(prefix) { |
0 | 166 |
return this.indexOf(prefix) == 0; |
2363 | 167 |
}; |
0 | 168 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
169 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
170 |
* .. function:: String.prototype.endswith(suffix) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
171 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
172 |
* python-like endsWith method for js strings |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
173 |
*/ |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
174 |
String.prototype.endswith = function(suffix) { |
0 | 175 |
var startPos = this.length - suffix.length; |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
176 |
if (startPos < 0) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
177 |
return false; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
178 |
} |
0 | 179 |
return this.lastIndexOf(suffix, startPos) == startPos; |
2363 | 180 |
}; |
0 | 181 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
182 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
183 |
* .. function:: String.prototype.strip() |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
184 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
185 |
* python-like strip method for js strings |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
186 |
*/ |
0 | 187 |
String.prototype.strip = function() { |
188 |
return this.replace(/^\s*(.*?)\s*$/, "$1"); |
|
189 |
}; |
|
190 |
||
6921
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
191 |
/** |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
192 |
* .. function:: String.prototype.rstrip() |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
193 |
* |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
194 |
* python-like rstrip method for js strings |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
195 |
*/ |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
196 |
String.prototype.rstrip = function(str) { |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
197 |
if (!str) { str = '\s' ; } |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
198 |
return this.replace(new RegExp('^(.*?)' + str + '*$'), "$1"); |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
199 |
}; |
c1fdf590712f
[js lib] new string.rstrip method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6880
diff
changeset
|
200 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
201 |
// ========= class factories ========= // |
0 | 202 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
203 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
204 |
* .. function:: makeUnboundMethod(meth) |
0 | 205 |
* |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
206 |
* transforms a function into an unbound method |
0 | 207 |
*/ |
208 |
function makeUnboundMethod(meth) { |
|
209 |
function unboundMeth(self) { |
|
5727
29afb9e715bb
[3.9] api update (mostly js) + add support for FCKEditor in formContents js function
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5658
diff
changeset
|
210 |
var newargs = cw.utils.sliceList(arguments, 1); |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
211 |
return meth.apply(self, newargs); |
0 | 212 |
} |
213 |
unboundMeth.__name__ = meth.__name__; |
|
214 |
return unboundMeth; |
|
215 |
} |
|
216 |
||
217 |
function attachMethodToClass(cls, methname, meth) { |
|
218 |
meth.__name__ = methname; |
|
219 |
// XXX : this is probably bad for memory usage |
|
220 |
cls.__dict__[methname] = meth; |
|
221 |
cls[methname] = makeUnboundMethod(meth); // for the class itself |
|
222 |
cls.prototype[methname] = meth; // for the instance |
|
223 |
} |
|
224 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
225 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
226 |
* .. function:: _isAttrSkipped(attrname) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
227 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
228 |
* simple internal function that tells if the attribute should |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
229 |
* be copied from baseclasses or not |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
230 |
*/ |
0 | 231 |
function _isAttrSkipped(attrname) { |
232 |
var skipped = ['__class__', '__dict__', '__bases__', 'prototype']; |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
233 |
for (var i = 0; i < skipped.length; i++) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
234 |
if (skipped[i] == attrname) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
235 |
return true; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
236 |
} |
0 | 237 |
} |
238 |
return false; |
|
239 |
} |
|
240 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
241 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
242 |
* .. function:: makeConstructor(userctor) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
243 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
244 |
* internal function used to build the class constructor |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
245 |
*/ |
0 | 246 |
function makeConstructor(userctor) { |
247 |
return function() { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
248 |
// this is a proxy to user's __init__ |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
249 |
if (userctor) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
250 |
userctor.apply(this, arguments); |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
251 |
} |
0 | 252 |
}; |
253 |
} |
|
254 |
||
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
255 |
/** |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
256 |
* .. function:: defclass(name, bases, classdict) |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
257 |
* |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
258 |
* this is a js class factory. objects returned by this function behave |
0 | 259 |
* more or less like a python class. The `class` function prototype is |
260 |
* inspired by the python `type` builtin |
|
6880
4be32427b2b9
[book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
261 |
* |
4be32427b2b9
[book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
262 |
* .. Note:: |
4be32427b2b9
[book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
263 |
* |
4be32427b2b9
[book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
264 |
* * methods are _STATICALLY_ attached when the class it created |
4be32427b2b9
[book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5727
diff
changeset
|
265 |
* * multiple inheritance was never tested, which means it doesn't work ;-) |
0 | 266 |
*/ |
267 |
function defclass(name, bases, classdict) { |
|
268 |
var baseclasses = bases || []; |
|
269 |
||
270 |
// this is the static inheritance approach (<=> differs from python) |
|
271 |
var basemeths = {}; |
|
272 |
var reverseLookup = []; |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
273 |
for (var i = baseclasses.length - 1; i >= 0; i--) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
274 |
reverseLookup.push(baseclasses[i]); |
0 | 275 |
} |
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
276 |
reverseLookup.push({ |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
277 |
'__dict__': classdict |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
278 |
}); |
0 | 279 |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
280 |
for (var i = 0; i < reverseLookup.length; i++) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
281 |
var cls = reverseLookup[i]; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
282 |
for (prop in cls.__dict__) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
283 |
// XXX hack to avoid __init__, __bases__... |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
284 |
if (!_isAttrSkipped(prop)) { |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
285 |
basemeths[prop] = cls.__dict__[prop]; |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
286 |
} |
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
287 |
} |
0 | 288 |
} |
289 |
var userctor = basemeths['__init__']; |
|
2385
8fa16dc4b8c8
[js] shrink htmlhelpers, extract massmailing functionality, jslint
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2363
diff
changeset
|
290 |
var constructor = makeConstructor(userctor); |
0 | 291 |
|
292 |
// python-like interface |
|
293 |
constructor.__name__ = name; |
|
294 |
constructor.__bases__ = baseclasses; |
|
295 |
constructor.__dict__ = {}; |
|
296 |
constructor.prototype.__class__ = constructor; |
|
297 |
// make bound / unbound methods |
|
298 |
for (methname in basemeths) { |
|
5658
7b9553a9db65
[ajax] refactor/cleanup low-level ajax functions
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
5186
diff
changeset
|
299 |
attachMethodToClass(constructor, methname, basemeths[methname]); |
0 | 300 |
} |
301 |
||
302 |
return constructor; |
|
303 |
} |