@contrail/util 1.1.3-alpha.0 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -9,7 +9,7 @@ export declare class ObjectUtil {
|
|
|
9
9
|
static getBySlugs(obj: any, rootSlug: string, slug?: string): any;
|
|
10
10
|
static setByPath(obj: any, path: string, value: any): void;
|
|
11
11
|
static chunk(array: any[], size: number): any[];
|
|
12
|
-
static removeEmptyProperties(
|
|
12
|
+
static removeEmptyProperties<T extends object>(data: T): T;
|
|
13
13
|
static isObject: typeof isObject;
|
|
14
14
|
static mergeDeep: typeof mergeDeep;
|
|
15
15
|
static cloneDeep: typeof cloneDeep;
|
|
@@ -15,7 +15,7 @@ class ObjectUtil {
|
|
|
15
15
|
return value === undefined ? def : value;
|
|
16
16
|
}
|
|
17
17
|
const pathUnits = path.split('.');
|
|
18
|
-
pathUnits.forEach(level => {
|
|
18
|
+
pathUnits.forEach((level) => {
|
|
19
19
|
if (obj) {
|
|
20
20
|
obj = obj[level];
|
|
21
21
|
}
|
|
@@ -55,28 +55,24 @@ class ObjectUtil {
|
|
|
55
55
|
}
|
|
56
56
|
return chunked;
|
|
57
57
|
}
|
|
58
|
-
static removeEmptyProperties(
|
|
59
|
-
if (typeof
|
|
60
|
-
return
|
|
61
|
-
if (Array.isArray(json)) {
|
|
62
|
-
return json
|
|
63
|
-
.map(item => (typeof item === 'object' ? ObjectUtil.removeEmptyProperties(item) : item))
|
|
64
|
-
.filter(item => item !== null && item !== undefined && item !== '');
|
|
58
|
+
static removeEmptyProperties(data) {
|
|
59
|
+
if (data === null || data === undefined || typeof data !== 'object') {
|
|
60
|
+
return data;
|
|
65
61
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
acc[key] = value;
|
|
62
|
+
const isEmpty = (value) => value === null || value === undefined || (typeof value === 'string' && value.trim() === '');
|
|
63
|
+
if (Array.isArray(data)) {
|
|
64
|
+
const cleaned = data
|
|
65
|
+
.map((item) => (typeof item === 'object' && item ? ObjectUtil.removeEmptyProperties(item) : item))
|
|
66
|
+
.filter((item) => !isEmpty(item));
|
|
67
|
+
return cleaned;
|
|
68
|
+
}
|
|
69
|
+
const cleaned = Object.entries(data).reduce((acc, [key, value]) => {
|
|
70
|
+
if (!isEmpty(value)) {
|
|
71
|
+
acc[key] = typeof value === 'object' && value ? ObjectUtil.removeEmptyProperties(value) : value;
|
|
77
72
|
}
|
|
78
73
|
return acc;
|
|
79
74
|
}, {});
|
|
75
|
+
return cleaned;
|
|
80
76
|
}
|
|
81
77
|
}
|
|
82
78
|
exports.ObjectUtil = ObjectUtil;
|