updeep-remeda/lib/freeze.js

36 lines
626 B
JavaScript
Raw Normal View History

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