-
Notifications
You must be signed in to change notification settings - Fork 20
/
jquery.type.js
110 lines (85 loc) · 2.65 KB
/
jquery.type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* jQuery JavaScript Library v1.7.2
* http://jquery.com/
*
* Copyright 2011, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*/
/*
jQuery.type() and other specific type checks
extracted from the jQuery 1.7.2 source
Credit: @sindresorhus
Usage:
console.log( isFunction( function(){} ) ); // true
console.log( isFunction( ['a','b'] ) ); // false
console.log( isArray( ['a','b'] ) ); // true
Demo: http://jsfiddle.net/mofle/AzdKq/
See the jQuery API for documentation:
http://api.jquery.com/category/utilities/
*/
(function( window ) {
"use strict";
// [[Class]] -> type pairs
var class2type = {},
classTypes = "Boolean Number String Function Array Date RegExp Object".split(" "),
l = classTypes.length,
classTypeName,
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty;
// See test/unit/core.js for details concerning isFunction.
// Since version 1.3, DOM methods and functions like alert
// aren't supported. They return false on IE (#2968).
window.isFunction = function( obj ) {
return window.type(obj) === "function";
};
window.isArray = Array.isArray || function( obj ) {
return window.type(obj) === "array";
};
window.isWindow = function( obj ) {
return obj != null && obj == obj.window;
};
window.isNumeric = function( obj ) {
return !isNaN( parseFloat(obj) ) && isFinite( obj );
};
window.type = function( obj ) {
return obj == null ?
String( obj ) :
class2type[ toString.call(obj) ] || "object";
};
window.isPlainObject = function( obj ) {
// Must be an Object.
// Because of IE, we also have to check the presence of the constructor property.
// Make sure that DOM nodes and window objects don't pass through, as well
if ( !obj || window.type(obj) !== "object" || obj.nodeType || window.isWindow( obj ) ) {
return false;
}
try {
// Not own constructor property must be Object
if ( obj.constructor &&
!hasOwn.call(obj, "constructor") &&
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
return false;
}
} catch ( e ) {
// IE8,9 Will throw exceptions on certain host objects #9897
return false;
}
// Own properties are enumerated firstly, so to speed up,
// if last one is own, then all properties are own.
var key;
for ( key in obj ) {}
return key === undefined || hasOwn.call( obj, key );
};
window.isEmptyObject = function( obj ) {
for ( var name in obj ) {
return false;
}
return true;
};
// Populate the class2type map
while ( l-- ) {
classTypeName = classTypes[ l ];
class2type[ "[object " + classTypeName + "]" ] = classTypeName.toLowerCase();
}
})( window );