OSDN Git Service

e8067de36a29964fb9a3628997c367cdbc79c0ef
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 03_XType.js
1 /*\r
2  * http://pettanr.sourceforge.jp/test/type.html\r
3  * \r
4  * need xua\r
5  */\r
6 \r
7 X.Type = {\r
8         isObject : function( v ){\r
9                 return v && typeof v === 'object'; // typeof null === 'object' に対策\r
10         },\r
11         \r
12         isFunction : function( v ){\r
13                 return typeof v === 'function';\r
14         },\r
15         \r
16         isUnknown : function( v ){\r
17                 return typeof v === 'unknown'; // ie の XHR.open など\r
18         },\r
19         \r
20         isArray :\r
21                 new Function( 'v',\r
22                         X_UA.IE < 5.5 || X_UA.NetFront < 4 ? // netfront3.4 は html に  instanceof をすると error になる\r
23                                 'return v&&v.push===Array.prototype.push' : // win ie5-, MacIE5.2\r
24                         X_UA.IE ?\r
25                                 'return v&&Object.prototype.toString.call(v)==="[object Array]"' :\r
26                                 'return v instanceof Array'\r
27                 ),\r
28                 \r
29         isBoolean : function( v ){\r
30                 return v === true || v === false;\r
31         },\r
32         \r
33         isString : function( v ){\r
34                 return typeof v === 'string'; // v === v + ''; // 文字列の加算は IE で遅いかも。\r
35         },\r
36         \r
37         isNumber : function( v ){\r
38                 return typeof v === 'number'; // v !== v || v + 0 === v;\r
39         },\r
40         \r
41         isFinite : function( v ){\r
42                 return typeof v === 'number' && isFinite( v ); // isFinite( '123' ) === true に対策\r
43         },\r
44         \r
45         isNaN : function( v ){\r
46                 return v !== v; // isNaN( 'NaN' ) === true      に対策\r
47         },\r
48         \r
49         isHTMLElement :\r
50                 new Function( 'v',\r
51                         ( X_UA.IE4 || X_UA.MacIE ) ?\r
52                                 'return v&&v.tagName&&v.insertAdjacentHTML&&true' : // ie4 or MacIE5.23, v.all <- error\r
53                         X_UA.NetFront < 4 ?\r
54                                 'return v&&v.nodeType===1' : // instanceof not a function. netfront3.4 は html に  instanceof をすると error になる\r
55                         window[ 'HTMLElement' ] ?\r
56                                 'return v instanceof HTMLElement' :\r
57                         //window[ 'Element' ] ?\r
58                         //      'return v instanceof Element' : // error @ie8\r
59                                 'return v&&v.appendChild&&v.nodeType===1'\r
60                 ),\r
61         \r
62         /*\r
63          * new Image した場合に HTMLImageElement が作られるブラウザと,そうでないブラウザ(IE8-)がある\r
64          */     \r
65         isImage :\r
66                 function( v ){\r
67                         if( v && v.constructor === window.Image ) return true;\r
68                         if( v && window.HTMLImageElement && v.constructor === window.HTMLImageElement ) return true; // ie6- は constructor が undef、HTMLImageElement が undef なので、HTMLElement の存在確認が必要\r
69                         if( X_UA.WebKit < 525.13 ){ // Safari3-\r
70                                 if( v && v.src !== undefined && v.onload !== undefined && X.Type.isNumber( v.height ) && X.Type.isNumber( v.width ) && X.Type.isBoolean( v.complete ) ){\r
71                                         return true;\r
72                                 };\r
73                         };\r
74                         return false;\r
75                 },\r
76         /*\r
77         isElementCollection : function(v) {\r
78                 return (Object.prototype.toString.call(v) === "[object HTMLCollection]");\r
79         },\r
80         */\r
81         isNull : function( v ){\r
82                 return v === null;\r
83         },\r
84         \r
85         isUndefined : function( v ){\r
86                 return v === void 0;\r
87         }\r
88 };\r
89 \r
90 console.log( 'X.Core.Type' );\r