Skip to content

Commit

Permalink
feat: add Object.freeze monkeypatcher fn
Browse files Browse the repository at this point in the history
  • Loading branch information
madkarmaa committed Nov 15, 2024
1 parent b4d9aaf commit 1a67632
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/deep-freeze.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Record<string, any>>(obj: T): Readonly<T> {
Object.getOwnPropertyNames(obj).forEach((prop) => {
const value = obj[prop];
if (value && typeof value === 'object') deepFreezeAndSeal(value);
});

originalSeal(obj);
return originalFreeze(obj);
}

Object.freeze = deepFreezeAndSeal;
1 change: 1 addition & 0 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import '../app.css';
import '../deep-freeze';

export const prerender = true;

0 comments on commit 1a67632

Please sign in to comment.