@helpers4/object 2.0.0-alpha.3 → 2.0.0-alpha.6
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/LICENSE.md +150 -650
- package/README.md +6 -4
- package/api.json +143 -0
- package/examples.json +85 -0
- package/lib/index.d.ts +116 -9
- package/lib/index.js +255 -216
- package/lib/index.js.map +1 -0
- package/package.json +19 -10
package/README.md
CHANGED
|
@@ -12,13 +12,13 @@ A set of helpers for working with URLs.
|
|
|
12
12
|
[https://helpers4.js.org/object](https://helpers4.js.org/object)
|
|
13
13
|
|
|
14
14
|
<!-- AUTOMATIC-METHODS -->
|
|
15
|
-
-
|
|
16
|
-
- set
|
|
17
|
-
- quickCompare
|
|
15
|
+
- deepClone
|
|
18
16
|
- deepCompare
|
|
19
17
|
- deepMerge
|
|
18
|
+
- get
|
|
19
|
+
- quickCompare
|
|
20
20
|
- removeUndefinedNull
|
|
21
|
-
-
|
|
21
|
+
- set
|
|
22
22
|
<!-- /AUTOMATIC-METHODS -->
|
|
23
23
|
|
|
24
24
|
## See
|
|
@@ -27,6 +27,7 @@ A set of helpers for working with URLs.
|
|
|
27
27
|
- [array](../array)
|
|
28
28
|
- [date](../date)
|
|
29
29
|
- [function](../function)
|
|
30
|
+
- [math](../math)
|
|
30
31
|
- [number](../number)
|
|
31
32
|
- [observable](../observable)
|
|
32
33
|
- [promise](../promise)
|
|
@@ -44,6 +45,7 @@ A set of helpers for working with URLs.
|
|
|
44
45
|
| array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
|
|
45
46
|
| date | [@helpers4/date](https://www.npmjs.com/package/@helpers4/date) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/date) | date utilities |
|
|
46
47
|
| function | [@helpers4/function](https://www.npmjs.com/package/@helpers4/function) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/function) | Function utilities and type guards |
|
|
48
|
+
| math | [@helpers4/math](https://www.npmjs.com/package/@helpers4/math) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/math) | math utilities |
|
|
47
49
|
| number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) | number utilities |
|
|
48
50
|
| object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
|
|
49
51
|
| observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
|
package/api.json
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "object",
|
|
3
|
+
"members": [
|
|
4
|
+
{
|
|
5
|
+
"name": "deepClone",
|
|
6
|
+
"kind": "function",
|
|
7
|
+
"description": "Creates a deep copy of an object or array",
|
|
8
|
+
"signature": "deepClone<T>(obj: T): T",
|
|
9
|
+
"params": [
|
|
10
|
+
{
|
|
11
|
+
"name": "obj",
|
|
12
|
+
"type": "T",
|
|
13
|
+
"description": "The object to clone"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"returns": "T"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"name": "deepCompare",
|
|
20
|
+
"kind": "function",
|
|
21
|
+
"description": "Deep comparison of two objects that returns detailed information about differences.",
|
|
22
|
+
"signature": "deepCompare(objA: object | null | undefined, objB: object | null | undefined): boolean | DeepCompareResult",
|
|
23
|
+
"params": [
|
|
24
|
+
{
|
|
25
|
+
"name": "objA",
|
|
26
|
+
"type": "object | null | undefined",
|
|
27
|
+
"description": "First object to compare (can be object, undefined, or null)"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "objB",
|
|
31
|
+
"type": "object | null | undefined",
|
|
32
|
+
"description": "Second object to compare (can be object, undefined, or null)"
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"returns": "boolean | DeepCompareResult"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"name": "DeepCompareResult",
|
|
39
|
+
"kind": "interface",
|
|
40
|
+
"description": "Result type for deep comparison when objects are not identical"
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"name": "deepMerge",
|
|
44
|
+
"kind": "function",
|
|
45
|
+
"description": "Merges two or more objects deeply",
|
|
46
|
+
"signature": "deepMerge<T>(target: T, sources: Record<string, any>[]): T",
|
|
47
|
+
"params": [
|
|
48
|
+
{
|
|
49
|
+
"name": "target",
|
|
50
|
+
"type": "T",
|
|
51
|
+
"description": "The target object"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"name": "sources",
|
|
55
|
+
"type": "Record<string, any>[]",
|
|
56
|
+
"description": "The source objects to merge"
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
"returns": "T"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"name": "get",
|
|
63
|
+
"kind": "function",
|
|
64
|
+
"description": "Gets a value from an object using a dot-notated path",
|
|
65
|
+
"signature": "get<T>(obj: any, path: string, defaultValue: T): T | undefined",
|
|
66
|
+
"params": [
|
|
67
|
+
{
|
|
68
|
+
"name": "obj",
|
|
69
|
+
"type": "any",
|
|
70
|
+
"description": "The object to get value from"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
"name": "path",
|
|
74
|
+
"type": "string",
|
|
75
|
+
"description": "The dot-notated path (e.g., 'a.b.c')"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"name": "defaultValue",
|
|
79
|
+
"type": "T",
|
|
80
|
+
"description": "Default value if path doesn't exist"
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"returns": "T | undefined"
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"name": "quickCompare",
|
|
87
|
+
"kind": "function",
|
|
88
|
+
"description": "Quick comparison of two objects using JSON.stringify.\nThis is a fast but simple comparison that may not work for all edge cases.",
|
|
89
|
+
"signature": "quickCompare(objA: unknown, objB: unknown): boolean",
|
|
90
|
+
"params": [
|
|
91
|
+
{
|
|
92
|
+
"name": "objA",
|
|
93
|
+
"type": "unknown",
|
|
94
|
+
"description": "First object to compare"
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"name": "objB",
|
|
98
|
+
"type": "unknown",
|
|
99
|
+
"description": "Second object to compare"
|
|
100
|
+
}
|
|
101
|
+
],
|
|
102
|
+
"returns": "boolean"
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"name": "removeUndefinedNull",
|
|
106
|
+
"kind": "function",
|
|
107
|
+
"description": "Remove null and undefined values from an object.",
|
|
108
|
+
"signature": "removeUndefinedNull<T>(obj: T): Partial<T>",
|
|
109
|
+
"params": [
|
|
110
|
+
{
|
|
111
|
+
"name": "obj",
|
|
112
|
+
"type": "T",
|
|
113
|
+
"description": "an object"
|
|
114
|
+
}
|
|
115
|
+
],
|
|
116
|
+
"returns": "Partial<T>"
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
"name": "set",
|
|
120
|
+
"kind": "function",
|
|
121
|
+
"description": "Sets a value in an object using a dot-notated path",
|
|
122
|
+
"signature": "set(obj: Record<string, any>, path: string, value: any): Record<string, any>",
|
|
123
|
+
"params": [
|
|
124
|
+
{
|
|
125
|
+
"name": "obj",
|
|
126
|
+
"type": "Record<string, any>",
|
|
127
|
+
"description": "The object to set value in"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"name": "path",
|
|
131
|
+
"type": "string",
|
|
132
|
+
"description": "The dot-notated path (e.g., 'a.b.c')"
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
"name": "value",
|
|
136
|
+
"type": "any",
|
|
137
|
+
"description": "The value to set"
|
|
138
|
+
}
|
|
139
|
+
],
|
|
140
|
+
"returns": "Record<string, any>"
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
package/examples.json
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "object",
|
|
3
|
+
"helpers": [
|
|
4
|
+
{
|
|
5
|
+
"helper": "deepClone",
|
|
6
|
+
"examples": [
|
|
7
|
+
{
|
|
8
|
+
"title": "Clone a nested object",
|
|
9
|
+
"description": "Creates a deep copy — modifying the clone does not affect the original.",
|
|
10
|
+
"code": "const original = { a: { b: 1 } };\nconst cloned = deepClone(original);\ncloned.a.b = 2;\n// original.a.b is still 1"
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"helper": "deepCompare",
|
|
16
|
+
"examples": [
|
|
17
|
+
{
|
|
18
|
+
"title": "Compare nested objects",
|
|
19
|
+
"description": "Deeply compares two objects, returning true when they are structurally equal.",
|
|
20
|
+
"code": "deepCompare({ a: { b: 1 } }, { a: { b: 1 } })\n// => true"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"title": "Detect deep differences",
|
|
24
|
+
"description": "Returns a detailed diff object when nested values differ.",
|
|
25
|
+
"code": "deepCompare({ a: { b: 1 } }, { a: { b: 2 } })\n// => { a: { b: false } }"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"helper": "deepMerge",
|
|
31
|
+
"examples": [
|
|
32
|
+
{
|
|
33
|
+
"title": "Merge two objects deeply",
|
|
34
|
+
"description": "Recursively merges source properties into the target object.",
|
|
35
|
+
"code": "deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 }, e: 4 })\n// => { a: 1, b: { c: 2, d: 3 }, e: 4 }"
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"helper": "get",
|
|
41
|
+
"examples": [
|
|
42
|
+
{
|
|
43
|
+
"title": "Access a nested property",
|
|
44
|
+
"description": "Uses a dot-notated path to retrieve a deeply nested value.",
|
|
45
|
+
"code": "get({ a: { b: { c: 42 } } }, 'a.b.c')\n// => 42"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"title": "Return default for missing path",
|
|
49
|
+
"description": "Returns the default value when the path does not exist.",
|
|
50
|
+
"code": "get({ a: 1 }, 'b.c', 'default')\n// => 'default'"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"helper": "quickCompare",
|
|
56
|
+
"examples": [
|
|
57
|
+
{
|
|
58
|
+
"title": "Compare two equal objects",
|
|
59
|
+
"description": "Uses JSON.stringify for a fast comparison.",
|
|
60
|
+
"code": "quickCompare({ a: 1, b: 2 }, { a: 1, b: 2 })\n// => true"
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"helper": "removeUndefinedNull",
|
|
66
|
+
"examples": [
|
|
67
|
+
{
|
|
68
|
+
"title": "Strip null and undefined values",
|
|
69
|
+
"description": "Returns a shallow copy of the object without null or undefined properties.",
|
|
70
|
+
"code": "removeUndefinedNull({ a: 1, b: null, c: undefined, d: 'ok' })\n// => { a: 1, d: 'ok' }"
|
|
71
|
+
}
|
|
72
|
+
]
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"helper": "set",
|
|
76
|
+
"examples": [
|
|
77
|
+
{
|
|
78
|
+
"title": "Set a nested property",
|
|
79
|
+
"description": "Creates intermediate objects as needed along the dot-notated path.",
|
|
80
|
+
"code": "set({}, 'a.b.c', 42)\n// => { a: { b: { c: 42 } } }"
|
|
81
|
+
}
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,117 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of helpers4.
|
|
3
|
+
* Copyright (C) 2025 baxyz
|
|
4
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Creates a deep copy of an object or array
|
|
8
|
+
* @param obj - The object to clone
|
|
9
|
+
* @returns Deep cloned object
|
|
10
|
+
*/
|
|
11
|
+
declare function deepClone<T>(obj: T): T;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This file is part of helpers4.
|
|
15
|
+
* Copyright (C) 2025 baxyz
|
|
16
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* Result type for deep comparison when objects are not identical
|
|
20
|
+
*/
|
|
21
|
+
interface DeepCompareResult {
|
|
22
|
+
[key: string]: "onlyA" | "onlyB" | false | DeepCompareResult;
|
|
6
23
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Deep comparison of two objects that returns detailed information about differences.
|
|
26
|
+
*
|
|
27
|
+
* @param objA - First object to compare (can be object, undefined, or null)
|
|
28
|
+
* @param objB - Second object to compare (can be object, undefined, or null)
|
|
29
|
+
* @returns `true` if objects are identical, `false` if incompatible types, or a `DeepCompareResult` object detailing differences
|
|
30
|
+
*/
|
|
31
|
+
declare function deepCompare(objA: object | undefined | null, objB: object | undefined | null): true | false | DeepCompareResult;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* This file is part of helpers4.
|
|
35
|
+
* Copyright (C) 2025 baxyz
|
|
36
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Merges two or more objects deeply
|
|
40
|
+
* @param target - The target object
|
|
41
|
+
* @param sources - The source objects to merge
|
|
42
|
+
* @returns The merged object
|
|
43
|
+
*/
|
|
44
|
+
declare function deepMerge<T extends Record<string, any>>(target: T, ...sources: Record<string, any>[]): T;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* This file is part of helpers4.
|
|
48
|
+
* Copyright (C) 2025 baxyz
|
|
49
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Gets a value from an object using a dot-notated path
|
|
53
|
+
* @param obj - The object to get value from
|
|
54
|
+
* @param path - The dot-notated path (e.g., 'a.b.c')
|
|
55
|
+
* @param defaultValue - Default value if path doesn't exist
|
|
56
|
+
* @returns The value at the path or default value
|
|
57
|
+
*/
|
|
58
|
+
declare function get<T = any>(obj: any, path: string, defaultValue?: T): T | undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* This file is part of helpers4.
|
|
62
|
+
* Copyright (C) 2025 baxyz
|
|
63
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Quick comparison of two objects using JSON.stringify.
|
|
67
|
+
* This is a fast but simple comparison that may not work for all edge cases.
|
|
68
|
+
*
|
|
69
|
+
* @param objA - First object to compare
|
|
70
|
+
* @param objB - Second object to compare
|
|
71
|
+
* @returns `true` if objects are identical according to JSON.stringify, `false` otherwise
|
|
72
|
+
*/
|
|
73
|
+
declare function quickCompare(objA: unknown, objB: unknown): boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* This file is part of helpers4.
|
|
77
|
+
* Copyright (C) 2025 baxyz
|
|
78
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
79
|
+
*/
|
|
80
|
+
/**
|
|
81
|
+
* Remove null and undefined values from an object.
|
|
82
|
+
*
|
|
83
|
+
* @param obj an object
|
|
84
|
+
* @returns A shallow copy of the object without null or undefined values
|
|
85
|
+
*/
|
|
86
|
+
declare function removeUndefinedNull<T extends Record<string, string | number | boolean | null | undefined>>(obj: T): Partial<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Remove null and undefined values from an object.
|
|
89
|
+
*
|
|
90
|
+
* @param obj a null object
|
|
91
|
+
* @returns null
|
|
92
|
+
*/
|
|
93
|
+
declare function removeUndefinedNull(obj: null): null;
|
|
94
|
+
/**
|
|
95
|
+
* Remove null and undefined values from an object.
|
|
96
|
+
*
|
|
97
|
+
* @param obj an undefined object
|
|
98
|
+
* @returns undefined
|
|
99
|
+
*/
|
|
100
|
+
declare function removeUndefinedNull(obj: undefined): undefined;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* This file is part of helpers4.
|
|
104
|
+
* Copyright (C) 2025 baxyz
|
|
105
|
+
* SPDX-License-Identifier: LGPL-3.0-or-later
|
|
106
|
+
*/
|
|
107
|
+
/**
|
|
108
|
+
* Sets a value in an object using a dot-notated path
|
|
109
|
+
* @param obj - The object to set value in
|
|
110
|
+
* @param path - The dot-notated path (e.g., 'a.b.c')
|
|
111
|
+
* @param value - The value to set
|
|
112
|
+
* @returns The modified object
|
|
113
|
+
*/
|
|
114
|
+
declare function set(obj: Record<string, any>, path: string, value: any): Record<string, any>;
|
|
115
|
+
|
|
116
|
+
export { deepClone, deepCompare, deepMerge, get, quickCompare, removeUndefinedNull, set };
|
|
117
|
+
export type { DeepCompareResult };
|