extract deepFreeze function

This commit is contained in:
Niels Lyngsø
2023-02-21 20:20:42 +01:00
parent 34b26141ff
commit ecd8f95bc9
2 changed files with 19 additions and 20 deletions

View File

@@ -0,0 +1,18 @@
export const deepFreeze = Object.freeze(function deepFreezeImpl<T>(inObj: T): T {
if (inObj != null && typeof inObj === 'object') {
Object.freeze(inObj);
Object.getOwnPropertyNames(inObj)?.forEach(function (prop) {
if (
// eslint-disable-next-line no-prototype-builtins
(inObj as any).hasOwnProperty(prop) &&
(inObj as any)[prop] != null &&
typeof (inObj as any)[prop] === 'object' &&
!Object.isFrozen((inObj as any)[prop])
) {
deepFreeze((inObj as any)[prop]);
}
});
}
return inObj;
});

View File

@@ -1,25 +1,6 @@
import { BehaviorSubject } from 'rxjs';
import { createObservablePart } from './create-observable-part.method';
// TODO: Should this handle array as well?
function deepFreeze<T>(inObj: T): T {
if (inObj != null && typeof inObj === 'object') {
Object.freeze(inObj);
Object.getOwnPropertyNames(inObj)?.forEach(function (prop) {
if (
// eslint-disable-next-line no-prototype-builtins
(inObj as any).hasOwnProperty(prop) &&
(inObj as any)[prop] != null &&
typeof (inObj as any)[prop] === 'object' &&
!Object.isFrozen((inObj as any)[prop])
) {
deepFreeze((inObj as any)[prop]);
}
});
}
return inObj;
}
import { deepFreeze } from './deep-freeze.function';
export function naiveObjectComparison(objOne: any, objTwo: any): boolean {
return JSON.stringify(objOne) === JSON.stringify(objTwo);