@js-utils-kit/object 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +113 -3
- package/dist/index.d.mts +113 -3
- package/dist/index.mjs +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(t=!1,...n){let r=e=>typeof e==`object`&&!!e,i={};for(let a of n)if(r(a))for(let n of Object.keys(a)){let o=a[n],s=i[n];Array.isArray(o)?i[n]=
|
|
1
|
+
function e(t,n=new WeakSet){if(t===null||typeof t!=`object`&&typeof t!=`function`)return t;let r=t;if(n.has(r))return t;n.add(r);let i=r;for(let t of Reflect.ownKeys(i)){let r=i[t];r!==null&&(typeof r==`object`||typeof r==`function`)&&e(r,n)}return Object.freeze(t)}function t(e=!1,...n){let r=e=>typeof e==`object`&&!!e,i={};for(let a of n)if(r(a))for(let n of Object.keys(a)){let o=a[n],s=i[n];Array.isArray(o)?i[n]=e&&Array.isArray(s)?[...s,...o]:[...o]:r(o)&&!Array.isArray(o)?i[n]=r(s)&&!Array.isArray(s)?t(e,s,o):t(e,{},o):i[n]=o}return i}exports.deepFreeze=e,exports.mergeObj=t;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
|
+
//#region src/deepFreeze.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Recursively freezes an object and all of its nested properties.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* This function performs a deep, immutable freeze:
|
|
7
|
+
* - Safely handles circular references
|
|
8
|
+
* - Freezes symbol and non-enumerable properties
|
|
9
|
+
* - Prevents repeated processing of shared references
|
|
10
|
+
* - Uses `WeakSet` internally to avoid memory leaks
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - The type of the value to freeze
|
|
13
|
+
*
|
|
14
|
+
* @returns The same value, deeply frozen and typed as `Readonly<T>`
|
|
15
|
+
*
|
|
16
|
+
* @example Basic usage
|
|
17
|
+
* ```ts
|
|
18
|
+
* const config = deepFreeze({
|
|
19
|
+
* api: {
|
|
20
|
+
* url: 'https://example.com',
|
|
21
|
+
* timeout: 5000
|
|
22
|
+
* }
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Compile-time error and runtime TypeError
|
|
26
|
+
* config.api.url = 'https://evil.com';
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Freezing arrays
|
|
30
|
+
* ```ts
|
|
31
|
+
* const list = deepFreeze([1, 2, { value: 3 }]);
|
|
32
|
+
*
|
|
33
|
+
* // Runtime TypeError
|
|
34
|
+
* list.push(4);
|
|
35
|
+
*
|
|
36
|
+
* // Runtime TypeError
|
|
37
|
+
* list[2].value = 10;
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example Circular references
|
|
41
|
+
* ```ts
|
|
42
|
+
* const obj: any = { name: 'circle' };
|
|
43
|
+
* obj.self = obj;
|
|
44
|
+
*
|
|
45
|
+
* // Does not throw or recurse infinitely
|
|
46
|
+
* deepFreeze(obj);
|
|
47
|
+
*
|
|
48
|
+
* // Runtime TypeError
|
|
49
|
+
* obj.name = 'x';
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Shared references
|
|
53
|
+
* ```ts
|
|
54
|
+
* const shared = { count: 0 };
|
|
55
|
+
*
|
|
56
|
+
* const state = deepFreeze({
|
|
57
|
+
* a: shared,
|
|
58
|
+
* b: shared
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Runtime TypeError
|
|
62
|
+
* state.a.count = 1;
|
|
63
|
+
*
|
|
64
|
+
* // Runtime TypeError
|
|
65
|
+
* state.b.count = 2;
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example Symbol and non-enumerable properties
|
|
69
|
+
* ```ts
|
|
70
|
+
* const secret = Symbol('secret');
|
|
71
|
+
*
|
|
72
|
+
* const obj = {};
|
|
73
|
+
* Object.defineProperty(obj, 'hidden', {
|
|
74
|
+
* value: { token: '123' },
|
|
75
|
+
* enumerable: false
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* (obj as any)[secret] = { enabled: true };
|
|
79
|
+
*
|
|
80
|
+
* deepFreeze(obj);
|
|
81
|
+
*
|
|
82
|
+
* // Runtime TypeError
|
|
83
|
+
* obj.hidden.token = '456';
|
|
84
|
+
*
|
|
85
|
+
* // Runtime TypeError
|
|
86
|
+
* (obj as any)[secret].enabled = false;
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example Functions with properties
|
|
90
|
+
* ```ts
|
|
91
|
+
* function task() {}
|
|
92
|
+
* (task as any).meta = { active: true };
|
|
93
|
+
*
|
|
94
|
+
* deepFreeze(task);
|
|
95
|
+
*
|
|
96
|
+
* // Runtime TypeError
|
|
97
|
+
* (task as any).meta.active = false;
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function deepFreeze<T>(
|
|
101
|
+
/**
|
|
102
|
+
* The value to deeply freeze.
|
|
103
|
+
*/
|
|
104
|
+
obj: T,
|
|
105
|
+
/**
|
|
106
|
+
* Internal WeakSet used to track visited objects during recursion.
|
|
107
|
+
*
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
visited?: WeakSet<object>): Readonly<T>;
|
|
111
|
+
//#endregion
|
|
1
112
|
//#region src/mergeObj.d.ts
|
|
2
113
|
/**
|
|
3
114
|
* Deeply merges multiple source objects into one.
|
|
@@ -43,7 +154,6 @@ declare function mergeObj(
|
|
|
43
154
|
*
|
|
44
155
|
* @default false
|
|
45
156
|
*/
|
|
46
|
-
appendArray?: boolean, /** One or more objects to deeply merge
|
|
47
|
-
...sources: object[]): object;
|
|
157
|
+
appendArray?: boolean, /** One or more objects to deeply merge */...sources: object[]): object;
|
|
48
158
|
//#endregion
|
|
49
|
-
export { mergeObj };
|
|
159
|
+
export { deepFreeze, mergeObj };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,114 @@
|
|
|
1
|
+
//#region src/deepFreeze.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Recursively freezes an object and all of its nested properties.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* This function performs a deep, immutable freeze:
|
|
7
|
+
* - Safely handles circular references
|
|
8
|
+
* - Freezes symbol and non-enumerable properties
|
|
9
|
+
* - Prevents repeated processing of shared references
|
|
10
|
+
* - Uses `WeakSet` internally to avoid memory leaks
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - The type of the value to freeze
|
|
13
|
+
*
|
|
14
|
+
* @returns The same value, deeply frozen and typed as `Readonly<T>`
|
|
15
|
+
*
|
|
16
|
+
* @example Basic usage
|
|
17
|
+
* ```ts
|
|
18
|
+
* const config = deepFreeze({
|
|
19
|
+
* api: {
|
|
20
|
+
* url: 'https://example.com',
|
|
21
|
+
* timeout: 5000
|
|
22
|
+
* }
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Compile-time error and runtime TypeError
|
|
26
|
+
* config.api.url = 'https://evil.com';
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Freezing arrays
|
|
30
|
+
* ```ts
|
|
31
|
+
* const list = deepFreeze([1, 2, { value: 3 }]);
|
|
32
|
+
*
|
|
33
|
+
* // Runtime TypeError
|
|
34
|
+
* list.push(4);
|
|
35
|
+
*
|
|
36
|
+
* // Runtime TypeError
|
|
37
|
+
* list[2].value = 10;
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example Circular references
|
|
41
|
+
* ```ts
|
|
42
|
+
* const obj: any = { name: 'circle' };
|
|
43
|
+
* obj.self = obj;
|
|
44
|
+
*
|
|
45
|
+
* // Does not throw or recurse infinitely
|
|
46
|
+
* deepFreeze(obj);
|
|
47
|
+
*
|
|
48
|
+
* // Runtime TypeError
|
|
49
|
+
* obj.name = 'x';
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Shared references
|
|
53
|
+
* ```ts
|
|
54
|
+
* const shared = { count: 0 };
|
|
55
|
+
*
|
|
56
|
+
* const state = deepFreeze({
|
|
57
|
+
* a: shared,
|
|
58
|
+
* b: shared
|
|
59
|
+
* });
|
|
60
|
+
*
|
|
61
|
+
* // Runtime TypeError
|
|
62
|
+
* state.a.count = 1;
|
|
63
|
+
*
|
|
64
|
+
* // Runtime TypeError
|
|
65
|
+
* state.b.count = 2;
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @example Symbol and non-enumerable properties
|
|
69
|
+
* ```ts
|
|
70
|
+
* const secret = Symbol('secret');
|
|
71
|
+
*
|
|
72
|
+
* const obj = {};
|
|
73
|
+
* Object.defineProperty(obj, 'hidden', {
|
|
74
|
+
* value: { token: '123' },
|
|
75
|
+
* enumerable: false
|
|
76
|
+
* });
|
|
77
|
+
*
|
|
78
|
+
* (obj as any)[secret] = { enabled: true };
|
|
79
|
+
*
|
|
80
|
+
* deepFreeze(obj);
|
|
81
|
+
*
|
|
82
|
+
* // Runtime TypeError
|
|
83
|
+
* obj.hidden.token = '456';
|
|
84
|
+
*
|
|
85
|
+
* // Runtime TypeError
|
|
86
|
+
* (obj as any)[secret].enabled = false;
|
|
87
|
+
* ```
|
|
88
|
+
*
|
|
89
|
+
* @example Functions with properties
|
|
90
|
+
* ```ts
|
|
91
|
+
* function task() {}
|
|
92
|
+
* (task as any).meta = { active: true };
|
|
93
|
+
*
|
|
94
|
+
* deepFreeze(task);
|
|
95
|
+
*
|
|
96
|
+
* // Runtime TypeError
|
|
97
|
+
* (task as any).meta.active = false;
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function deepFreeze<T>(
|
|
101
|
+
/**
|
|
102
|
+
* The value to deeply freeze.
|
|
103
|
+
*/
|
|
104
|
+
obj: T,
|
|
105
|
+
/**
|
|
106
|
+
* Internal WeakSet used to track visited objects during recursion.
|
|
107
|
+
*
|
|
108
|
+
* @internal
|
|
109
|
+
*/
|
|
110
|
+
visited?: WeakSet<object>): Readonly<T>;
|
|
111
|
+
//#endregion
|
|
1
112
|
//#region src/mergeObj.d.ts
|
|
2
113
|
/**
|
|
3
114
|
* Deeply merges multiple source objects into one.
|
|
@@ -43,7 +154,6 @@ declare function mergeObj(
|
|
|
43
154
|
*
|
|
44
155
|
* @default false
|
|
45
156
|
*/
|
|
46
|
-
appendArray?: boolean, /** One or more objects to deeply merge
|
|
47
|
-
...sources: object[]): object;
|
|
157
|
+
appendArray?: boolean, /** One or more objects to deeply merge */...sources: object[]): object;
|
|
48
158
|
//#endregion
|
|
49
|
-
export { mergeObj };
|
|
159
|
+
export { deepFreeze, mergeObj };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(t=!1,...n){let r=e=>typeof e==`object`&&!!e,i={};for(let a of n)if(r(a))for(let n of Object.keys(a)){let o=a[n],s=i[n];Array.isArray(o)?i[n]=
|
|
1
|
+
function e(t,n=new WeakSet){if(t===null||typeof t!=`object`&&typeof t!=`function`)return t;let r=t;if(n.has(r))return t;n.add(r);let i=r;for(let t of Reflect.ownKeys(i)){let r=i[t];r!==null&&(typeof r==`object`||typeof r==`function`)&&e(r,n)}return Object.freeze(t)}function t(e=!1,...n){let r=e=>typeof e==`object`&&!!e,i={};for(let a of n)if(r(a))for(let n of Object.keys(a)){let o=a[n],s=i[n];Array.isArray(o)?i[n]=e&&Array.isArray(s)?[...s,...o]:[...o]:r(o)&&!Array.isArray(o)?i[n]=r(s)&&!Array.isArray(s)?t(e,s,o):t(e,{},o):i[n]=o}return i}export{e as deepFreeze,t as mergeObj};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@js-utils-kit/object",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Object utilities",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"types": "./dist/index.d.cts",
|
|
31
31
|
"exports": {
|
|
32
32
|
".": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
33
|
+
"require": "./dist/index.cjs",
|
|
34
|
+
"import": "./dist/index.mjs"
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {},
|