-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (162 loc) · 3.33 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var is = exports
var obj = Object.prototype
var navigator = global.navigator
// reserved words in es3: instanceof null undefined arguments boolean false true function int
// only have is.string and is.object, not is.str and is.obj
// instanceof null undefined arguments boolean false true function int
is.browser = function() {
if (!is.wechatApp()) {
if (navigator && global.window == global) {
return true
}
}
return false
}
// simple modern browser detect
is.h5 = function() {
if (is.browser() && navigator.geolocation) {
return true
}
return false
}
is.mobile = function() {
if (is.browser() && /mobile/i.test(navigator.userAgent)) {
return true
}
return false
}
is.wechatApp = function() {
if ('object' == typeof wx) {
if (wx && is.fn(wx.createVideoContext)) {
// wechat js sdk has no createVideoContext
return true
}
}
return false
}
function _class(val) {
var name = obj.toString.call(val)
// [object Class]
return name.substring(8, name.length - 1).toLowerCase()
}
function _type(val) {
// undefined object boolean number string symbol function
return typeof val
}
function owns(owner, key) {
return obj.hasOwnProperty.call(owner, key)
}
is._class = _class
is._type = _type
is.owns = owns
// not a number
is.nan = function(val) {
return val !== val
}
is.bool = function(val) {
return 'boolean' == _class(val)
}
is.infinite = function(val) {
return val == Infinity || val == -Infinity
}
is.number = function(num) {
return !isNaN(num) && 'number' == _class(num)
}
// integer or decimal
is.iod = function(val) {
if (is.number(val) && !is.infinite(val)) {
return true
}
return false
}
is.decimal = function(val) {
if (is.iod(val)) {
return 0 != val % 1
}
return false
}
is.integer = function(val) {
if (is.iod(val)) {
return 0 == val % 1
}
return false
}
// object or function
is.oof = function(val) {
if (val) {
var tp = _type(val)
return 'object' == tp || 'function' == tp
}
return false
}
// regexp should return object
is.object = function(obj) {
return is.oof(obj) && 'function' != _class(obj)
}
is.hash = is.plainObject = function(hash) {
if (hash) {
if ('object' == _class(hash)) {
// old window is object
if (hash.nodeType || hash.setInterval) {
return false
}
return true
}
}
return false
}
is.undef = function(val) {
return 'undefined' == _type(val)
}
// host function should return function, e.g. alert
is.fn = function(fn) {
return 'function' == _class(fn)
}
is.string = function(str) {
return 'string' == _class(str)
}
// number or string
is.nos = function(val) {
return is.iod(val) || is.string(val)
}
is.array = function(arr) {
return 'array' == _class(arr)
}
is.arraylike = function(arr) {
// window has length for iframe too, but it is not arraylike
if (!is.window(arr) && is.object(arr)) {
var len = arr.length
if (is.integer(len) && len >= 0) {
return true
}
}
return false
}
is.window = function(val) {
if (val && val.window == val) {
return true
}
return false
}
is.empty = function(val) {
if (is.string(val) || is.arraylike(val)) {
return 0 === val.length
}
if (is.hash(val)) {
for (var key in val) {
if (owns(val, key)) {
return false
}
}
}
return true
}
is.element = function(elem) {
if (elem && 1 === elem.nodeType) {
return true
}
return false
}
is.regexp = function(val) {
return 'regexp' == _class(val)
}