-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy path2692-make-object-immutable.ts
57 lines (50 loc) · 2.26 KB
/
2692-make-object-immutable.ts
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
47
48
49
50
51
52
53
54
55
56
57
type Obj = Array<any> | Record<string, any>;
const makeImmutable = (obj: Obj): Obj => {
// Define the methods that we want to block on our immutable object
const methods = new Set(['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse']);
// Depth-first search function to traverse and handle different types of elements in the object
function dfs(obj: Obj): Obj {
// If the object is null, we just return it directly
if(obj === null) return null;
// Handle function types separately to block mutating methods
if(typeof obj === 'function') {
return new Proxy(obj, {
apply(func: Function, thisArg: any, argumentList: any[]) {
// Block the execution of certain methods
if(methods.has(func.name)) {
throw `Error Calling Method: ${func.name}`;
}
return func.apply(thisArg, argumentList);
}
});
}
// Handle array types
if(Array.isArray(obj)) {
return new Proxy(obj, {
set(_: Obj, prop: string | number | symbol) {
// Block the modification of the array
throw `Error Modifying Index: ${prop.toString()}`;
},
get(obj: Obj, prop: string | number | symbol) {
// Continue the depth-first search for each element in the array
return dfs(obj[prop as keyof typeof obj]);
}
});
}
// If it's not an object, we don't need to do anything special with it, so we return it directly
if(typeof obj !== 'object') return obj;
// Handle object types
return new Proxy(obj, {
set(_: Obj, prop: string | number | symbol) {
// Block the modification of the object
throw `Error Modifying: ${prop.toString()}`;
},
get(obj: Obj, prop: string | number | symbol) {
// Continue the depth-first search for each property of the object
return dfs(obj[prop as keyof typeof obj]);
}
});
}
// Start the depth-first search on the initial object
return dfs(obj);
};