2015-08-02 07:15:52 +00:00
|
|
|
function isFreezable(obj) {
|
2015-08-03 17:37:26 +00:00
|
|
|
if (obj === null) return false;
|
|
|
|
|
2015-08-02 14:06:35 +00:00
|
|
|
return Array.isArray(obj) ||
|
|
|
|
typeof obj === 'object';
|
|
|
|
}
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2015-08-02 14:06:35 +00:00
|
|
|
function needsFreezing(obj) {
|
|
|
|
return isFreezable(obj) && !Object.isFrozen(obj);
|
2015-08-02 07:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function recur(obj) {
|
|
|
|
Object.freeze(obj);
|
|
|
|
|
|
|
|
Object.keys(obj).forEach((key) => {
|
|
|
|
const value = obj[key];
|
2015-08-02 14:06:35 +00:00
|
|
|
if (needsFreezing(value)) {
|
2015-08-02 07:15:52 +00:00
|
|
|
recur(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function freeze(obj) {
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2015-08-02 14:06:35 +00:00
|
|
|
if (needsFreezing(obj)) {
|
2015-08-02 07:15:52 +00:00
|
|
|
recur(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|