-
Notifications
You must be signed in to change notification settings - Fork 4
/
is-empty.ts
42 lines (39 loc) · 929 Bytes
/
is-empty.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
import { isArrayLike } from './is-Array';
import getType from './get-type';
import isPrototype from './is-Prototype';
import isArguments from './is-Arguments';
import isTypedArray from './is-TypedArray';
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* 检查是否空对象
* @param value
* @category Is
*/
function isEmpty(value: any): boolean {
if (value == null) {
return true;
}
// array
if (
isArrayLike(value) &&
(Array.isArray(value) || typeof value === 'string' || isTypedArray(value) || isArguments(value))
) {
return !value.length;
}
// map
const type = getType(value);
if (type === 'Map' || type === 'Set') {
return !value.size;
}
// object
if (isPrototype(value)) {
return !Object.keys(value).length;
}
for (const key in value) {
if (hasOwnProperty.call(value, key)) {
return false;
}
}
return true;
}
export default isEmpty;