-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
is.ts
142 lines (131 loc) · 3.85 KB
/
is.ts
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
/**
* Checks whether given value's type is one of a few Error or Error-like
* {@link isError}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isError(wat: any): boolean {
switch (Object.prototype.toString.call(wat)) {
case '[object Error]':
return true;
case '[object Exception]':
return true;
case '[object DOMException]':
return true;
default:
return wat instanceof Error;
}
}
/**
* Checks whether given value's type is ErrorEvent
* {@link isErrorEvent}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isErrorEvent(wat: any): boolean {
return Object.prototype.toString.call(wat) === '[object ErrorEvent]';
}
/**
* Checks whether given value's type is DOMError
* {@link isDOMError}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isDOMError(wat: any): boolean {
return Object.prototype.toString.call(wat) === '[object DOMError]';
}
/**
* Checks whether given value's type is DOMException
* {@link isDOMException}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isDOMException(wat: any): boolean {
return Object.prototype.toString.call(wat) === '[object DOMException]';
}
/**
* Checks whether given value's type is a string
* {@link isString}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isString(wat: any): boolean {
return Object.prototype.toString.call(wat) === '[object String]';
}
/**
* Checks whether given value's is a primitive (undefined, null, number, boolean, string)
* {@link isPrimitive}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isPrimitive(wat: any): boolean {
return wat === null || (typeof wat !== 'object' && typeof wat !== 'function');
}
/**
* Checks whether given value's type is an object literal
* {@link isPlainObject}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isPlainObject(wat: any): boolean {
return Object.prototype.toString.call(wat) === '[object Object]';
}
/**
* Checks whether given value's type is an Event instance
* {@link isEvent}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isEvent(wat: any): boolean {
// tslint:disable-next-line:strict-type-predicates
return typeof Event !== 'undefined' && wat instanceof Event;
}
/**
* Checks whether given value's type is an Element instance
* {@link isElement}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isElement(wat: any): boolean {
// tslint:disable-next-line:strict-type-predicates
return typeof Element !== 'undefined' && wat instanceof Element;
}
/**
* Checks whether given value's type is an regexp
* {@link isRegExp}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isRegExp(wat: any): boolean {
return Object.prototype.toString.call(wat) === '[object RegExp]';
}
/**
* Checks whether given value has a then function.
* @param wat A value to be checked.
*/
export function isThenable(wat: any): boolean {
// tslint:disable:no-unsafe-any
return Boolean(wat && wat.then && typeof wat.then === 'function');
// tslint:enable:no-unsafe-any
}
/**
* Checks whether given value's type is a SyntheticEvent
* {@link isSyntheticEvent}.
*
* @param wat A value to be checked.
* @returns A boolean representing the result.
*/
export function isSyntheticEvent(wat: any): boolean {
// tslint:disable-next-line:no-unsafe-any
return isPlainObject(wat) && 'nativeEvent' in wat && 'preventDefault' in wat && 'stopPropagation' in wat;
}