OSDN Git Service

ba646f842127545bb5e0aeb7fae8c99d1d1aa3f9
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 00_builtin.js
1 /**\r
2  * @preserve Copyright 2012-2014 pettanR team.\r
3  * https://sourceforge.jp/projects/pettanr/\r
4  * BSD 3-Clause License\r
5  */\r
6 \r
7 /* \r
8  * ビルトインオブジェクトに拡張したい10のメソッド\r
9  * http://d.hatena.ne.jp/ofk/20080922/1222047483\r
10  */\r
11 \r
12 Function.prototype.apply || (Function.prototype.apply = function (x, y) {\r
13         var a, i, r, j;\r
14         x = x || window;\r
15         y = y || [];\r
16         \r
17         // apply 内で apply を呼んだ場合に備える\r
18         if( x === window ){\r
19                 x.__apply = void 0;\r
20         } else {\r
21                 if( x.constructor && x.constructor.prototype.__apply ){\r
22                         delete x.constructor.prototype.__apply;\r
23                 } else\r
24                 if( x.__apply ) delete x.__apply;\r
25         };\r
26         \r
27         x.__apply = this;\r
28         if (!x.__apply) x.constructor.prototype.__apply = this;\r
29         j = y.length;\r
30         switch (j) {\r
31                 case 0: r = x.__apply(); break;\r
32                 case 1: r = x.__apply(y[0]); break;\r
33                 case 2: r = x.__apply(y[0], y[1]); break;\r
34                 case 3: r = x.__apply(y[0], y[1], y[2]); break;\r
35                 case 4: r = x.__apply(y[0], y[1], y[2], y[3]); break;\r
36                 case 5: r = x.__apply(y[0], y[1], y[2], y[3], y[4]); break;\r
37                 case 6: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5]); break;\r
38                 case 7: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6]); break;\r
39                 case 8: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7]); break;\r
40                 case 9: r = x.__apply(y[0], y[1], y[2], y[3], y[4], y[5], y[6], y[7], y[8]); break;\r
41                 default:\r
42                         a = [];\r
43                         for (i = 0; i < j; ++i)\r
44                                 a[i] = 'y[' + i + ']';\r
45                         //r = eval('x.__apply(' + a.join(',') + ')');\r
46                         // closuer compiler 対策\r
47                         r = (new Function( 'x,y', 'return x.__apply(' + a.join(',') + ')' ))( x, y );\r
48                         break;\r
49         };\r
50         // ie5\r
51         if( x === window ){\r
52                 x.__apply = void 0;\r
53         } else {\r
54                 //alert( typeof x );\r
55                 if( x.constructor && x.constructor.prototype.__apply ){\r
56                         delete x.constructor.prototype.__apply;\r
57                 } else\r
58                 if( x.__apply ) delete x.__apply;\r
59         };\r
60         return r;\r
61 });\r
62 Function.prototype.call || (Function.prototype.call = function () {\r
63         var a = arguments, x = a[0], y = [], i = 1, j = a.length;\r
64         for (; i < j; ++i)\r
65                 y[i - 1] = a[i];\r
66         return this.apply(x, y);\r
67 });\r
68 \r
69 Array.prototype.pop || (Array.prototype.pop = function () {\r
70         var r = this[this.length - 1];\r
71         --this.length;\r
72         return r;\r
73 });\r
74 Array.prototype.push || (Array.prototype.push = function () {\r
75         var a = arguments, i = 0, j = a.length, l = this.length;\r
76         for (; i < j; ++i)\r
77                 this[l + i] = a[i];\r
78         return this.length;\r
79 });\r
80 Array.prototype.shift || (Array.prototype.shift = function () {\r
81         var r = this[0], i = 1, j = this.length;\r
82         for( ; i < j; ++i)\r
83                 this[i - 1] = this[i];\r
84         --this.length;\r
85         return r;\r
86 });\r
87 Array.prototype.unshift || (Array.prototype.unshift = function () {\r
88         var a = arguments, l = a.length, j = this.length += l - 1, i = j;\r
89         for (; i >= l; --i)\r
90                 this[i] = this[i - l];\r
91         for (i = 0; i < l; ++i)\r
92                 this[i] = a[i];\r
93         return j;\r
94 });\r
95 Array.prototype.splice || (Array.prototype.splice = function (x, y) {\r
96         var a = arguments, s = a.length - 2 - y, r = this.slice(x, x + y),i,j;\r
97         if (s > 0) {\r
98                 for (i = this.length - 1, j = x + y; i >= j; --i)\r
99                         this[i + s] = this[i];\r
100         }\r
101         else if (s < 0) {\r
102                 for (i = x + y, j = this.length; i < j; ++i)\r
103                         this[i + s] = this[i];\r
104                 this.length += s;\r
105         }\r
106         for (i = 2, j = a.length; i < j; ++i)\r
107                 this[i - 2 + x] = a[i];\r
108         return r;\r
109 });\r
110 \r
111 /*\r
112  * original\r
113  * JavaScript 1.6, Array.indexOfを下位互換実装する\r
114  * http://www.inazumatv.com/contents/archives/7965\r
115  */\r
116 \r
117 Array.prototype.indexOf || (Array.prototype.indexOf = function( searchElement, fromIndex ){\r
118         var l = this.length >>> 0, i;\r
119         \r
120         if( l === 0 ) return -1;\r
121         \r
122         if( fromIndex ){\r
123             i = fromIndex || 0;\r
124             i = i === -Infinity ? 0 : ( i < 0 ? -i : i ) | 0; // Math.floor\r
125             if( l <= i ) return -1;\r
126         };\r
127 \r
128         for( i = 0 <= i ? i : 0 < l + i ? l + i : 0; i < l; ++i ){\r
129             if( this[ i ] === searchElement ) return i;\r
130         };\r
131         return -1;\r
132         });\r
133 \r
134 \r
135 /*\r
136  * JavaScript split Bugs: Fixed!\r
137  * http://blog.stevenlevithan.com/archives/cross-browser-split\r
138  */\r
139 \r
140 /*\r
141  * Window\r
142  * by https://web.archive.org/web/20100413085309/http://nurucom-archives.hp.infoseek.co.jp/digital/trans-uri.html\r
143  */\r
144 \r
145 /*\r
146  * //\r
147 // TransURI (UTF-8): transURI.js (Ver.041211)\r
148 //\r
149 // Copyright (C) http://nurucom-archives.hp.infoseek.co.jp/digital/\r
150 //\r
151 \r
152 EncodeURI=function(str){\r
153         return str.replace(/[^!#$&-;=?-Z_a-z~]/g,function(s){\r
154                 var c=s.charCodeAt(0);\r
155                 return (c<16?"%0"+c.toString(16):c<128?"%"+c.toString(16):c<2048?"%"+(c>>6|192).toString(16)+"%"+(c&63|128).toString(16):"%"+(c>>12|224).toString(16)+"%"+(c>>6&63|128).toString(16)+"%"+(c&63|128).toString(16)).toUpperCase()\r
156         })\r
157 };\r
158 \r
159 EncodeURIComponent=function(str){\r
160         return str.replace(/[^!'-*.0-9A-Z_a-z~-]/g,function(s){\r
161                 var c=s.charCodeAt(0);\r
162                 return (c<16?'%0'+c.toString(16):c<128?'%'+c.toString(16):c<2048?'%'+(c>>6|192).toString(16)+'%'+(c&63|128).toString(16):'%'+(c>>12|224).toString(16)+'%'+(c>>6&63|128).toString(16)+'%'+(c&63|128).toString(16)).toUpperCase()\r
163         })\r
164 };\r
165 \r
166 DecodeURI=function(str){\r
167         return str.replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig,function(s){\r
168                 var c=parseInt(s.substring(1),16);\r
169                 return String.fromCharCode(c<128?c:c<224?(c&31)<<6|parseInt(s.substring(4),16)&63:((c&15)<<6|parseInt(s.substring(4),16)&63)<<6|parseInt(s.substring(7),16)&63)\r
170         })\r
171 };\r
172  */\r
173 \r
174 /* 正規表現が使われているため、まだ投入しない itozyun\r
175 window.encodeURI || (window.encodeURI = function (x) {\r
176         return ("" + x).replace(/[^!#$&-;=?-Z_a-z~]/g, function (s) {\r
177                 var c = s.charCodeAt(0), p = "%";\r
178                 return (\r
179                         c < 16 ? "%0" + c.toString(16) :\r
180                         c < 128 ? p + c.toString(16) :\r
181                         c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :\r
182                         p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)\r
183                 ).toUpperCase();\r
184         });\r
185 });\r
186 \r
187 window.encodeURIComponent || (window.encodeURIComponent = function (x) {\r
188         return ("" + x).replace(/[^!'-*.0-9A-Z_a-z~-]/g, function (s) {\r
189                 var c = s.charCodeAt(0), p = "%";\r
190                 return (\r
191                         c < 16 ? "%0" + c.toString(16) :\r
192                         c < 128 ? p + c.toString(16) :\r
193                         c < 2048 ? p + (c >> 6 | 192).toString(16) + p + (c & 63 | 128).toString(16) :\r
194                         p + (c >> 12 | 224).toString(16) + p + (c >> 6 & 63 | 128).toString(16) + p + (c & 63 | 128).toString(16)\r
195                 ).toUpperCase();\r
196         });\r
197 });\r
198 \r
199 // 手抜き\r
200 window.decodeURI || (window.decodeURI = function (x) {\r
201         return ("" + x).replace(/%(E(0%[AB]|[1-CEF]%[89AB]|D%[89])[0-9A-F]|C[2-9A-F]|D[0-9A-F])%[89AB][0-9A-F]|%[0-7][0-9A-F]/ig, function (s) {\r
202                 var c = parseInt(s.substring(1), 16);\r
203                 return String.fromCharCode(\r
204                         c < 128 ? c :\r
205                         c < 224 ? (c & 31) << 6 | parseInt(s.substring(4), 16) & 63 :\r
206                         ((c & 15) << 6 | parseInt(s.substring(4), 16) & 63) << 6 | parseInt(s.substring(7), 16) & 63\r
207                 );\r
208         });\r
209 });\r
210 */\r
211 \r
212 \r
213 //window.decodeURIComponent || (window.decodeURIComponent = window.decodeURI);\r
214 \r
215 \r
216 /*\r
217  * String\r
218  */\r
219 \r
220 // replace(RegExp, Function)対応\r
221 /*\r
222 if (window.ActiveXObject ? !Number.prototype.toFixed : (!navigator.taintEnabled && !document.createElement("input").setSelectionRange))\r
223         (function () {\r
224                 var g = String.prototype.replace;\r
225                 String.prototype.replace = function (x, y) {\r
226                         var s = this, z = y;\r
227                         // 第二引数が関数\r
228                         if (y instanceof Function) {\r
229                                 // 第一引数が正規表現\r
230                                 if (x instanceof RegExp) {\r
231                                         // その上、グローバルマッチ\r
232                                         if (x.global || /^\/.*g$/.test(x)) {\r
233                                                 var r = [], m;\r
234                                                 while ((m = x.exec(s)) != null) {\r
235                                                         var i = m.index;\r
236                                                         r[r.length] = s.slice(0, i);\r
237                                                         s = s.slice(i + m[0].length);\r
238                                                         r[r.length] = y.apply(null, m.concat(i, this));\r
239                                                 }\r
240                                                 r[r.length] = s;\r
241                                                 return r.join("");\r
242                                         }\r
243                                         var m = x.exec(s);\r
244                                         if (!m)\r
245                                                 return s;\r
246                                         z = y.apply(null, m.concat(m.index, s));\r
247                                 }\r
248                                 else {\r
249                                         var i = s.indexOf(x);\r
250                                         if (i < 0)\r
251                                                 return s;\r
252                                         z = y(x, i, s);\r
253                                 }\r
254                         }\r
255                         return g.call(s, x, z);\r
256                 };\r
257         })(); */\r
258         \r
259 /*\r
260  * Safari の JavaScript の不備 \r
261  * http://nanto.asablo.jp/blog/2006/01/13/209495\r
262  * \r
263  * web.paulownia.jp - JavaScriptとクロージャ\r
264  * https://web.archive.org/web/20070526063400/http://web.paulownia.jp/script/oop/closure.html\r
265  * MacOSX 10.3のsafariにはhasOwnPropertyが実装されていないので、独自に追加する必要があります。\r
266  * \r
267  * prototype汚染問題でhasOwnPropertyを使わないクロスブラウザな方法\r
268  * http://os0x.hatenablog.com/entry/20080901/1220272509\r
269  */\r
270 /*\r
271 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){\r
272                 var proto = this.constructor && this.constructor.prototype,\r
273                         __p__ = proto && proto.__proto__,\r
274                         v     = this[ p ],\r
275                         r     = false;\r
276                 \r
277                 if( __p__ ) proto.__proto__ = null;\r
278                 \r
279                 if( p in this ){\r
280                         if( v !== v ){\r
281                                 if( proto && ( p in proto ) && proto[ p ] !== proto[ p ] ){ // proto[ p ] is NaN\r
282                                         proto[ p ] = 0; // different value\r
283                                         r = this[ p ] !== this[ p ]; // isNaN?\r
284                                         proto[ p ] = v; // set NaN\r
285                                 } else {\r
286                                         r = true;\r
287                                 };\r
288                         } else\r
289                         if( proto && p in proto && proto[ p ] === v ){\r
290                                 // this と proto に同名で同値が書かれている可能性あり\r
291                                 proto[ p ] = v + ' '; // different value\r
292                                 r = v === this[ p ];\r
293                                 proto[ p ] = v;\r
294                         } else {\r
295                                 r = true;\r
296                         };\r
297                 };\r
298                 \r
299                 if( __p__ ) proto.__proto__ = __p__;\r
300                 \r
301                 return r;\r
302   }); */\r
303 /*\r
304 Object.prototype.hasOwnProperty || (Object.prototype.hasOwnProperty = function( p ){\r
305                 var proto = this.constructor && this.constructor.prototype,\r
306                         __p__ = proto && proto.__proto__,\r
307                         r     = false,//!!( __p__ && ( proto.__proto__ = null ) )\r
308                         _pro_, v, isNaN;\r
309                 \r
310                 if( __p__ ) proto.__proto__ = null;\r
311                 if( this.__proto__ ){\r
312                         _pro_ = this.__proto__;\r
313                         this.__proto__ = null;\r
314                 };\r
315                 \r
316                 if( p === '__proto__' ){\r
317                         r = !!_pro_;\r
318                 } else {\r
319                         v     = this[ p ];\r
320                         isNaN = v !== v;                \r
321                         \r
322                         if( p in this ){\r
323                                 if( proto && p in proto && ( proto[ p ] === v ) ^ isNaN ){ //true + false, false + true\r
324                                         // this と proto に同名で同値が書かれている可能性あり\r
325                                         proto[ p ] = v + ' '; // different value\r
326                                         r = ( v === this[ p ] ) ^ isNaN; // true + false, false + true\r
327                                         proto[ p ] = v;\r
328                                 } else {\r
329                                         r = true;\r
330                                 };\r
331                         };                      \r
332                 };\r
333 \r
334                 if( __p__ ) proto.__proto__ = __p__;\r
335                 if( _p_ ) this.__proto__ = _pro_;\r
336                 return r;\r
337   }); */