-
Notifications
You must be signed in to change notification settings - Fork 1
/
is.ts
113 lines (99 loc) · 2.41 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
import { Func } from './types'
/**
* Check argument is primitive or not
*
* @export
* @param {*} obj
* @returns {boolean}
*/
export function isPrimitive(obj: any): boolean {
return null == obj || 'object' !== typeof obj
}
/**
* To test if the input is a number
*
* @export
* @param {*} testee
* @returns {testee is number}
*/
export function isNumber(testee: unknown): testee is number {
return typeof testee === 'number'
}
/**
* To test if the input is a string
*
* @export
* @param {*} testee
* @returns {testee is number}
*/
export function isString(testee: unknown): testee is string {
return typeof testee === 'string'
}
/**
* To test if the input is a boolean
*
* @export
* @param {*} testee
* @returns {testee is number}
*/
export function isBoolean(testee: unknown): testee is boolean {
return typeof testee === 'boolean'
}
/**
* To test if the input is a function
*
* @export
* @param {*} testee
* @returns {testee is number}
*/
export function isFunction(testee: unknown): testee is Func {
return typeof testee === 'function'
}
/**
* To test if an argument is an Array
*
* @export
* @param {*} testee
* @returns {boolean}
*/
export function isArray<T = any>(testee: unknown): testee is T[] {
if (Array.isArray) {
return Array.isArray(testee)
}
return Object.prototype.toString.call(testee) === '[object Array]'
}
/**
* Check input param is undefined or null
*
* @param {*} testee
* @returns {boolean}
*/
export function isUndefinedOrNull(testee: unknown): testee is (null | undefined) {
return testee === null || testee === undefined
}
/**
* Check input param is "Empty": undefined or null or {}, []
*
* @export
* @param {any} obj
* @returns {boolean}
*/
export function isEmpty(obj: any): boolean {
// null and undefined are "empty"
if (obj == null) return true
// Assume if it has a length property with a non-zero value
// that that property is correct.
if (obj.length > 0) return false
if (obj.length === 0) return true
// If it isn't an object at this point
// it is empty, but it can't be anything *but* empty
// Is it empty? Depends on your application.
if (typeof obj !== 'object') return false
// Otherwise, does it have any properties of its own?
// Note that this doesn't handle
// toString and valueOf enumeration bugs in IE < 9
for (let key in obj) {
if (Object.hasOwnProperty.call(obj, key)) return false
}
return true
}