packages/store/src/utils/freeze.ts

Summary

Maintainability
A
1 hr
Test Coverage
A
100%
/**
 * Object freeze code
 * https://github.com/jsdf/deep-freeze
 */
export const deepFreeze = (o: any) => {
  Object.freeze(o);

  const oIsFunction = typeof o === 'function';
  const hasOwnProp = Object.prototype.hasOwnProperty;

  Object.getOwnPropertyNames(o).forEach(function (prop) {
    if (
      hasOwnProp.call(o, prop) &&
      (oIsFunction ? prop !== 'caller' && prop !== 'callee' && prop !== 'arguments' : true) &&
      o[prop] !== null &&
      (typeof o[prop] === 'object' || typeof o[prop] === 'function') &&
      !Object.isFrozen(o[prop])
    ) {
      deepFreeze(o[prop]);
    }
  });

  return o;
};