From 1a67632aa60b544dd78cd31a2f02bff9d7b463cc Mon Sep 17 00:00:00 2001 From: madkarmaa Date: Fri, 15 Nov 2024 16:08:12 +0100 Subject: [PATCH] feat: add Object.freeze monkeypatcher fn --- src/deep-freeze.ts | 19 +++++++++++++++++++ src/routes/+layout.ts | 1 + 2 files changed, 20 insertions(+) create mode 100644 src/deep-freeze.ts diff --git a/src/deep-freeze.ts b/src/deep-freeze.ts new file mode 100644 index 0000000..5e6405c --- /dev/null +++ b/src/deep-freeze.ts @@ -0,0 +1,19 @@ +const originalFreeze = Object.freeze; +const originalSeal = Object.seal; + +/** + * Recursively freezes and seals an object and all its nested properties. + * @param obj The object to freeze and seal. + * @returns The frozen and sealed object. + */ +function deepFreezeAndSeal>(obj: T): Readonly { + Object.getOwnPropertyNames(obj).forEach((prop) => { + const value = obj[prop]; + if (value && typeof value === 'object') deepFreezeAndSeal(value); + }); + + originalSeal(obj); + return originalFreeze(obj); +} + +Object.freeze = deepFreezeAndSeal; diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index b5d639d..cdab990 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -1,3 +1,4 @@ import '../app.css'; +import '../deep-freeze'; export const prerender = true;