From ecd8f95bc97e903d362f6f2dff9a0a336d1dad2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Lyngs=C3=B8?= Date: Tue, 21 Feb 2023 20:20:42 +0100 Subject: [PATCH] extract deepFreeze function --- .../observable-api/deep-freeze.function.ts | 18 ++++++++++++++++ .../libs/observable-api/deep-state.ts | 21 +------------------ 2 files changed, 19 insertions(+), 20 deletions(-) create mode 100644 src/Umbraco.Web.UI.Client/libs/observable-api/deep-freeze.function.ts diff --git a/src/Umbraco.Web.UI.Client/libs/observable-api/deep-freeze.function.ts b/src/Umbraco.Web.UI.Client/libs/observable-api/deep-freeze.function.ts new file mode 100644 index 0000000000..6dc099932d --- /dev/null +++ b/src/Umbraco.Web.UI.Client/libs/observable-api/deep-freeze.function.ts @@ -0,0 +1,18 @@ +export const deepFreeze = Object.freeze(function deepFreezeImpl(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; +}); diff --git a/src/Umbraco.Web.UI.Client/libs/observable-api/deep-state.ts b/src/Umbraco.Web.UI.Client/libs/observable-api/deep-state.ts index d7b03f83f3..68933c336f 100644 --- a/src/Umbraco.Web.UI.Client/libs/observable-api/deep-state.ts +++ b/src/Umbraco.Web.UI.Client/libs/observable-api/deep-state.ts @@ -1,25 +1,6 @@ import { BehaviorSubject } from 'rxjs'; import { createObservablePart } from './create-observable-part.method'; - -// TODO: Should this handle array as well? -function deepFreeze(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);