@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/lib/index.js CHANGED
@@ -1,231 +1,270 @@
1
- // helpers/object/get.ts
2
- function get(obj, path, defaultValue) {
3
- const keys = path.split(".");
4
- let result = obj;
5
- for (const key of keys) {
6
- if (result == null || typeof result !== "object") {
7
- return defaultValue;
8
- }
9
- result = result[key];
10
- }
11
- return result !== undefined ? result : defaultValue;
12
- }
13
- // helpers/object/set.ts
14
- function set(obj, path, value) {
15
- const keys = path.split(".");
16
- let current = obj;
17
- for (let i = 0;i < keys.length - 1; i++) {
18
- const key = keys[i];
19
- if (!(key in current) || typeof current[key] !== "object" || current[key] === null) {
20
- current[key] = {};
21
- }
22
- current = current[key];
23
- }
24
- current[keys[keys.length - 1]] = value;
25
- return obj;
26
- }
27
- // helpers/object/quickCompare.ts
28
- function quickCompare(objA, objB) {
29
- if (objA === objB) {
30
- return true;
31
- }
32
- if (typeof objA === "function" || typeof objB === "function") {
33
- return objA === objB;
34
- }
35
- try {
36
- return JSON.stringify(objA) === JSON.stringify(objB);
37
- } catch {
38
- return objA === objB;
39
- }
1
+ import { isNullish } from "radashi";
2
+ //#region helpers/object/deepClone.ts
3
+ /**
4
+ * This file is part of helpers4.
5
+ * Copyright (C) 2025 baxyz
6
+ * SPDX-License-Identifier: LGPL-3.0-or-later
7
+ */
8
+ /**
9
+ * Creates a deep copy of an object or array
10
+ * @param obj - The object to clone
11
+ * @returns Deep cloned object
12
+ */
13
+ function deepClone(obj) {
14
+ if (obj === null || typeof obj !== "object") return obj;
15
+ if (obj instanceof Date) return new Date(obj.getTime());
16
+ if (Array.isArray(obj)) return obj.map((item) => deepClone(item));
17
+ const cloned = {};
18
+ for (const key in obj) if (obj.hasOwnProperty(key)) cloned[key] = deepClone(obj[key]);
19
+ return cloned;
40
20
  }
41
- // helpers/array/quickCompare.ts
42
- function quickCompare2(arrA, arrB) {
43
- try {
44
- return JSON.stringify(arrA) === JSON.stringify(arrB);
45
- } catch {
46
- return arrA === arrB;
47
- }
21
+ //#endregion
22
+ //#region helpers/array/quickCompare.ts
23
+ /**
24
+ * This file is part of helpers4.
25
+ * Copyright (C) 2025 baxyz
26
+ * SPDX-License-Identifier: LGPL-3.0-or-later
27
+ */
28
+ /**
29
+ * Quick comparison of two arrays using JSON.stringify.
30
+ * This is a fast but simple comparison that may not work for all edge cases.
31
+ *
32
+ * @param arrA - First array to compare
33
+ * @param arrB - Second array to compare
34
+ * @returns `true` if arrays are identical according to JSON.stringify, `false` otherwise
35
+ */
36
+ function quickCompare$1(arrA, arrB) {
37
+ try {
38
+ return JSON.stringify(arrA) === JSON.stringify(arrB);
39
+ } catch {
40
+ return arrA === arrB;
41
+ }
48
42
  }
49
-
50
- // helpers/date/compare.ts
43
+ //#endregion
44
+ //#region helpers/date/compare.ts
45
+ /**
46
+ * Comparison of two dates.
47
+ *
48
+ * @param dateA - First date to compare
49
+ * @param dateB - Second date to compare
50
+ * @param options - Comparison options
51
+ * @returns `true` if dates are identical according to the specified precision, `false` otherwise
52
+ */
51
53
  function compare(dateA, dateB, options = {}) {
52
- const { precision = "milliseconds" } = options;
53
- if (!(dateA instanceof Date) || !(dateB instanceof Date)) {
54
- return dateA === dateB;
55
- }
56
- if (isNaN(dateA.getTime()) || isNaN(dateB.getTime())) {
57
- return isNaN(dateA.getTime()) && isNaN(dateB.getTime());
58
- }
59
- switch (precision) {
60
- case "days":
61
- return dateA.toDateString() === dateB.toDateString();
62
- case "hours":
63
- const hoursA = Math.floor(dateA.getTime() / (1000 * 60 * 60));
64
- const hoursB = Math.floor(dateB.getTime() / (1000 * 60 * 60));
65
- return hoursA === hoursB;
66
- case "minutes":
67
- const minutesA = Math.floor(dateA.getTime() / (1000 * 60));
68
- const minutesB = Math.floor(dateB.getTime() / (1000 * 60));
69
- return minutesA === minutesB;
70
- case "seconds":
71
- const secondsA = Math.floor(dateA.getTime() / 1000);
72
- const secondsB = Math.floor(dateB.getTime() / 1000);
73
- return secondsA === secondsB;
74
- case "milliseconds":
75
- default:
76
- return dateA.getTime() === dateB.getTime();
77
- }
54
+ const { precision = "milliseconds" } = options;
55
+ if (!(dateA instanceof Date) || !(dateB instanceof Date)) return dateA === dateB;
56
+ if (isNaN(dateA.getTime()) || isNaN(dateB.getTime())) return isNaN(dateA.getTime()) && isNaN(dateB.getTime());
57
+ switch (precision) {
58
+ case "days": return dateA.toDateString() === dateB.toDateString();
59
+ case "hours": return Math.floor(dateA.getTime() / (1e3 * 60 * 60)) === Math.floor(dateB.getTime() / (1e3 * 60 * 60));
60
+ case "minutes": return Math.floor(dateA.getTime() / (1e3 * 60)) === Math.floor(dateB.getTime() / (1e3 * 60));
61
+ case "seconds": return Math.floor(dateA.getTime() / 1e3) === Math.floor(dateB.getTime() / 1e3);
62
+ default: return dateA.getTime() === dateB.getTime();
63
+ }
78
64
  }
79
-
80
- // helpers/type/isSpecialObject.ts
65
+ //#endregion
66
+ //#region helpers/type/isSpecialObject.ts
67
+ /**
68
+ * This file is part of helpers4.
69
+ * Copyright (C) 2025 baxyz
70
+ * SPDX-License-Identifier: LGPL-3.0-or-later
71
+ */
72
+ /**
73
+ * Determines if a value is a special object that should not have its properties compared deeply.
74
+ * Special objects include: Date, Function, Promise, Observable, RegExp, Error, Map, Set, WeakMap, WeakSet, etc.
75
+ *
76
+ * @param value - The value to check
77
+ * @returns `true` if the value is a special object, `false` otherwise
78
+ */
81
79
  function isSpecialObject(value) {
82
- if (value === null || value === undefined) {
83
- return false;
84
- }
85
- if (typeof value === "function") {
86
- return true;
87
- }
88
- if (typeof value !== "object") {
89
- return false;
90
- }
91
- if (Array.isArray(value)) {
92
- return false;
93
- }
94
- if (value instanceof Date || value instanceof Promise || value instanceof RegExp || value instanceof Error || value instanceof Map || value instanceof Set || value instanceof WeakMap || value instanceof WeakSet || value instanceof ArrayBuffer || value instanceof DataView) {
95
- return true;
96
- }
97
- if (value.constructor?.name === "Observable") {
98
- return true;
99
- }
100
- if (typeof HTMLElement !== "undefined" && value instanceof HTMLElement) {
101
- return true;
102
- }
103
- const constructorName = value.constructor?.name;
104
- if (constructorName && [
105
- "Buffer",
106
- "URL",
107
- "URLSearchParams",
108
- "File",
109
- "Blob",
110
- "FormData",
111
- "Headers",
112
- "Request",
113
- "Response",
114
- "EventTarget",
115
- "Symbol"
116
- ].includes(constructorName)) {
117
- return true;
118
- }
119
- return false;
80
+ if (value === null || value === void 0) return false;
81
+ if (typeof value === "function") return true;
82
+ if (typeof value !== "object") return false;
83
+ if (Array.isArray(value)) return false;
84
+ if (value instanceof Date || value instanceof Promise || value instanceof RegExp || value instanceof Error || value instanceof Map || value instanceof Set || value instanceof WeakMap || value instanceof WeakSet || value instanceof ArrayBuffer || value instanceof DataView) return true;
85
+ if (value.constructor?.name === "Observable") return true;
86
+ if (typeof HTMLElement !== "undefined" && value instanceof HTMLElement) return true;
87
+ const constructorName = value.constructor?.name;
88
+ if (constructorName && [
89
+ "Buffer",
90
+ "URL",
91
+ "URLSearchParams",
92
+ "File",
93
+ "Blob",
94
+ "FormData",
95
+ "Headers",
96
+ "Request",
97
+ "Response",
98
+ "EventTarget",
99
+ "Symbol"
100
+ ].includes(constructorName)) return true;
101
+ return false;
120
102
  }
121
-
122
- // helpers/object/deepCompare.ts
103
+ //#endregion
104
+ //#region helpers/object/deepCompare.ts
105
+ /**
106
+ * This file is part of helpers4.
107
+ * Copyright (C) 2025 baxyz
108
+ * SPDX-License-Identifier: LGPL-3.0-or-later
109
+ */
110
+ /**
111
+ * Deep comparison of two objects that returns detailed information about differences.
112
+ *
113
+ * @param objA - First object to compare (can be object, undefined, or null)
114
+ * @param objB - Second object to compare (can be object, undefined, or null)
115
+ * @returns `true` if objects are identical, `false` if incompatible types, or a `DeepCompareResult` object detailing differences
116
+ */
123
117
  function deepCompare(objA, objB) {
124
- if (objA === objB) {
125
- return true;
126
- }
127
- if (objA == null || objB == null) {
128
- return false;
129
- }
130
- if (objA instanceof Date && objB instanceof Date) {
131
- return compare(objA, objB);
132
- }
133
- if (Array.isArray(objA) && Array.isArray(objB)) {
134
- return quickCompare2(objA, objB);
135
- }
136
- if (Array.isArray(objA) || Array.isArray(objB)) {
137
- return false;
138
- }
139
- if (isSpecialObject(objA) || isSpecialObject(objB)) {
140
- return objA === objB;
141
- }
142
- const keys = Array.from(new Set([...Object.keys(objA), ...Object.keys(objB)]));
143
- const differences = {};
144
- for (const key of keys) {
145
- const hasA = key in objA;
146
- const hasB = key in objB;
147
- if (!hasA && hasB) {
148
- differences[key] = "onlyB";
149
- } else if (hasA && !hasB) {
150
- differences[key] = "onlyA";
151
- } else if (hasA && hasB) {
152
- const valueA = objA[key];
153
- const valueB = objB[key];
154
- if (Array.isArray(valueA) && Array.isArray(valueB)) {
155
- if (!quickCompare2(valueA, valueB)) {
156
- differences[key] = false;
157
- }
158
- } else if (valueA instanceof Date && valueB instanceof Date) {
159
- if (!compare(valueA, valueB)) {
160
- differences[key] = false;
161
- }
162
- } else if (isSpecialObject(valueA) || isSpecialObject(valueB)) {
163
- if (valueA !== valueB) {
164
- differences[key] = false;
165
- }
166
- } else if (valueA !== null && valueB !== null && valueA !== undefined && valueB !== undefined && typeof valueA === "object" && typeof valueB === "object" && !isSpecialObject(valueA) && !isSpecialObject(valueB)) {
167
- const nestedResult = deepCompare(valueA, valueB);
168
- if (nestedResult !== true) {
169
- differences[key] = nestedResult === false ? false : nestedResult;
170
- }
171
- } else if (valueA !== valueB) {
172
- differences[key] = false;
173
- }
174
- }
175
- }
176
- return Object.keys(differences).length > 0 ? differences : true;
118
+ if (objA === objB) return true;
119
+ if (objA == null || objB == null) return false;
120
+ if (objA instanceof Date && objB instanceof Date) return compare(objA, objB);
121
+ if (Array.isArray(objA) && Array.isArray(objB)) return quickCompare$1(objA, objB);
122
+ if (Array.isArray(objA) || Array.isArray(objB)) return false;
123
+ if (isSpecialObject(objA) || isSpecialObject(objB)) return objA === objB;
124
+ const keys = Array.from(new Set([...Object.keys(objA), ...Object.keys(objB)]));
125
+ const differences = {};
126
+ for (const key of keys) {
127
+ const hasA = key in objA;
128
+ const hasB = key in objB;
129
+ if (!hasA && hasB) differences[key] = "onlyB";
130
+ else if (hasA && !hasB) differences[key] = "onlyA";
131
+ else {
132
+ const valueA = objA[key];
133
+ const valueB = objB[key];
134
+ if (Array.isArray(valueA) && Array.isArray(valueB)) {
135
+ if (!quickCompare$1(valueA, valueB)) differences[key] = false;
136
+ } else if (valueA instanceof Date && valueB instanceof Date) {
137
+ if (!compare(valueA, valueB)) differences[key] = false;
138
+ } else if (isSpecialObject(valueA) || isSpecialObject(valueB)) {
139
+ if (valueA !== valueB) differences[key] = false;
140
+ } else if (valueA !== null && valueB !== null && valueA !== void 0 && valueB !== void 0 && typeof valueA === "object" && typeof valueB === "object" && !isSpecialObject(valueA) && !isSpecialObject(valueB)) {
141
+ const nestedResult = deepCompare(valueA, valueB);
142
+ if (nestedResult !== true) differences[key] = nestedResult === false ? false : nestedResult;
143
+ } else if (valueA !== valueB) differences[key] = false;
144
+ }
145
+ }
146
+ return Object.keys(differences).length > 0 ? differences : true;
177
147
  }
178
- // helpers/object/deepMerge.ts
148
+ //#endregion
149
+ //#region helpers/object/deepMerge.ts
150
+ /**
151
+ * This file is part of helpers4.
152
+ * Copyright (C) 2025 baxyz
153
+ * SPDX-License-Identifier: LGPL-3.0-or-later
154
+ */
155
+ /**
156
+ * Merges two or more objects deeply
157
+ * @param target - The target object
158
+ * @param sources - The source objects to merge
159
+ * @returns The merged object
160
+ */
179
161
  function deepMerge(target, ...sources) {
180
- if (!sources.length)
181
- return target;
182
- const source = sources.shift();
183
- if (!source)
184
- return deepMerge(target, ...sources);
185
- for (const key in source) {
186
- const targetValue = target[key];
187
- const sourceValue = source[key];
188
- if (isObject(targetValue) && isObject(sourceValue)) {
189
- target[key] = deepMerge(targetValue, sourceValue);
190
- } else if (sourceValue !== undefined) {
191
- target[key] = sourceValue;
192
- }
193
- }
194
- return deepMerge(target, ...sources);
162
+ if (!sources.length) return target;
163
+ const source = sources.shift();
164
+ if (!source) return deepMerge(target, ...sources);
165
+ for (const key in source) {
166
+ const targetValue = target[key];
167
+ const sourceValue = source[key];
168
+ if (isObject(targetValue) && isObject(sourceValue)) target[key] = deepMerge(targetValue, sourceValue);
169
+ else if (sourceValue !== void 0) target[key] = sourceValue;
170
+ }
171
+ return deepMerge(target, ...sources);
195
172
  }
196
173
  function isObject(item) {
197
- return item && typeof item === "object" && !Array.isArray(item);
174
+ return item && typeof item === "object" && !Array.isArray(item);
198
175
  }
199
- // helpers/object/removeUndefinedNull.ts
200
- import { isNullish } from "radashi";
176
+ //#endregion
177
+ //#region helpers/object/get.ts
178
+ /**
179
+ * This file is part of helpers4.
180
+ * Copyright (C) 2025 baxyz
181
+ * SPDX-License-Identifier: LGPL-3.0-or-later
182
+ */
183
+ /**
184
+ * Gets a value from an object using a dot-notated path
185
+ * @param obj - The object to get value from
186
+ * @param path - The dot-notated path (e.g., 'a.b.c')
187
+ * @param defaultValue - Default value if path doesn't exist
188
+ * @returns The value at the path or default value
189
+ */
190
+ function get(obj, path, defaultValue) {
191
+ const keys = path.split(".");
192
+ let result = obj;
193
+ for (const key of keys) {
194
+ if (result == null || typeof result !== "object") return defaultValue;
195
+ result = result[key];
196
+ }
197
+ return result !== void 0 ? result : defaultValue;
198
+ }
199
+ //#endregion
200
+ //#region helpers/object/quickCompare.ts
201
+ /**
202
+ * This file is part of helpers4.
203
+ * Copyright (C) 2025 baxyz
204
+ * SPDX-License-Identifier: LGPL-3.0-or-later
205
+ */
206
+ /**
207
+ * Quick comparison of two objects using JSON.stringify.
208
+ * This is a fast but simple comparison that may not work for all edge cases.
209
+ *
210
+ * @param objA - First object to compare
211
+ * @param objB - Second object to compare
212
+ * @returns `true` if objects are identical according to JSON.stringify, `false` otherwise
213
+ */
214
+ function quickCompare(objA, objB) {
215
+ if (objA === objB) return true;
216
+ if (typeof objA === "function" || typeof objB === "function") return objA === objB;
217
+ try {
218
+ return JSON.stringify(objA) === JSON.stringify(objB);
219
+ } catch {
220
+ return objA === objB;
221
+ }
222
+ }
223
+ //#endregion
224
+ //#region helpers/object/removeUndefinedNull.ts
225
+ /**
226
+ * This file is part of helpers4.
227
+ * Copyright (C) 2025 baxyz
228
+ * SPDX-License-Identifier: LGPL-3.0-or-later
229
+ */
230
+ /**
231
+ * Remove null and undefined values from an object.
232
+ *
233
+ * @param obj an object
234
+ * @returns A shallow copy of the object without null or undefined values
235
+ */
201
236
  function removeUndefinedNull(obj) {
202
- return obj ? Object.entries(obj).filter(([_, v]) => !isNullish(v)).reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {}) : obj;
237
+ return obj ? Object.entries(obj).filter(([_, v]) => !isNullish(v)).reduce((acc, [k, v]) => ({
238
+ ...acc,
239
+ [k]: v
240
+ }), {}) : obj;
203
241
  }
204
- // helpers/object/deepClone.ts
205
- function deepClone(obj) {
206
- if (obj === null || typeof obj !== "object") {
207
- return obj;
208
- }
209
- if (obj instanceof Date) {
210
- return new Date(obj.getTime());
211
- }
212
- if (Array.isArray(obj)) {
213
- return obj.map((item) => deepClone(item));
214
- }
215
- const cloned = {};
216
- for (const key in obj) {
217
- if (obj.hasOwnProperty(key)) {
218
- cloned[key] = deepClone(obj[key]);
219
- }
220
- }
221
- return cloned;
242
+ //#endregion
243
+ //#region helpers/object/set.ts
244
+ /**
245
+ * This file is part of helpers4.
246
+ * Copyright (C) 2025 baxyz
247
+ * SPDX-License-Identifier: LGPL-3.0-or-later
248
+ */
249
+ /**
250
+ * Sets a value in an object using a dot-notated path
251
+ * @param obj - The object to set value in
252
+ * @param path - The dot-notated path (e.g., 'a.b.c')
253
+ * @param value - The value to set
254
+ * @returns The modified object
255
+ */
256
+ function set(obj, path, value) {
257
+ const keys = path.split(".");
258
+ let current = obj;
259
+ for (let i = 0; i < keys.length - 1; i++) {
260
+ const key = keys[i];
261
+ if (!(key in current) || typeof current[key] !== "object" || current[key] === null) current[key] = {};
262
+ current = current[key];
263
+ }
264
+ current[keys[keys.length - 1]] = value;
265
+ return obj;
222
266
  }
223
- export {
224
- set,
225
- removeUndefinedNull,
226
- quickCompare,
227
- get,
228
- deepMerge,
229
- deepCompare,
230
- deepClone
231
- };
267
+ //#endregion
268
+ export { deepClone, deepCompare, deepMerge, get, quickCompare, removeUndefinedNull, set };
269
+
270
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../helpers/object/deepClone.ts","../../../helpers/array/quickCompare.ts","../../../helpers/date/compare.ts","../../../helpers/type/isSpecialObject.ts","../../../helpers/object/deepCompare.ts","../../../helpers/object/deepMerge.ts","../../../helpers/object/get.ts","../../../helpers/object/quickCompare.ts","../../../helpers/object/removeUndefinedNull.ts","../../../helpers/object/set.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Creates a deep copy of an object or array\n * @param obj - The object to clone\n * @returns Deep cloned object\n */\nexport function deepClone<T>(obj: T): T {\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n\n if (obj instanceof Date) {\n return new Date(obj.getTime()) as T;\n }\n\n if (Array.isArray(obj)) {\n return obj.map(item => deepClone(item)) as T;\n }\n\n const cloned = {} as T;\n for (const key in obj) {\n if (obj.hasOwnProperty(key)) {\n cloned[key] = deepClone(obj[key]);\n }\n }\n\n return cloned;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Quick comparison of two arrays using JSON.stringify.\n * This is a fast but simple comparison that may not work for all edge cases.\n * \n * @param arrA - First array to compare\n * @param arrB - Second array to compare\n * @returns `true` if arrays are identical according to JSON.stringify, `false` otherwise\n */\nexport function quickCompare<T>(arrA: T[], arrB: T[]): boolean {\n try {\n return JSON.stringify(arrA) === JSON.stringify(arrB);\n } catch {\n // If JSON.stringify fails (e.g., circular references), fall back to reference equality\n return arrA === arrB;\n }\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Options for date comparison\n */\nexport interface DateCompareOptions {\n /** \n * Comparison precision\n * - 'milliseconds': Compare all including milliseconds (default)\n * - 'seconds': Ignore milliseconds, compare to the second\n * - 'minutes': Ignore seconds and milliseconds, compare to the minute\n * - 'hours': Ignore minutes, seconds and milliseconds, compare to the hour\n * - 'days': Compare only the date part (ignore time completely)\n */\n precision?: 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';\n}\n\n/**\n * Comparison of two dates.\n * \n * @param dateA - First date to compare\n * @param dateB - Second date to compare\n * @param options - Comparison options\n * @returns `true` if dates are identical according to the specified precision, `false` otherwise\n */\nexport function compare(dateA: Date, dateB: Date, options: DateCompareOptions = {}): boolean {\n const { precision = 'milliseconds' } = options;\n\n if (!(dateA instanceof Date) || !(dateB instanceof Date)) {\n return dateA === dateB;\n }\n\n // Handle invalid dates\n if (isNaN(dateA.getTime()) || isNaN(dateB.getTime())) {\n return isNaN(dateA.getTime()) && isNaN(dateB.getTime());\n }\n\n switch (precision) {\n case 'days':\n return dateA.toDateString() === dateB.toDateString();\n\n case 'hours':\n const hoursA = Math.floor(dateA.getTime() / (1000 * 60 * 60));\n const hoursB = Math.floor(dateB.getTime() / (1000 * 60 * 60));\n return hoursA === hoursB;\n\n case 'minutes':\n const minutesA = Math.floor(dateA.getTime() / (1000 * 60));\n const minutesB = Math.floor(dateB.getTime() / (1000 * 60));\n return minutesA === minutesB;\n\n case 'seconds':\n const secondsA = Math.floor(dateA.getTime() / 1000);\n const secondsB = Math.floor(dateB.getTime() / 1000);\n return secondsA === secondsB;\n\n case 'milliseconds':\n default:\n return dateA.getTime() === dateB.getTime();\n }\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Determines if a value is a special object that should not have its properties compared deeply.\n * Special objects include: Date, Function, Promise, Observable, RegExp, Error, Map, Set, WeakMap, WeakSet, etc.\n * \n * @param value - The value to check\n * @returns `true` if the value is a special object, `false` otherwise\n */\nexport function isSpecialObject(value: unknown): boolean {\n if (value === null || value === undefined) {\n return false;\n }\n\n // Functions are special objects\n if (typeof value === 'function') {\n return true;\n }\n\n // Non-objects are not special\n if (typeof value !== 'object') {\n return false;\n }\n\n // Arrays are not considered special objects for our purposes\n if (Array.isArray(value)) {\n return false;\n }\n\n // Check for built-in special objects\n if (\n value instanceof Date ||\n value instanceof Promise ||\n value instanceof RegExp ||\n value instanceof Error ||\n value instanceof Map ||\n value instanceof Set ||\n value instanceof WeakMap ||\n value instanceof WeakSet ||\n value instanceof ArrayBuffer ||\n value instanceof DataView\n ) {\n return true;\n }\n\n // Check for Observable (RxJS or similar)\n if (value.constructor?.name === 'Observable') {\n return true;\n }\n\n // Check for DOM elements (if in browser environment)\n if (typeof HTMLElement !== 'undefined' && value instanceof HTMLElement) {\n return true;\n }\n\n // Check for other built-in types by constructor name\n const constructorName = value.constructor?.name;\n if (constructorName && [\n 'Buffer', // Node.js Buffer\n 'URL', // Web URL API\n 'URLSearchParams',\n 'File', // Web File API\n 'Blob', // Web Blob API\n 'FormData', // Web FormData API\n 'Headers', // Web Headers API\n 'Request', // Web Request API\n 'Response', // Web Response API\n 'EventTarget', // Web EventTarget API\n 'Symbol' // Symbol objects\n ].includes(constructorName)) {\n return true;\n }\n\n return false;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\nimport { quickCompare as quickCompareArray } from '../array/quickCompare';\nimport { compare as compareDate } from '../date/compare';\nimport { isSpecialObject } from '../type/isSpecialObject';\n\n/**\n * Result type for deep comparison when objects are not identical\n */\nexport interface DeepCompareResult {\n [key: string]: \"onlyA\" | \"onlyB\" | false | DeepCompareResult;\n}\n\n/**\n * Deep comparison of two objects that returns detailed information about differences.\n * \n * @param objA - First object to compare (can be object, undefined, or null)\n * @param objB - Second object to compare (can be object, undefined, or null)\n * @returns `true` if objects are identical, `false` if incompatible types, or a `DeepCompareResult` object detailing differences\n */\nexport function deepCompare(objA: object | undefined | null, objB: object | undefined | null): true | false | DeepCompareResult {\n // Quick reference equality check\n if (objA === objB) {\n return true;\n }\n\n // Handle null/undefined cases\n if (objA == null || objB == null) {\n return false;\n }\n\n // Handle Date objects\n if (objA instanceof Date && objB instanceof Date) {\n return compareDate(objA, objB);\n }\n\n // Handle Arrays at root level - use quickCompare\n if (Array.isArray(objA) && Array.isArray(objB)) {\n return quickCompareArray(objA, objB);\n }\n\n // If one is array and other is not\n if (Array.isArray(objA) || Array.isArray(objB)) {\n return false;\n }\n\n // Handle special objects - compare by reference only\n if (isSpecialObject(objA) || isSpecialObject(objB)) {\n return objA === objB;\n }\n\n // Handle objects - detailed comparison\n const keys = Array.from(new Set([...Object.keys(objA), ...Object.keys(objB)]));\n\n const differences: DeepCompareResult = {};\n\n for (const key of keys) {\n const hasA = key in objA;\n const hasB = key in objB;\n\n if (!hasA && hasB) {\n differences[key] = \"onlyB\";\n } else if (hasA && !hasB) {\n differences[key] = \"onlyA\";\n } else {\n // Both objects have this key - compare values\n const valueA = (objA as any)[key];\n const valueB = (objB as any)[key];\n\n // For arrays, use quickCompare\n if (Array.isArray(valueA) && Array.isArray(valueB)) {\n if (!quickCompareArray(valueA, valueB)) {\n differences[key] = false;\n }\n }\n // For dates, use compareDate\n else if (valueA instanceof Date && valueB instanceof Date) {\n if (!compareDate(valueA, valueB)) {\n differences[key] = false;\n }\n }\n // For special objects, compare by reference only\n else if (isSpecialObject(valueA) || isSpecialObject(valueB)) {\n if (valueA !== valueB) {\n differences[key] = false;\n }\n }\n // For objects, recurse (only plain objects now)\n else if (\n valueA !== null && valueB !== null &&\n valueA !== undefined && valueB !== undefined &&\n typeof valueA === 'object' && typeof valueB === 'object' &&\n !isSpecialObject(valueA) && !isSpecialObject(valueB)\n ) {\n const nestedResult = deepCompare(valueA, valueB);\n if (nestedResult !== true) {\n differences[key] = nestedResult === false ? false : nestedResult;\n }\n }\n // For primitives and other types, check equality\n else if (valueA !== valueB) {\n differences[key] = false;\n }\n }\n }\n\n return Object.keys(differences).length > 0 ? differences : true;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Merges two or more objects deeply\n * @param target - The target object\n * @param sources - The source objects to merge\n * @returns The merged object\n */\nexport function deepMerge<T extends Record<string, any>>(target: T, ...sources: Record<string, any>[]): T {\n if (!sources.length) return target;\n const source = sources.shift();\n\n if (!source) return deepMerge(target, ...sources);\n\n for (const key in source) {\n const targetValue = target[key];\n const sourceValue = source[key];\n\n if (isObject(targetValue) && isObject(sourceValue)) {\n (target as any)[key] = deepMerge(targetValue, sourceValue);\n } else if (sourceValue !== undefined) {\n (target as any)[key] = sourceValue;\n }\n }\n\n return deepMerge(target, ...sources);\n}\n\nfunction isObject(item: any): item is object {\n return item && typeof item === 'object' && !Array.isArray(item);\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Gets a value from an object using a dot-notated path\n * @param obj - The object to get value from\n * @param path - The dot-notated path (e.g., 'a.b.c')\n * @param defaultValue - Default value if path doesn't exist\n * @returns The value at the path or default value\n */\nexport function get<T = any>(obj: any, path: string, defaultValue?: T): T | undefined {\n const keys = path.split('.');\n let result = obj;\n\n for (const key of keys) {\n if (result == null || typeof result !== 'object') {\n return defaultValue;\n }\n result = result[key];\n }\n\n return result !== undefined ? result : defaultValue;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Quick comparison of two objects using JSON.stringify.\n * This is a fast but simple comparison that may not work for all edge cases.\n *\n * @param objA - First object to compare\n * @param objB - Second object to compare\n * @returns `true` if objects are identical according to JSON.stringify, `false` otherwise\n */\nexport function quickCompare(objA: unknown, objB: unknown): boolean {\n // Quick reference equality check\n if (objA === objB) {\n return true;\n }\n\n // Handle functions - they can't be JSON.stringified reliably\n if (typeof objA === 'function' || typeof objB === 'function') {\n return objA === objB;\n }\n\n try {\n return JSON.stringify(objA) === JSON.stringify(objB);\n } catch {\n // If JSON.stringify fails (e.g., circular references), fall back to basic comparison\n return objA === objB;\n }\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\nimport { isNullish } from \"radashi\";\n\n/**\n * Remove null and undefined values from an object.\n *\n * @param obj an object\n * @returns A shallow copy of the object without null or undefined values\n */\nexport function removeUndefinedNull<T extends Record<string, string | number | boolean | null | undefined>>(obj: T): Partial<T>;\n\n/**\n * Remove null and undefined values from an object.\n *\n * @param obj a null object\n * @returns null\n */\nexport function removeUndefinedNull(obj: null): null;\n\n/**\n * Remove null and undefined values from an object.\n *\n * @param obj an undefined object\n * @returns undefined\n */\nexport function removeUndefinedNull(obj: undefined): undefined;\n\n/**\n * Remove null and undefined values from an object.\n *\n * @param obj an object\n * @returns A shallow copy of the object without null or undefined values\n */\nexport function removeUndefinedNull<T extends Record<string, string | number | boolean | null | undefined>>(obj: T | null | undefined): Partial<T> | null | undefined {\n return obj ? Object.entries(obj)\n .filter(([_, v]) => !isNullish(v))\n .reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {} as Partial<T>) : obj;\n}\n\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Sets a value in an object using a dot-notated path\n * @param obj - The object to set value in\n * @param path - The dot-notated path (e.g., 'a.b.c')\n * @param value - The value to set\n * @returns The modified object\n */\nexport function set(obj: Record<string, any>, path: string, value: any): Record<string, any> {\n const keys = path.split('.');\n let current = obj;\n\n for (let i = 0; i < keys.length - 1; i++) {\n const key = keys[i];\n\n if (!(key in current) || typeof current[key] !== 'object' || current[key] === null) {\n current[key] = {};\n }\n\n current = current[key];\n }\n\n current[keys[keys.length - 1]] = value;\n return obj;\n}\n"],"mappings":";;;;;;;;;;;;AAWA,SAAgB,UAAa,KAAW;AACtC,KAAI,QAAQ,QAAQ,OAAO,QAAQ,SACjC,QAAO;AAGT,KAAI,eAAe,KACjB,QAAO,IAAI,KAAK,IAAI,SAAS,CAAC;AAGhC,KAAI,MAAM,QAAQ,IAAI,CACpB,QAAO,IAAI,KAAI,SAAQ,UAAU,KAAK,CAAC;CAGzC,MAAM,SAAS,EAAE;AACjB,MAAK,MAAM,OAAO,IAChB,KAAI,IAAI,eAAe,IAAI,CACzB,QAAO,OAAO,UAAU,IAAI,KAAK;AAIrC,QAAO;;;;;;;;;;;;;;;;;ACjBT,SAAgB,eAAgB,MAAW,MAAoB;AAC7D,KAAI;AACF,SAAO,KAAK,UAAU,KAAK,KAAK,KAAK,UAAU,KAAK;SAC9C;AAEN,SAAO,SAAS;;;;;;;;;;;;;ACUpB,SAAgB,QAAQ,OAAa,OAAa,UAA8B,EAAE,EAAW;CAC3F,MAAM,EAAE,YAAY,mBAAmB;AAEvC,KAAI,EAAE,iBAAiB,SAAS,EAAE,iBAAiB,MACjD,QAAO,UAAU;AAInB,KAAI,MAAM,MAAM,SAAS,CAAC,IAAI,MAAM,MAAM,SAAS,CAAC,CAClD,QAAO,MAAM,MAAM,SAAS,CAAC,IAAI,MAAM,MAAM,SAAS,CAAC;AAGzD,SAAQ,WAAR;EACE,KAAK,OACH,QAAO,MAAM,cAAc,KAAK,MAAM,cAAc;EAEtD,KAAK,QAGH,QAFe,KAAK,MAAM,MAAM,SAAS,IAAI,MAAO,KAAK,IAAI,KAC9C,KAAK,MAAM,MAAM,SAAS,IAAI,MAAO,KAAK,IAAI;EAG/D,KAAK,UAGH,QAFiB,KAAK,MAAM,MAAM,SAAS,IAAI,MAAO,IAAI,K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QAAQ,SAAS,YAAY,QAAQ,SAAS,KAC5E,SAAQ,OAAO,EAAE;AAGnB,YAAU,QAAQ;;AAGpB,SAAQ,KAAK,KAAK,SAAS,MAAM;AACjC,QAAO"}
package/package.json CHANGED
@@ -1,42 +1,51 @@
1
1
  {
2
2
  "name": "@helpers4/object",
3
- "version": "2.0.0-alpha.3",
3
+ "version": "2.0.0-alpha.6",
4
4
  "description": "A set of helpers in TS/JS, compatible with tree-shaking, for object.",
5
5
  "author": "baxyz <baxy@etik.com>",
6
- "license": "AGPL-3.0",
6
+ "license": "LGPL-3.0",
7
7
  "repository": {
8
8
  "type": "git",
9
- "url": "git+https://github.com/helpers4/helpers4.git"
9
+ "url": "git+https://github.com/helpers4/typescript.git"
10
10
  },
11
11
  "type": "module",
12
+ "sideEffects": false,
12
13
  "main": "lib/index.js",
13
14
  "types": "lib/index.d.ts",
14
15
  "exports": {
15
16
  ".": {
16
17
  "types": "./lib/index.d.ts",
17
18
  "import": "./lib/index.js"
18
- }
19
+ },
20
+ "./examples.json": "./examples.json",
21
+ "./api.json": "./api.json",
22
+ "./package.json": "./package.json"
19
23
  },
20
24
  "keywords": [
21
25
  "helpers",
22
26
  "object",
23
- "get",
24
- "set",
25
- "quickCompare",
27
+ "deepClone",
26
28
  "deepCompare",
27
29
  "deepMerge",
30
+ "get",
31
+ "quickCompare",
28
32
  "removeUndefinedNull",
29
- "deepClone"
33
+ "set"
30
34
  ],
31
35
  "files": [
32
36
  "lib/index.js",
33
37
  "lib/index.d.ts",
34
38
  "lib/index.js.map",
39
+ "examples.json",
40
+ "api.json",
35
41
  "LICENSE.md",
36
42
  "package.json",
37
43
  "README.md"
38
44
  ],
45
+ "engines": {
46
+ "node": ">=18.0.0"
47
+ },
39
48
  "peerDependencies": {
40
- "radashi": "^12.4.0"
49
+ "radashi": "^12.7.2"
41
50
  }
42
- }
51
+ }