tools.js
1.45 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
/**
* String type check
*/
exports.isStr = (v) => typeof v === 'string'
/**
* Number type check
*/
exports.isNum = (v) => typeof v === 'number'
/**
* Array type check
*/
exports.isArr = Array.isArray
/**
* undefined type check
*/
exports.isUndef = (v) => v === undefined
exports.isTrue = (v) => v === true
exports.isFalse = (v) => v === false
/**
* Function type check
*/
exports.isFunc = (v) => typeof v === 'function'
/**
* Quick object check - this is primarily used to tell
* Objects from primitive values when we know the value
* is a JSON-compliant type.
*/
exports.isObj = exports.isObject = (obj) => {
return obj !== null && typeof obj === 'object'
}
/**
* Strict object type check. Only returns true
* for plain JavaScript objects.
*/
const _toString = Object.prototype.toString
exports.isPlainObject = (obj) => {
return _toString.call(obj) === '[object Object]'
}
/**
* Check whether the object has the property.
*/
const hasOwnProperty = Object.prototype.hasOwnProperty
exports.hasOwn = (obj, key) => {
return hasOwnProperty.call(obj, key)
}
/**
* Perform no operation.
* Stubbing args to make Flow happy without leaving useless transpiled code
* with ...rest (https://flow.org/blog/2017/05/07/Strict-Function-Call-Arity/)
*/
exports.noop = (a, b, c) => {}
/**
* Check if val is a valid array index.
*/
exports.isValidArrayIndex = (val) => {
const n = parseFloat(String(val))
return n >= 0 && Math.floor(n) === n && isFinite(val)
}