updeep-remeda/lib/freeze.js

56 lines
1.1 KiB
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-19 14:29:06 +00:00
/**
* Deeply freeze a plain javascript object.
*
* If `process.env.NODE_ENV === 'production'`, this returns the original object
* without freezing.
*
* Or if `process.env.UPDEEP_MODE === 'dangerously_never_freeze'`, this returns the original object
* without freezing.
2015-08-19 14:29:06 +00:00
*
* @function
* @sig a -> a
* @param {object} object Object to freeze.
* @return {object} Frozen object, unless in production, then the same object.
*/
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
}
if (process.env.UPDEEP_MODE === 'dangerously_never_freeze') {
return object;
}
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
}
2015-08-19 14:29:06 +00:00
export default freeze;