updeep-remeda/lib/freeze.js

49 lines
945 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-19 14:29:06 +00:00
/**
* Deeply freeze a plain javascript object.
*
* If `process.env.NODE_ENV === 'production'`, this returns the original object
* witout freezing.
*
* @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
}
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;