-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis.js
120 lines (99 loc) · 2.36 KB
/
is.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
import { nativeRoot, toStringTag } from '../_/setup'
/**
* isUndefined
*/
export const isUndefined = val => val === void 0
/**
* isNull
*/
export const isNull = val => val === null
/**
* check `typeof val` is 'object'
*/
export const isObjectLike = val => !isNull(val) && typeof val === 'object'
/**
* check Boolean object created with `constructor`
*/
const isBooleanObject = val =>
isObjectLike(val) && toStringTag(val) === '[object Boolean]'
/**
* isBoolean
*/
export const isBoolean = val =>
val === true || val === false || isBooleanObject(val)
/**
* check Number object
*/
const isNumberObject = val =>
isObjectLike(val) && toStringTag(val) === '[object Number]'
/**
* isNumber
*/
export const isNumber = val => typeof val === 'number' || isNumberObject(val)
/**
* isFinite
*/
export const isFinite = val => isNumber(val) && nativeRoot.isFinite(val)
/**
* isNaNOnly
*/
export const isNaNOnly = val => isNumber(val) && val !== val
/**
* check String object
*/
const isStringObject = val =>
isObjectLike(val) && toStringTag(val) === '[object String]'
/**
* isString
*/
export const isString = val => typeof val === 'string' || isStringObject(val)
/**
* isObject
*/
export const isObject = val => isObjectLike(val) || isFunction(val)
/**
* isPlainObject
*/
export const isPlainObject = val => {
// check is object
if (!isObjectLike(val) || toStringTag(val) !== '[object Object]') return false
// check is create by Object.create(null)
if (Object.getPrototypeOf(val) === null) return true
// @see https://github.com/bevry/typechecker/blob/f57a045/source/index.ts#L36
// Ugly, but test passing
return val.__proto__ === Object.prototype
}
/**
* isArguments
*/
export const isArguments = val =>
isObjectLike(val) && toStringTag(val) === '[object Arguments]'
/**
* isError
*/
export const isError = val =>
isObjectLike(val) && toStringTag(val) === '[object Error]'
/**
* isFunction
*/
export const isFunction = val => typeof val === 'function'
/**
* isArrayLike
*/
export const isArrayLike = val => {
if (!val || isFunction(val)) return false
return (
isNumber(val.length) &&
val.length > -1 &&
val.length <= Number.MAX_SAFE_INTEGER
)
}
/**
* isArray
*/
export const isArray =
Array.isArray || (val => toStringTag(val) === '[object Array]')
/**
* isSymbol
*/
export const isSymbol = val => typeof val === 'symbol'