@contrail/util 1.1.10-alpha-2 → 1.1.10-alpha-4

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.
@@ -1 +1 @@
1
- export declare function cloneDeep(obj: any): any;
1
+ export declare function cloneDeep<T>(obj: T): T | null;
@@ -1 +1 @@
1
- export declare function retainOnlyProperties<T, K extends keyof T>(obj: T, keysToKeep: K[]): Pick<T, K>;
1
+ export declare function retainOnlyProperties<T>(obj: T, keysToKeep: string[]): Partial<T>;
@@ -2,12 +2,40 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.retainOnlyProperties = void 0;
4
4
  function retainOnlyProperties(obj, keysToKeep) {
5
+ if (obj === null || obj === undefined) {
6
+ return obj;
7
+ }
8
+ if (typeof obj !== 'object') {
9
+ return obj;
10
+ }
11
+ if (obj instanceof Date) {
12
+ return obj;
13
+ }
14
+ if (isArray(obj)) {
15
+ obj.forEach((item) => retainOnlyProperties(item, keysToKeep));
16
+ }
5
17
  const keepSet = new Set(keysToKeep);
6
18
  for (const key in obj) {
7
19
  if (!keepSet.has(key)) {
8
20
  delete obj[key];
9
21
  }
22
+ else {
23
+ const value = obj[key];
24
+ const isObjectOrArray = value && typeof value === 'object';
25
+ if (!isObjectOrArray) {
26
+ continue;
27
+ }
28
+ if (isArray(value)) {
29
+ value.forEach((item) => retainOnlyProperties(item, keysToKeep));
30
+ }
31
+ else {
32
+ retainOnlyProperties(value, keysToKeep);
33
+ }
34
+ }
10
35
  }
11
36
  return obj;
12
37
  }
13
38
  exports.retainOnlyProperties = retainOnlyProperties;
39
+ function isArray(value) {
40
+ return Array.isArray(value) && value !== null;
41
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.10-alpha-2",
3
+ "version": "1.1.10-alpha-4",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",