updeep-remeda/lib/freeze.js
2015-08-03 10:37:26 -07:00

36 lines
575 B
JavaScript

function isFreezable(obj) {
if (obj === null) return false;
return Array.isArray(obj) ||
typeof obj === 'object';
}
function needsFreezing(obj) {
return isFreezable(obj) && !Object.isFrozen(obj);
}
function recur(obj) {
Object.freeze(obj);
Object.keys(obj).forEach((key) => {
const value = obj[key];
if (needsFreezing(value)) {
recur(value);
}
});
return obj;
}
export default function freeze(obj) {
if (process.env.NODE_ENV === 'production') {
return obj;
}
if (needsFreezing(obj)) {
recur(obj);
}
return obj;
}