-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepFreeze.ts
106 lines (85 loc) · 2.82 KB
/
deepFreeze.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (c) 2023 Michael Federczuk
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import type { GenericKey } from "./types";
import { canValueHaveProperties, getPropertyKeys } from "./_internal/utils";
const deepFreezeKeysOfObject = (obj: Record<GenericKey, unknown>, keys: readonly GenericKey[]) => {
for (const key of keys) {
const descriptor: PropertyDescriptor = (Object.getOwnPropertyDescriptor(obj, key) as PropertyDescriptor);
deepFreeze(descriptor.get);
deepFreeze(descriptor.set);
deepFreeze(descriptor.value);
}
};
deepFreeze(deepFreezeKeysOfObject);
const deepFreezePrototypeExcludingConstructor = (prototype: Record<GenericKey, unknown>) => {
const keys: GenericKey[] = getPropertyKeys(prototype)
.filter((key: GenericKey) => (key !== "constructor"));
deepFreezeKeysOfObject(prototype, keys);
};
deepFreeze(deepFreezePrototypeExcludingConstructor);
// eslint-disable-next-line @typescript-eslint/ban-types
const deepFreezeFunctionWithPrototype = <F extends Function>(func: F): Readonly<F> => {
const keys: GenericKey[] = getPropertyKeys(func)
.filter((key: GenericKey) => (key !== "prototype"));
deepFreezePrototypeExcludingConstructor(func.prototype);
for (const key of keys) {
deepFreeze((func as Record<GenericKey, unknown>)[key]);
}
return Object.freeze(func);
};
deepFreeze(deepFreezeFunctionWithPrototype);
/**
* Recursively freezes **arr** and all of its items & other properties.
*
* @param arr The 3-dimensional array to freeze.
*
* @returns **arr**, deeply frozen.
*/
function deepFreeze<T>(arr: readonly (readonly (readonly T[])[])[]): readonly (readonly (readonly Readonly<T>[])[])[];
/**
* Recursively freezes **arr** and all of its items & other properties.
*
* @param arr The 2-dimensional array to freeze.
*
* @returns **arr**, deeply frozen.
*/
function deepFreeze<T>(arr: readonly (readonly T[])[]): readonly (readonly Readonly<T>[])[];
/**
* Recursively freezes **arr** and all of its items & other properties.
*
* @param arr The 1-dimensional array to freeze.
*
* @returns **arr**, deeply frozen.
*/
function deepFreeze<T>(arr: readonly T[]): readonly Readonly<T>[];
/**
* Recursively freezes **obj** and all of its properties.
*
* @param obj The object to freeze.
*
* @returns **obj**, deeply frozen.
*/
function deepFreeze<T>(obj: T): Readonly<T>;
function deepFreeze<T>(obj: T): Readonly<T> {
if (!(canValueHaveProperties(obj))) {
return obj;
}
if ((typeof obj === "function") && ("prototype" in obj)) {
return deepFreezeFunctionWithPrototype(obj);
}
deepFreezeKeysOfObject(
(obj as Record<GenericKey, unknown>),
getPropertyKeys(obj),
);
if ((obj instanceof Map) || (obj instanceof Set)) {
for (const [key, value] of obj.entries()) {
deepFreeze(value);
deepFreeze(key);
}
}
return Object.freeze(obj);
}
deepFreeze(deepFreeze);
export { deepFreeze };