-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2692-MakeObjectImmutable.js
More file actions
199 lines (182 loc) · 6.4 KB
/
2692-MakeObjectImmutable.js
File metadata and controls
199 lines (182 loc) · 6.4 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// 2692. Make Object Immutable
// Write a function that takes an object obj and returns a new immutable version of this object.
// An immutable object is an object that can't be altered and will throw an error if any attempt is made to alter it.
// There are three types of error messages that can be produced from this new object.
// Attempting to modify a key on the object will result in this error message: `Error Modifying: ${key}`.
// Attempting to modify an index on an array will result in this error message: `Error Modifying Index: ${index}`.
// Attempting to call a method that mutates an array will result in this error message: `Error Calling Method: ${methodName}`. You may assume the only methods that can mutate an array are ['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse'].
// obj is a valid JSON object or array, meaning it is the output of JSON.parse().
// Note that a string literal should be thrown, not an Error.
// Example 1:
// Input:
// obj = {
// "x": 5
// }
// fn = (obj) => {
// obj.x = 5;
// return obj.x;
// }
// Output: {"value": null, "error": "Error Modifying: x"}
// Explanation: Attempting to modify a key on an object resuts in a thrown error. Note that it doesn't matter that the value was set to the same value as it was before.
// Example 2:
// Input:
// obj = [1, 2, 3]
// fn = (arr) => {
// arr[1] = {};
// return arr[2];
// }
// Output: {"value": null, "error": "Error Modifying Index: 1"}
// Explanation: Attempting to modify an array results in a thrown error.
// Example 3:
// Input:
// obj = {
// "arr": [1, 2, 3]
// }
// fn = (obj) => {
// obj.arr.push(4);
// return 42;
// }
// Output: { "value": null, "error": "Error Calling Method: push"}
// Explanation: Calling a method that can result in a mutation results in a thrown error.
// Example 4:
// Input:
// obj = {
// "x": 2,
// "y": 2
// }
// fn = (obj) => {
// return Object.keys(obj);
// }
// Output: {"value": ["x", "y"], "error": null}
// Explanation: No mutations were attempted so the function returns as normal.
// Constraints:
// obj is a valid JSON object or array
// 2 <= JSON.stringify(obj).length <= 10^5
/**
* @param {Object | Array} obj
* @return {Object | Array} immutable obj
*/
// use Proxy
var makeImmutable = function(obj) {
let newObj
switch (Object.prototype.toString.call(obj)) {
case "[object Object]": // 如果是 Object 递归给 value 加上 proxy
newObj = {}
for(key of Object.keys(obj)) newObj[key] = makeImmutable(obj[key]);
break;
case "[object Array]": // 如果是 Array 递归给 value 加上 proxy
newObj = []
for(let i = 0; i < obj.length; i++) newObj[i] = makeImmutable(obj[i]);
break;
default:
return obj;
}
const handler = {
get(target,p) {
const methods = {
'pop':true,
'push':true,
'shift':true,
'unshift':true,
'splice':true,
'sort':true,
'reverse':true
};
// Attempting to call a method that mutates an array will result in this error message: `Error Calling Method: ${methodName}`.
// You may assume the only methods that can mutate an array are ['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse'].
if(methods[p]) {
return () => {
throw `Error Calling Method: ${p}`
}
}
return target[p]
},
set(target,pos) {
// Attempting to modify an index on an array will result in this error message: `Error Modifying Index: ${index}`.
if(Array.isArray(target)) throw `Error Modifying Index: ${pos}`;
// Attempting to modify a key on the object will result in this error message: `Error Modifying: ${key}`.
throw `Error Modifying: ${pos}`;
}
}
return new Proxy(newObj,handler);
};
// use Proxy + Reflect
var makeImmutable1 = function(obj) {
const proxify = (obj) => {
// 如果是数组,给可以改变数据的方法加上 Proxy
if (Array.isArray(obj)) {
['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse'].forEach(method => {
obj[method] = new Proxy(obj[method], {
apply(target) {
throw `Error Calling Method: ${target.name}`
}
})
})
}
return new Proxy(obj, {
get(target, prop) {
const val = Reflect.get(target, prop);
// 不为对象支持返回值
if (!(typeof val === 'object' && val !== null)) return val;
return proxify(val);
},
set(target, prop) {
const preStr = Array.isArray(target) ? 'Error Modifying Index' : 'Error Modifying';
throw `${preStr}: ${String(prop)}`
}
})
};
return proxify(obj);
};
/**
* const obj = makeImmutable({x: 5});
* obj.x = 6; // throws "Error Modifying x"
*/
try {
const obj = makeImmutable({x: 5});
obj.x = 6; // throws "Error Modifying x"
} catch (err) {
console.log(err) // Error Modifying x
}
try {
const arr = makeImmutable([1, 2, 3]);
arr[1] = {}; // throws "Error Modifying Index: 1"
} catch (err) {
console.log(err) // Error Modifying Index: 1
}
try {
const obj = makeImmutable({"arr": [1, 2, 3]});
obj.arr.push(4); // throws "Error Calling Method: push"
} catch (err) {
console.log(err) // Error Calling Method: push
}
try {
const obj = makeImmutable({"x": 2,"y": 2});
console.log(Object.keys(obj)) // ["x", "y"]
} catch (err) {
console.log(err) //
}
try {
const obj = makeImmutable1({x: 5});
obj.x = 6; // throws "Error Modifying x"
} catch (err) {
console.log(err) // Error Modifying x
}
try {
const arr = makeImmutable1([1, 2, 3]);
arr[1] = {}; // throws "Error Modifying Index: 1"
} catch (err) {
console.log(err) // Error Modifying Index: 1
}
try {
const obj = makeImmutable1({"arr": [1, 2, 3]});
obj.arr.push(4); // throws "Error Calling Method: push"
} catch (err) {
console.log(err) // Error Calling Method: push
}
try {
const obj = makeImmutable1({"x": 2,"y": 2});
console.log(Object.keys(obj)) // ["x", "y"]
} catch (err) {
console.log(err) //
}