updeep-remeda/lib/freeze.js

34 lines
540 B
JavaScript
Raw Normal View History

2015-08-02 07:15:52 +00:00
function isFreezable(obj) {
return Array.isArray(obj) ||
typeof obj === 'object';
}
2015-08-02 07:15:52 +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];
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;
}
if (needsFreezing(obj)) {
2015-08-02 07:15:52 +00:00
recur(obj);
}
return obj;
}