-
Notifications
You must be signed in to change notification settings - Fork 0
/
is-empty.ts
68 lines (66 loc) · 1.56 KB
/
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
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
/**
* @packageDocumentation
* @module @tomato-js/shared
*/
import { isArray, isNil, isObject } from "./is-type";
/**
* 是否为空对象
*
* 脚本举例
* ```
* import { isEmptyObject } from '@tomato-js/shared'
* const node = isEmptyObject({});//true
* ```
*
* @param object - 需要判断的对象
* @returns 是否为没有属性的空对象如{}
*/
export function isEmptyObject(object: any) {
return !Object.keys(object).length;
}
/**
* 是否为空数组
*
* 新增于v0.0.22
*
* 脚本举例
* ```
* import { isEmptyArray } from '@tomato-js/shared';
* const node = isEmptyArray([]);//true
* const node = isEmptyArray([1]);//false
* ```
*
* @param arr - 需要判断的数组
* @returns 是否为没有属性的数组比如[]
*/
export function isEmptyArray(arr: any[]) {
return isArray(arr) && arr.length === 0;
}
/**
* 是否为空对象或空数组
*
* 新增于v0.0.22
*
* 脚本举例
* ```
* import { isEmpty } from '@tomato-js/shared'
* const node = isEmptyArray([]);//true
* const node = isEmptyArray([1]);//false
* ```
*
* @param collection - 需要判断的集合
* @param containNil - 是否需要算上null或undefined,默认为true
* @returns 是否为没有属性的空对象如{},数组如[],undefined,null
*/
export function isEmpty(collection: any, containNil: boolean = true) {
if (containNil && isNil(collection)) {
return true;
}
if (isArray(collection)) {
return isEmptyArray(collection);
}
if (isObject(collection)) {
return isEmptyObject(collection);
}
return false;
}