@helpers4/object 2.0.0-alpha.10 → 2.0.0-alpha.14

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/llms.txt ADDED
@@ -0,0 +1,520 @@
1
+ # @helpers4/object
2
+
3
+ > Tree-shakable TypeScript utility functions for the `object` domain.
4
+ > Package: `@helpers4/object` — Version: 2.0.0-alpha.14
5
+ > License: LGPL-3.0-or-later
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @helpers4/object
11
+ # or
12
+ pnpm add @helpers4/object
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { compact, deepClone, deepCompare, ... } from '@helpers4/object';
19
+ ```
20
+
21
+ ## Functions
22
+
23
+ | Function | Description |
24
+ |---|---|
25
+ | `compact` | Removes all entries with falsy values (`false`, `null`, `undefined`, `0`, `""`, `NaN`) from an objec |
26
+ | `deepClone` | Creates a deep copy of an object or array |
27
+ | `deepCompare` | Deep comparison of two objects that returns detailed information about differences. |
28
+ | `DeepCompareResult` | Result type for deep comparison when objects are not identical |
29
+ | `deepMerge` | Merges two or more objects deeply |
30
+ | `get` | Gets a value from an object using a dot-notated path |
31
+ | `omit` | Creates a new object without the specified keys. |
32
+ | `pick` | Creates a new object with only the specified keys. |
33
+ | `removeUndefinedNull` | Remove null and undefined values from an object. |
34
+ | `set` | Sets a value in an object using a dot-notated path |
35
+ | `shallowEquals` | Quick comparison of two objects using JSON.stringify. This is a fast but simple comparison that may |
36
+
37
+ ---
38
+
39
+ ## API Reference
40
+
41
+ ### `compact`
42
+
43
+ Removes all entries with falsy values (`false`, `null`, `undefined`, `0`, `""`, `NaN`) from an object.
44
+
45
+ ```typescript
46
+ import { compact } from '@helpers4/object';
47
+
48
+ compact<T extends Record<string, unknown>>(obj: T): Partial<T>
49
+ ```
50
+
51
+ **Parameters:**
52
+
53
+ - `obj: T` — The source object
54
+
55
+ **Returns:** `Partial<T>` — A new object containing only entries with truthy values
56
+
57
+ ```typescript
58
+ import { compact } from '@helpers4/object';
59
+
60
+ compact(obj: undefined): undefined
61
+ ```
62
+
63
+ **Parameters:**
64
+
65
+ - `obj: undefined` — The source object
66
+
67
+ **Returns:** `undefined` — A new object containing only entries with truthy values
68
+
69
+ ```typescript
70
+ import { compact } from '@helpers4/object';
71
+
72
+ compact(obj: null): null
73
+ ```
74
+
75
+ **Parameters:**
76
+
77
+ - `obj: null` — The source object
78
+
79
+ **Returns:** `null` — A new object containing only entries with truthy values
80
+
81
+ **Examples:**
82
+
83
+ *Remove falsy values from object*
84
+
85
+ Removes all entries with falsy values (false, null, undefined, 0, "", NaN).
86
+
87
+ ```typescript
88
+ compact({ a: 1, b: null, c: '', d: 0, e: 'hello' })
89
+ // => { a: 1, e: 'hello' }
90
+ ```
91
+
92
+ *Clean up API response*
93
+
94
+ Useful to strip empty or missing fields before sending data.
95
+
96
+ ```typescript
97
+ compact({ name: 'Alice', email: '', age: 0, role: 'admin' })
98
+ // => { name: 'Alice', role: 'admin' }
99
+ ```
100
+
101
+ ---
102
+
103
+ ### `deepClone`
104
+
105
+ Creates a deep copy of an object or array
106
+
107
+ ```typescript
108
+ import { deepClone } from '@helpers4/object';
109
+
110
+ deepClone<T>(obj: T): T
111
+ ```
112
+
113
+ **Parameters:**
114
+
115
+ - `obj: T` — The object to clone
116
+
117
+ **Returns:** `T` — Deep cloned object
118
+
119
+ **Examples:**
120
+
121
+ *Clone a nested object*
122
+
123
+ Creates a deep copy — modifying the clone does not affect the original.
124
+
125
+ ```typescript
126
+ const original = { a: { b: 1 } };
127
+ const cloned = deepClone(original);
128
+ cloned.a.b = 2;
129
+ // original.a.b is still 1
130
+ ```
131
+
132
+ ---
133
+
134
+ ### `deepCompare`
135
+
136
+ Deep comparison of two objects that returns detailed information about differences.
137
+
138
+ ```typescript
139
+ import { deepCompare } from '@helpers4/object';
140
+
141
+ deepCompare(objA: object | null | undefined, objB: object | null | undefined): boolean | DeepCompareResult
142
+ ```
143
+
144
+ **Parameters:**
145
+
146
+ - `objA: object | null | undefined` — First object to compare (can be object, undefined, or null)
147
+ - `objB: object | null | undefined` — Second object to compare (can be object, undefined, or null)
148
+
149
+ **Returns:** `boolean | DeepCompareResult` — `true` if objects are identical, `false` if incompatible types, or a `DeepCompareResult` object detailing differences
150
+
151
+ **Examples:**
152
+
153
+ *Compare nested objects*
154
+
155
+ Deeply compares two objects, returning true when they are structurally equal.
156
+
157
+ ```typescript
158
+ deepCompare({ a: { b: 1 } }, { a: { b: 1 } })
159
+ // => true
160
+ ```
161
+
162
+ *Detect deep differences*
163
+
164
+ Returns a detailed diff object when nested values differ.
165
+
166
+ ```typescript
167
+ deepCompare({ a: { b: 1 } }, { a: { b: 2 } })
168
+ // => { a: { b: false } }
169
+ ```
170
+
171
+ ---
172
+
173
+ ### `DeepCompareResult`
174
+
175
+ Result type for deep comparison when objects are not identical
176
+
177
+ ---
178
+
179
+ ### `deepMerge`
180
+
181
+ Merges two or more objects deeply
182
+
183
+ ```typescript
184
+ import { deepMerge } from '@helpers4/object';
185
+
186
+ deepMerge<T extends Record<string, unknown>>(target: T, sources: Record<string, unknown>[]): T
187
+ ```
188
+
189
+ **Parameters:**
190
+
191
+ - `target: T` — The target object
192
+ - `sources: Record<string, unknown>[]` — The source objects to merge
193
+
194
+ **Returns:** `T` — The merged object
195
+
196
+ ```typescript
197
+ import { deepMerge } from '@helpers4/object';
198
+
199
+ deepMerge(target: undefined, sources: Record<string, unknown>[]): undefined
200
+ ```
201
+
202
+ **Parameters:**
203
+
204
+ - `target: undefined` — The target object
205
+ - `sources: Record<string, unknown>[]` — The source objects to merge
206
+
207
+ **Returns:** `undefined` — The merged object
208
+
209
+ ```typescript
210
+ import { deepMerge } from '@helpers4/object';
211
+
212
+ deepMerge(target: null, sources: Record<string, unknown>[]): null
213
+ ```
214
+
215
+ **Parameters:**
216
+
217
+ - `target: null` — The target object
218
+ - `sources: Record<string, unknown>[]` — The source objects to merge
219
+
220
+ **Returns:** `null` — The merged object
221
+
222
+ **Examples:**
223
+
224
+ *Merge two objects deeply*
225
+
226
+ Recursively merges source properties into the target object.
227
+
228
+ ```typescript
229
+ deepMerge({ a: 1, b: { c: 2 } }, { b: { d: 3 }, e: 4 })
230
+ // => { a: 1, b: { c: 2, d: 3 }, e: 4 }
231
+ ```
232
+
233
+ ---
234
+
235
+ ### `get`
236
+
237
+ Gets a value from an object using a dot-notated path
238
+
239
+ ```typescript
240
+ import { get } from '@helpers4/object';
241
+
242
+ get<T = unknown>(obj: unknown, path: string, defaultValue?: T): T | undefined
243
+ ```
244
+
245
+ **Parameters:**
246
+
247
+ - `obj: unknown` — The object to get value from
248
+ - `path: string` — The dot-notated path (e.g., 'a.b.c')
249
+ - `defaultValue?: T` — Default value if path doesn't exist
250
+
251
+ **Returns:** `T | undefined` — The value at the path or default value
252
+
253
+ **Examples:**
254
+
255
+ *Access a nested property*
256
+
257
+ Uses a dot-notated path to retrieve a deeply nested value.
258
+
259
+ ```typescript
260
+ get({ a: { b: { c: 42 } } }, 'a.b.c')
261
+ // => 42
262
+ ```
263
+
264
+ *Return default for missing path*
265
+
266
+ Returns the default value when the path does not exist.
267
+
268
+ ```typescript
269
+ get({ a: 1 }, 'b.c', 'default')
270
+ // => 'default'
271
+ ```
272
+
273
+ ---
274
+
275
+ ### `omit`
276
+
277
+ Creates a new object without the specified keys.
278
+
279
+ ```typescript
280
+ import { omit } from '@helpers4/object';
281
+
282
+ omit<T extends Record<string, unknown>, K extends string | number | symbol>(obj: T, keys: readonly K[]): Omit<T, K>
283
+ ```
284
+
285
+ **Parameters:**
286
+
287
+ - `obj: T` — The source object
288
+ - `keys: readonly K[]` — The keys to omit
289
+
290
+ **Returns:** `Omit<T, K>` — A new object without the omitted keys
291
+
292
+ ```typescript
293
+ import { omit } from '@helpers4/object';
294
+
295
+ omit(obj: undefined, keys: readonly string[]): undefined
296
+ ```
297
+
298
+ **Parameters:**
299
+
300
+ - `obj: undefined` — The source object
301
+ - `keys: readonly string[]` — The keys to omit
302
+
303
+ **Returns:** `undefined` — A new object without the omitted keys
304
+
305
+ ```typescript
306
+ import { omit } from '@helpers4/object';
307
+
308
+ omit(obj: null, keys: readonly string[]): null
309
+ ```
310
+
311
+ **Parameters:**
312
+
313
+ - `obj: null` — The source object
314
+ - `keys: readonly string[]` — The keys to omit
315
+
316
+ **Returns:** `null` — A new object without the omitted keys
317
+
318
+ **Examples:**
319
+
320
+ *Omit specific keys*
321
+
322
+ Creates a new object without the specified keys.
323
+
324
+ ```typescript
325
+ omit({ a: 1, b: 2, c: 3 }, ['b'])
326
+ // => { a: 1, c: 3 }
327
+ ```
328
+
329
+ *Remove sensitive fields*
330
+
331
+ Useful to strip sensitive data before sending to client.
332
+
333
+ ```typescript
334
+ const user = { id: 1, name: 'Alice', password: 'secret', token: 'abc123' };
335
+ omit(user, ['password', 'token'])
336
+ // => { id: 1, name: 'Alice' }
337
+ ```
338
+
339
+ ---
340
+
341
+ ### `pick`
342
+
343
+ Creates a new object with only the specified keys.
344
+
345
+ ```typescript
346
+ import { pick } from '@helpers4/object';
347
+
348
+ pick<T extends Record<string, unknown>, K extends string | number | symbol>(obj: T, keys: readonly K[]): Pick<T, K>
349
+ ```
350
+
351
+ **Parameters:**
352
+
353
+ - `obj: T` — The source object
354
+ - `keys: readonly K[]` — The keys to pick
355
+
356
+ **Returns:** `Pick<T, K>` — A new object with only the picked keys
357
+
358
+ ```typescript
359
+ import { pick } from '@helpers4/object';
360
+
361
+ pick(obj: undefined, keys: readonly string[]): undefined
362
+ ```
363
+
364
+ **Parameters:**
365
+
366
+ - `obj: undefined` — The source object
367
+ - `keys: readonly string[]` — The keys to pick
368
+
369
+ **Returns:** `undefined` — A new object with only the picked keys
370
+
371
+ ```typescript
372
+ import { pick } from '@helpers4/object';
373
+
374
+ pick(obj: null, keys: readonly string[]): null
375
+ ```
376
+
377
+ **Parameters:**
378
+
379
+ - `obj: null` — The source object
380
+ - `keys: readonly string[]` — The keys to pick
381
+
382
+ **Returns:** `null` — A new object with only the picked keys
383
+
384
+ **Examples:**
385
+
386
+ *Pick specific keys*
387
+
388
+ Creates a new object with only the specified keys.
389
+
390
+ ```typescript
391
+ pick({ a: 1, b: 2, c: 3 }, ['a', 'c'])
392
+ // => { a: 1, c: 3 }
393
+ ```
394
+
395
+ *Extract user fields*
396
+
397
+ Useful to select only the fields you need from an object.
398
+
399
+ ```typescript
400
+ const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
401
+ pick(user, ['id', 'name', 'email'])
402
+ // => { id: 1, name: 'Alice', email: 'alice@example.com' }
403
+ ```
404
+
405
+ ---
406
+
407
+ ### `removeUndefinedNull`
408
+
409
+ Remove null and undefined values from an object.
410
+
411
+ ```typescript
412
+ import { removeUndefinedNull } from '@helpers4/object';
413
+
414
+ removeUndefinedNull<T extends Record<string, string | number | boolean | null | undefined>>(obj: T): Partial<T>
415
+ ```
416
+
417
+ **Parameters:**
418
+
419
+ - `obj: T` — an object
420
+
421
+ **Returns:** `Partial<T>` — A shallow copy of the object without null or undefined values
422
+
423
+ ```typescript
424
+ import { removeUndefinedNull } from '@helpers4/object';
425
+
426
+ removeUndefinedNull(obj: null): null
427
+ ```
428
+
429
+ **Parameters:**
430
+
431
+ - `obj: null` — a null object
432
+
433
+ **Returns:** `null` — null
434
+
435
+ ```typescript
436
+ import { removeUndefinedNull } from '@helpers4/object';
437
+
438
+ removeUndefinedNull(obj: undefined): undefined
439
+ ```
440
+
441
+ **Parameters:**
442
+
443
+ - `obj: undefined` — an undefined object
444
+
445
+ **Returns:** `undefined` — undefined
446
+
447
+ **Examples:**
448
+
449
+ *Strip null and undefined values*
450
+
451
+ Returns a shallow copy of the object without null or undefined properties.
452
+
453
+ ```typescript
454
+ removeUndefinedNull({ a: 1, b: null, c: undefined, d: 'ok' })
455
+ // => { a: 1, d: 'ok' }
456
+ ```
457
+
458
+ ---
459
+
460
+ ### `set`
461
+
462
+ Sets a value in an object using a dot-notated path
463
+
464
+ ```typescript
465
+ import { set } from '@helpers4/object';
466
+
467
+ set(obj: Record<string, unknown>, path: string, value: unknown): Record<string, unknown>
468
+ ```
469
+
470
+ **Parameters:**
471
+
472
+ - `obj: Record<string, unknown>` — The object to set value in
473
+ - `path: string` — The dot-notated path (e.g., 'a.b.c')
474
+ - `value: unknown` — The value to set
475
+
476
+ **Returns:** `Record<string, unknown>` — The modified object
477
+
478
+ **Examples:**
479
+
480
+ *Set a nested property*
481
+
482
+ Creates intermediate objects as needed along the dot-notated path.
483
+
484
+ ```typescript
485
+ set({}, 'a.b.c', 42)
486
+ // => { a: { b: { c: 42 } } }
487
+ ```
488
+
489
+ ---
490
+
491
+ ### `shallowEquals`
492
+
493
+ Quick comparison of two objects using JSON.stringify.
494
+ This is a fast but simple comparison that may not work for all edge cases.
495
+
496
+ ```typescript
497
+ import { shallowEquals } from '@helpers4/object';
498
+
499
+ shallowEquals(objA: unknown, objB: unknown): boolean
500
+ ```
501
+
502
+ **Parameters:**
503
+
504
+ - `objA: unknown` — First object to compare
505
+ - `objB: unknown` — Second object to compare
506
+
507
+ **Returns:** `boolean` — `true` if objects are identical according to JSON.stringify, `false` otherwise
508
+
509
+ **Examples:**
510
+
511
+ *Compare two equal objects*
512
+
513
+ Uses JSON.stringify for a fast comparison.
514
+
515
+ ```typescript
516
+ shallowEquals({ a: 1, b: 2 }, { a: 1, b: 2 })
517
+ // => true
518
+ ```
519
+
520
+ ---