-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.ts
More file actions
102 lines (82 loc) · 3.01 KB
/
type.ts
File metadata and controls
102 lines (82 loc) · 3.01 KB
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
export const getType = ((_typeList: string[]) => {
let toStr = Object.prototype.toString
let elemParser = /\[object HTML(.*)\]/
let classToType = {}
for (let name of _typeList) {
classToType[`[object ${name}]`] = name.toLowerCase()
}
return (target: any): string => {
let found: any
let strType = toStr.call(target)
if ((found = classToType[strType])) {
return found
} else if ((found = strType.match(elemParser))) {
return found[1].toLowerCase()
} else {
return "object"
}
}
})(
"Boolean Number String Function Array Date RegExp Undefined Null NodeList Symbol Object".split(
" "
)
)
// Aliases for getType
export const of = getType
export const type = getType
function _isTargetOfType<T>(type: string) {
return (target: any): target is T => getType(target) === type
}
function _isNotTargetOfType(type: string) {
return (target: any): target is unknown => getType(target) !== type
}
export const isBoolean = _isTargetOfType<boolean>("boolean")
export const isNotBoolean = _isNotTargetOfType("boolean")
export const isNumber = _isTargetOfType<number>("number")
export const isNotNumber = _isNotTargetOfType("number")
export const isString = _isTargetOfType<string>("string")
export const isNotString = _isNotTargetOfType("string")
export const isFunction = _isTargetOfType<Function>("function")
export const isNotFunction = _isNotTargetOfType("function")
export const isArray = _isTargetOfType<Array<any>>("array")
export const isNotArray = _isNotTargetOfType("array")
export const isDate = _isTargetOfType<Date>("date")
export const isNotDate = _isNotTargetOfType("date")
export const isRegExp = _isTargetOfType<RegExp>("regexp")
export const isNotRegExp = _isNotTargetOfType("regexp")
export const isUndefined = _isTargetOfType<undefined>("undefined")
export const isNotUndefined = _isNotTargetOfType("undefined")
export const isNull = _isTargetOfType<null>("null")
export const isNotNull = _isNotTargetOfType("null")
export const isNodeList = _isTargetOfType<NodeList>("nodelist")
export const isNotNodeList = _isNotTargetOfType("nodelist")
export const isObject = _isTargetOfType<Object>("object")
export const isNotObject = _isNotTargetOfType("object")
export const isSymbol = _isTargetOfType<Symbol>("symbol")
export const isNotSymbol = _isNotTargetOfType("symbol")
export const isEmpty = (target) => {
switch (getType(target)) {
case "null":
return true
case "undefined":
return true
case "string":
return target === ""
case "object":
return Object.keys(target).length === 0
case "array":
return target.length === 0
case "number":
return isNaN(target)
case "nodelist":
return target.length === 0
default:
return false
}
}
export const isNotEmpty = (target) => !isEmpty(target)
const _elementTestRe = /element$/
export const isElement = (target: any): target is HTMLElement =>
_elementTestRe.test(getType(target))
export const isNotElement = (target: any): target is unknown =>
!isElement(target)