@contrail/util 1.1.10-alpha-4 → 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,2 @@
|
|
|
1
|
-
export declare function retainOnlyProperties<T>(obj: T, keysToKeep:
|
|
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]>;
|
|
@@ -2,40 +2,24 @@
|
|
|
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
|
-
}
|
|
17
5
|
const keepSet = new Set(keysToKeep);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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);
|
|
21
12
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (!isObjectOrArray) {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
if (isArray(value)) {
|
|
29
|
-
value.forEach((item) => retainOnlyProperties(item, keysToKeep));
|
|
13
|
+
for (const key in target) {
|
|
14
|
+
if (!keepSet.has(key)) {
|
|
15
|
+
delete target[key];
|
|
30
16
|
}
|
|
31
17
|
else {
|
|
32
|
-
|
|
18
|
+
target[key] = retain(target[key]);
|
|
33
19
|
}
|
|
34
20
|
}
|
|
35
|
-
|
|
36
|
-
|
|
21
|
+
return target;
|
|
22
|
+
};
|
|
23
|
+
return retain(obj);
|
|
37
24
|
}
|
|
38
25
|
exports.retainOnlyProperties = retainOnlyProperties;
|
|
39
|
-
function isArray(value) {
|
|
40
|
-
return Array.isArray(value) && value !== null;
|
|
41
|
-
}
|