Make Object Immutable Problem
Description
LeetCode Problem 2692.
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:
objis a valid JSON object or array2 <= JSON.stringify(obj).length <= 10^5
Solution
Approach: Recursive read-only proxy
Wrap the object in a Proxy whose set trap always throws, with the message depending on the target: Error Modifying Index: <i> for arrays and Error Modifying: <key> for objects. The thrown value must be a string, not an Error.
Nested values must be protected too, so the get trap wraps any object it returns in another proxy (lazily, at access time, so no deep copy is needed). For arrays, reading one of the seven mutating method names (pop, push, shift, unshift, splice, sort, reverse) returns a proxy of that function whose apply trap throws Error Calling Method: <name>. Non-mutating methods such as map run normally; they read through the proxy, so nothing is changed.
Complexity: Time O(1) extra per property access (wrapping is lazy); space O(1) per accessed nested object.
var makeImmutable = function (obj) {
const mutators = new Set(['pop', 'push', 'shift', 'unshift', 'splice', 'sort', 'reverse']);
const wrap = value => {
if (value === null || typeof value !== 'object') return value;
const isArray = Array.isArray(value);
return new Proxy(value, {
get(target, key, receiver) {
const result = Reflect.get(target, key, receiver);
if (isArray && typeof key === 'string' && mutators.has(key)) {
return new Proxy(result, {
apply() {
throw `Error Calling Method: ${key}`;
},
});
}
return wrap(result);
},
set(_target, key) {
throw isArray
? `Error Modifying Index: ${String(key)}`
: `Error Modifying: ${String(key)}`;
},
});
};
return wrap(obj);
};