@contrail/util 1.1.10-alpha-2 → 1.1.10-alpha-5
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:
|
|
1
|
+
export declare function cloneDeep<T>(obj: T): T | null;
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export declare function retainOnlyProperties<T, K extends
|
|
1
|
+
export declare function retainOnlyProperties<T extends Record<string, any>, K extends readonly string[]>(obj: T[], keysToKeep: K): Array<Pick<T, K[number]>>;
|
|
2
|
+
export declare function retainOnlyProperties<T extends Record<string, any>, K extends readonly string[]>(obj: T, keysToKeep: K): Pick<T, K[number]>;
|
|
@@ -3,11 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.retainOnlyProperties = void 0;
|
|
4
4
|
function retainOnlyProperties(obj, keysToKeep) {
|
|
5
5
|
const keepSet = new Set(keysToKeep);
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const shouldReturnAsIs = (value) => value === null || value === undefined || typeof value !== 'object' || value instanceof Date;
|
|
7
|
+
const retain = (target) => {
|
|
8
|
+
if (shouldReturnAsIs(target))
|
|
9
|
+
return target;
|
|
10
|
+
if (Array.isArray(target)) {
|
|
11
|
+
return target.map(retain);
|
|
9
12
|
}
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
for (const key in target) {
|
|
14
|
+
if (!keepSet.has(key)) {
|
|
15
|
+
delete target[key];
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
target[key] = retain(target[key]);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return target;
|
|
22
|
+
};
|
|
23
|
+
return retain(obj);
|
|
12
24
|
}
|
|
13
25
|
exports.retainOnlyProperties = retainOnlyProperties;
|