forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.ts
More file actions
46 lines (45 loc) · 1.57 KB
/
Copy pathobject.ts
File metadata and controls
46 lines (45 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
export function getValidatedObject<T>(source: any, compare: T): Partial<T> {
const result = {};
if (source == null || typeof source !== 'object' || Array.isArray(source)) {
return null;
}
Object.keys(source).forEach((key) => {
const value = source[key];
const compareValue = compare[key];
if (value == null || compareValue == null) {
return;
}
const array1 = Array.isArray(value);
const array2 = Array.isArray(compareValue);
if (array1 || array2) {
if (array1 && array2) {
result[key] = value;
}
} else if (typeof value === 'object' && typeof compareValue === 'object') {
result[key] = getValidatedObject(value, compareValue);
} else if (typeof value === typeof compareValue) {
result[key] = value;
}
});
return result;
}
export function getPreviousObject<T>(source: any, compare: T): Partial<T> {
const result = {};
if (source == null || typeof source !== 'object') {
return null;
}
Object.keys(source).forEach((key) => {
const value = source[key];
const compareValue = compare[key];
if (value == null || compare[key] == null) {
return;
}
// TODO: Array implementation.
if (typeof value === 'object' && typeof compareValue === 'object') {
result[key] = getPreviousObject(value, compareValue);
} else if (value !== compareValue) {
result[key] = compareValue;
}
});
return result;
}