-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.js
199 lines (164 loc) · 5.38 KB
/
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
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
196
197
198
199
/*! lil-type - v0.1.2 - MIT License - https://github.com/lil-js/type */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['exports'], factory)
} else if (typeof exports === 'object') {
factory(exports)
if (typeof module === 'object' && module !== null) {
module.exports = exports
}
} else {
factory((root.lil = root.lil || {}))
}
}(this, function (exports) {
'use strict'
var VERSION = '0.1.2'
var toStr = Object.prototype.toString
var nativeIsFinite = isFinite
var nativeIsArray = Array.isArray
var keys = Object.keys
var binaryRegex = /[U]?Int|Float[0-9]{1,2}Array\]$/i
var types = [
'Boolean', 'NaN', 'Number', 'String', 'Null',
'Undefined', 'RegExp', 'Date', 'Function', 'Symbol',
'Arguments', 'Error', 'Array', 'Element',
'Generator', 'Map', 'WeakMap', 'WeakSet',
'Binary', 'Object'
]
exports.type = { VERSION: VERSION }
function isNull(o) {
return o === null
}
exports.isNull = isNull
function isObject(o) {
return o && toStr.call(o) === '[object Object]' || false
}
exports.isObject = isObject
exports.isPlainObject = function isPlainObject(o) {
return isObject(o)
&& isObject(Object.getPrototypeOf(o))
&& isNull(Object.getPrototypeOf(Object.getPrototypeOf(o)))
}
function isArray(o) {
return nativeIsArray ? nativeIsArray(o) : (o && toStr.call(o) === '[object Array]') || false
}
exports.isArray = isArray
function isTypedArray(o) {
return o && binaryRegex.test(toStr.call(o)) || false
}
exports.isTypedArray = isTypedArray
function isFn(fn) {
return typeof fn === 'function'
}
exports.isFn = exports.isFunction = isFn
function isBool(o) {
return o === true || o === false || o && toStr.call(o) === '[object Boolean]' || false
}
exports.isBool = exports.isBoolean = isBool
function isDate(o) {
return o && toStr.call(o) === '[object Date]' || false
}
exports.isDate = isDate
exports.isElement = function isElement(o) {
return o && o.nodeType === 1 || false
}
function isString(o) {
return typeof o === 'string' ||
o && typeof o === 'object' && toStr.call(o) === '[object String]' || false
}
exports.isString = isString
function isNumber(o) {
return typeof o === 'number' ||
o && typeof o === 'object' && toStr.call(o) === '[object Number]' || false
}
exports.isNumber = isNumber
function isRegExp(o) {
return o && toStr.call(o) === '[object RegExp]' || false
}
exports.isRegExp = isRegExp
function isNaN(o) {
return isNumber(o) && o != +o
}
exports.isNaN = isNaN
exports.isFinite = function isFinite(o) {
return nativeIsFinite(o) && !isNaN(parseFloat(o)) || false
}
function isError(o) {
return o && toStr.call(o).indexOf('Error') !== -1 || false
}
exports.isError = isError
var isMap = exports.isMap = function isMap(o) {
return o && toStr.call(o) === '[object Map]' || false
}
var isWeakMap = exports.isWeakMap = isMapType('WeakMap')
var isWeakSet = exports.isWeakSet = isMapType('WeakSet')
function isMapType(type) {
return function isMapType(o) {
return o && toStr.call(o) === '[object ' + type + ']' || false
}
}
exports.isPromise = function isPromise(o) {
return isObject(o) && isFn(o.then) || false
}
exports.isGenerator = function isGenerator(o) {
return isObject(o) && isFn(o.next) && isFn(o.send) || false
}
exports.isBuffer = function isBuffer(o) {
return o && toStr.call(o) === '[object Buffer]'
|| toStr.call(o) === '[object ArrayBuffer]'
|| toStr.call(o) === '[object DataView]' || false
}
function isBlob(o) {
return o && toStr.call(o) === '[object Blob]' || toStr.call(o) === '[object BlobBuilder]' || false
}
exports.isBlob = isBlob
function isFile(o) {
return o && toStr.call(o) === '[object File]' || toStr.call(o) === '[object FileReader]' || false
}
exports.isBlob = isFile
exports.isBinary = function isBinary(o) {
return o && isBlob(o) || isFile(o) || isTypedArray(o) || false
}
function isUndefined(o) {
return typeof o === 'undefined'
}
exports.isUndefined = isUndefined
function isSymbol(o) {
return o && toStr.call(o) === '[object Symbol]' || false
}
exports.isSymbol = isSymbol
function isArguments(o) {
return o && toStr.call(o) === '[object Arguments]' || false
}
exports.isArguments = isArguments
function isEmpty(o) {
if (!o) return true
if (isString(o) || isArray(o)) return o.length === 0
if (isObject(o)) return keys(o).length === 0
return false
}
exports.isEmpty = isEmpty
exports.notEmpty = function (o) {
return !isEmpty(o)
}
exports.isMutable = function isMutable(o) {
return (isObject(o) && !Object.isFrozen(o))
|| isArray(o) || isError(o) || isArguments(o) || isDate(o) || isFn(o) || false
}
exports.isIterable = function isIterable(o) {
return isObject(o) || isArray(o) || isArguments(o) || isMap(o) || isWeakMap(o) || isWeakSet(o) || false
}
exports.isPrimitive = function (o) {
return isBool(o) || isString(o) || isNumber(o)
|| isFn(o) || isNull(o) || isUndefined(o)
|| isRegExp(o) || isSymbol(o) || false
}
exports.is = exports.isType = function isType(o) {
for (var i = 0, l = types.length; i < l; i += 1) {
if (exports['is' + types[i]](o)) {
return types[i].toLowerCase()
}
}
return 'undefined'
}
}))