-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2692-make-object-immutable.js
More file actions
62 lines (56 loc) · 1.93 KB
/
2692-make-object-immutable.js
File metadata and controls
62 lines (56 loc) · 1.93 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
/**
* 2692. Make Object Immutable
* https://leetcode.com/problems/make-object-immutable/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {Object|Array} obj
* @return {Object|Array} immutable obj
*/
var makeImmutable = function(obj) {
const mutatingMethods = new Set(['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse']);
return new Proxy(obj, {
set(target, key) {
if (Array.isArray(target)) {
throw `Error Modifying Index: ${key}`;
}
throw `Error Modifying: ${key}`;
},
get(target, key) {
const value = target[key];
if (typeof value === 'object' && value !== null) {
return makeImmutable(value);
}
if (typeof value === 'function') {
return new Proxy(value, {
apply(...args) {
if (mutatingMethods.has(key)) {
throw `Error Calling Method: ${key}`;
}
return Reflect.apply(...args);
}
});
}
return value;
}
});
};