forked from Couto/jquery.parts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.type.js
121 lines (92 loc) · 2.81 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
111
112
113
114
115
116
117
118
119
120
121
/*!
* jQuery JavaScript Library v1.8.3
* http://jquery.com/
*
* Copyright 2013 jQuery Foundation and other contributors
* Released under the MIT license
* http://jquery.org/license
*/
/*
jQuery.type() and other specific type checks
extracted from the jQuery 1.8.3 source
Credit: @sindresorhus, @wibblymat
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 = {},
core_toString = Object.prototype.toString,
core_hasOwn = Object.prototype.hasOwnProperty;
var each = function( obj, callback ) {
var i = 0,
length = obj.length;
for ( ; i < length; ) {
if ( callback.call( obj[ i ], i, obj[ i++ ] ) === false ) {
break;
}
}
return obj;
};
// 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[ core_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 &&
!core_hasOwn.call(obj, "constructor") &&
!core_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 || core_hasOwn.call( obj, key );
};
window.isEmptyObject = function( obj ) {
var name;
for ( name in obj ) {
return false;
}
return true;
};
// Populate the class2type map
each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {
class2type[ "[object " + name + "]" ] = name.toLowerCase();
});
})( window );