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