@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(json: Record<string, any>): Record<string, any>;
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(json) {
59
- if (typeof json !== 'object' || json === null)
60
- return json;
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
- return Object.entries(json).reduce((acc, [key, value]) => {
67
- if (value == null || value === '')
68
- return acc;
69
- if (typeof value === 'object') {
70
- const cleanedValue = ObjectUtil.removeEmptyProperties(value);
71
- if (Array.isArray(value) || Object.keys(cleanedValue).length > 0) {
72
- acc[key] = cleanedValue;
73
- }
74
- }
75
- else {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.3-alpha.0",
3
+ "version": "1.1.3",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",