forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
38 lines (36 loc) · 1.14 KB
/
Solution.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
type Obj = Array<any> | Record<any, any>;
function makeImmutable(obj: Obj): Obj {
const arrayHandler: ProxyHandler<Array<any>> = {
set: (_, prop) => {
throw `Error Modifying Index: ${String(prop)}`;
},
};
const objectHandler: ProxyHandler<Record<any, any>> = {
set: (_, prop) => {
throw `Error Modifying: ${String(prop)}`;
},
};
const fnHandler: ProxyHandler<Function> = {
apply: target => {
throw `Error Calling Method: ${target.name}`;
},
};
const fn = ['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse'];
const dfs = (obj: Obj) => {
for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
obj[key] = dfs(obj[key]);
}
}
if (Array.isArray(obj)) {
fn.forEach(f => (obj[f] = new Proxy(obj[f], fnHandler)));
return new Proxy(obj, arrayHandler);
}
return new Proxy(obj, objectHandler);
};
return dfs(obj);
}
/**
* const obj = makeImmutable({x: 5});
* obj.x = 6; // throws "Error Modifying x"
*/