@helpers4/object 2.0.2 → 2.0.4
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/README.md +2 -0
- package/lib/index.d.ts +152 -18
- package/lib/index.js +136 -38
- package/lib/index.js.map +1 -1
- package/llms.txt +216 -29
- package/meta/api.json +198 -36
- package/meta/examples.json +48 -3
- package/package.json +3 -1
package/meta/api.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"category": "object",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"functions": [
|
|
5
5
|
{
|
|
6
6
|
"name": "compact",
|
|
@@ -113,73 +113,73 @@
|
|
|
113
113
|
{
|
|
114
114
|
"name": "deepMerge",
|
|
115
115
|
"kind": "function",
|
|
116
|
-
"description": "Merges two or more objects deeply",
|
|
116
|
+
"description": "Merges two or more objects deeply.\n\nRecursively merges own enumerable properties — both string and symbol keys.\nPlain objects are merged recursively; all other values (arrays, class instances,\nprimitives, etc.) are replaced by the source value.\n`undefined` source values do not overwrite existing target values.",
|
|
117
117
|
"since": "1.9.0",
|
|
118
118
|
"signatures": [
|
|
119
119
|
{
|
|
120
|
-
"signature": "deepMerge<T extends Record<
|
|
121
|
-
"description": "Merges two or more objects deeply",
|
|
120
|
+
"signature": "deepMerge<T extends Record<PropertyKey, unknown>>(target: T, sources: Record<PropertyKey, unknown>[]): T",
|
|
121
|
+
"description": "Merges two or more objects deeply.\n\nRecursively merges own enumerable properties — both string and symbol keys.\nPlain objects are merged recursively; all other values (arrays, class instances,\nprimitives, etc.) are replaced by the source value.\n`undefined` source values do not overwrite existing target values.",
|
|
122
122
|
"params": [
|
|
123
123
|
{
|
|
124
124
|
"name": "target",
|
|
125
125
|
"type": "T",
|
|
126
|
-
"description": "The target object"
|
|
126
|
+
"description": "The target object (mutated in place)"
|
|
127
127
|
},
|
|
128
128
|
{
|
|
129
129
|
"name": "sources",
|
|
130
|
-
"type": "Record<
|
|
131
|
-
"description": "
|
|
130
|
+
"type": "Record<PropertyKey, unknown>[]",
|
|
131
|
+
"description": "One or more source objects to merge into the target"
|
|
132
132
|
}
|
|
133
133
|
],
|
|
134
134
|
"returns": {
|
|
135
135
|
"type": "T",
|
|
136
|
-
"description": "The
|
|
136
|
+
"description": "The mutated target"
|
|
137
137
|
},
|
|
138
138
|
"typeParameters": [
|
|
139
139
|
{
|
|
140
140
|
"name": "T",
|
|
141
|
-
"constraint": "Record<
|
|
141
|
+
"constraint": "Record<PropertyKey, unknown>"
|
|
142
142
|
}
|
|
143
143
|
]
|
|
144
144
|
},
|
|
145
145
|
{
|
|
146
|
-
"signature": "deepMerge(target: undefined, sources: Record<
|
|
147
|
-
"description": "Merges two or more objects deeply",
|
|
146
|
+
"signature": "deepMerge(target: undefined, sources: Record<PropertyKey, unknown>[]): undefined",
|
|
147
|
+
"description": "Merges two or more objects deeply.\n\nRecursively merges own enumerable properties — both string and symbol keys.\nPlain objects are merged recursively; all other values (arrays, class instances,\nprimitives, etc.) are replaced by the source value.\n`undefined` source values do not overwrite existing target values.",
|
|
148
148
|
"params": [
|
|
149
149
|
{
|
|
150
150
|
"name": "target",
|
|
151
151
|
"type": "undefined",
|
|
152
|
-
"description": "The target object"
|
|
152
|
+
"description": "The target object (mutated in place)"
|
|
153
153
|
},
|
|
154
154
|
{
|
|
155
155
|
"name": "sources",
|
|
156
|
-
"type": "Record<
|
|
157
|
-
"description": "
|
|
156
|
+
"type": "Record<PropertyKey, unknown>[]",
|
|
157
|
+
"description": "One or more source objects to merge into the target"
|
|
158
158
|
}
|
|
159
159
|
],
|
|
160
160
|
"returns": {
|
|
161
161
|
"type": "undefined",
|
|
162
|
-
"description": "The
|
|
162
|
+
"description": "The mutated target"
|
|
163
163
|
}
|
|
164
164
|
},
|
|
165
165
|
{
|
|
166
|
-
"signature": "deepMerge(target: null, sources: Record<
|
|
167
|
-
"description": "Merges two or more objects deeply",
|
|
166
|
+
"signature": "deepMerge(target: null, sources: Record<PropertyKey, unknown>[]): null",
|
|
167
|
+
"description": "Merges two or more objects deeply.\n\nRecursively merges own enumerable properties — both string and symbol keys.\nPlain objects are merged recursively; all other values (arrays, class instances,\nprimitives, etc.) are replaced by the source value.\n`undefined` source values do not overwrite existing target values.",
|
|
168
168
|
"params": [
|
|
169
169
|
{
|
|
170
170
|
"name": "target",
|
|
171
171
|
"type": "null",
|
|
172
|
-
"description": "The target object"
|
|
172
|
+
"description": "The target object (mutated in place)"
|
|
173
173
|
},
|
|
174
174
|
{
|
|
175
175
|
"name": "sources",
|
|
176
|
-
"type": "Record<
|
|
177
|
-
"description": "
|
|
176
|
+
"type": "Record<PropertyKey, unknown>[]",
|
|
177
|
+
"description": "One or more source objects to merge into the target"
|
|
178
178
|
}
|
|
179
179
|
],
|
|
180
180
|
"returns": {
|
|
181
181
|
"type": "null",
|
|
182
|
-
"description": "The
|
|
182
|
+
"description": "The mutated target"
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
],
|
|
@@ -331,33 +331,33 @@
|
|
|
331
331
|
{
|
|
332
332
|
"name": "get",
|
|
333
333
|
"kind": "function",
|
|
334
|
-
"description": "Gets a value from an object using a dot-notated path",
|
|
334
|
+
"description": "Gets a value from an object using a dot/bracket-notated path or explicit key array.\n\n**Two path forms are supported:**\n\n1. **String path** — dot notation (`'a.b.c'`) and bracket notation (`'layers[1].name'`)\n are both accepted and mixed freely. Segments are traversed as string keys; `[n]`\n indices become numeric keys.\n\n2. **Key array** (`PropertyKey[]`) — explicit array of `string | number | symbol` keys,\n no parsing performed. Enables symbol-keyed traversal and compile-time type inference:\n `get(obj, ['a', 'b'] as const)` infers the return type from the path.",
|
|
335
335
|
"since": "1.9.0",
|
|
336
336
|
"signatures": [
|
|
337
337
|
{
|
|
338
338
|
"signature": "get<T = unknown>(obj: unknown, path: string, defaultValue?: T): T | undefined",
|
|
339
|
-
"description": "Gets a value from an object using a dot-notated path",
|
|
339
|
+
"description": "Gets a value from an object using a dot/bracket-notated path or explicit key array.\n\n**Two path forms are supported:**\n\n1. **String path** — dot notation (`'a.b.c'`) and bracket notation (`'layers[1].name'`)\n are both accepted and mixed freely. Segments are traversed as string keys; `[n]`\n indices become numeric keys.\n\n2. **Key array** (`PropertyKey[]`) — explicit array of `string | number | symbol` keys,\n no parsing performed. Enables symbol-keyed traversal and compile-time type inference:\n `get(obj, ['a', 'b'] as const)` infers the return type from the path.",
|
|
340
340
|
"params": [
|
|
341
341
|
{
|
|
342
342
|
"name": "obj",
|
|
343
343
|
"type": "unknown",
|
|
344
|
-
"description": "The object to
|
|
344
|
+
"description": "The object to read from"
|
|
345
345
|
},
|
|
346
346
|
{
|
|
347
347
|
"name": "path",
|
|
348
348
|
"type": "string",
|
|
349
|
-
"description": "
|
|
349
|
+
"description": "Dot/bracket-notation string or explicit `PropertyKey[]`"
|
|
350
350
|
},
|
|
351
351
|
{
|
|
352
352
|
"name": "defaultValue",
|
|
353
353
|
"type": "T",
|
|
354
|
-
"description": "
|
|
354
|
+
"description": "Returned when the path is absent or resolves to `undefined`",
|
|
355
355
|
"optional": true
|
|
356
356
|
}
|
|
357
357
|
],
|
|
358
358
|
"returns": {
|
|
359
359
|
"type": "T | undefined",
|
|
360
|
-
"description": "The value at the path or
|
|
360
|
+
"description": "The value at the path, or `defaultValue`"
|
|
361
361
|
},
|
|
362
362
|
"typeParameters": [
|
|
363
363
|
{
|
|
@@ -365,6 +365,42 @@
|
|
|
365
365
|
"default": "unknown"
|
|
366
366
|
}
|
|
367
367
|
]
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
"signature": "get<T extends object, Path extends readonly PropertyKey[]>(obj: T, path: Path, defaultValue?: DeepGet<T, Path>): DeepGet<T, Path> | undefined",
|
|
371
|
+
"description": "Gets a value from an object using a dot/bracket-notated path or explicit key array.\n\n**Two path forms are supported:**\n\n1. **String path** — dot notation (`'a.b.c'`) and bracket notation (`'layers[1].name'`)\n are both accepted and mixed freely. Segments are traversed as string keys; `[n]`\n indices become numeric keys.\n\n2. **Key array** (`PropertyKey[]`) — explicit array of `string | number | symbol` keys,\n no parsing performed. Enables symbol-keyed traversal and compile-time type inference:\n `get(obj, ['a', 'b'] as const)` infers the return type from the path.",
|
|
372
|
+
"params": [
|
|
373
|
+
{
|
|
374
|
+
"name": "obj",
|
|
375
|
+
"type": "T",
|
|
376
|
+
"description": "The object to read from"
|
|
377
|
+
},
|
|
378
|
+
{
|
|
379
|
+
"name": "path",
|
|
380
|
+
"type": "Path",
|
|
381
|
+
"description": "Dot/bracket-notation string or explicit `PropertyKey[]`"
|
|
382
|
+
},
|
|
383
|
+
{
|
|
384
|
+
"name": "defaultValue",
|
|
385
|
+
"type": "DeepGet<T, Path>",
|
|
386
|
+
"description": "Returned when the path is absent or resolves to `undefined`",
|
|
387
|
+
"optional": true
|
|
388
|
+
}
|
|
389
|
+
],
|
|
390
|
+
"returns": {
|
|
391
|
+
"type": "DeepGet<T, Path> | undefined",
|
|
392
|
+
"description": "The value at the path, or `defaultValue`"
|
|
393
|
+
},
|
|
394
|
+
"typeParameters": [
|
|
395
|
+
{
|
|
396
|
+
"name": "T",
|
|
397
|
+
"constraint": "object"
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
"name": "Path",
|
|
401
|
+
"constraint": "readonly PropertyKey[]"
|
|
402
|
+
}
|
|
403
|
+
]
|
|
368
404
|
}
|
|
369
405
|
],
|
|
370
406
|
"examples": [
|
|
@@ -377,6 +413,11 @@
|
|
|
377
413
|
"title": "Return default for missing path",
|
|
378
414
|
"description": "Returns the default value when the path does not exist.",
|
|
379
415
|
"code": "get({ a: 1 }, 'b.c', 'default')\n// => 'default'"
|
|
416
|
+
},
|
|
417
|
+
{
|
|
418
|
+
"title": "Get via key array (supports symbols)",
|
|
419
|
+
"description": "Pass an explicit PropertyKey[] to bypass parsing. Supports string, number, and symbol keys.",
|
|
420
|
+
"code": "const id = Symbol('id')\nget({ [id]: 'alice' }, [id])\n// => 'alice'"
|
|
380
421
|
}
|
|
381
422
|
],
|
|
382
423
|
"sourceFile": "get.ts"
|
|
@@ -477,6 +518,78 @@
|
|
|
477
518
|
],
|
|
478
519
|
"sourceFile": "invert.ts"
|
|
479
520
|
},
|
|
521
|
+
{
|
|
522
|
+
"name": "isEmpty",
|
|
523
|
+
"kind": "function",
|
|
524
|
+
"description": "Checks if a plain object has no own enumerable string-keyed properties.\n\nSymbol-keyed properties are not counted. Use `Object.getOwnPropertySymbols`\nseparately if symbol keys matter for your use case.",
|
|
525
|
+
"since": "2.0.3",
|
|
526
|
+
"signatures": [
|
|
527
|
+
{
|
|
528
|
+
"signature": "isEmpty(value: Record<PropertyKey, unknown>): boolean",
|
|
529
|
+
"description": "Checks if a plain object has no own enumerable string-keyed properties.\n\nSymbol-keyed properties are not counted. Use `Object.getOwnPropertySymbols`\nseparately if symbol keys matter for your use case.",
|
|
530
|
+
"params": [
|
|
531
|
+
{
|
|
532
|
+
"name": "value",
|
|
533
|
+
"type": "Record<PropertyKey, unknown>",
|
|
534
|
+
"description": "The object to check"
|
|
535
|
+
}
|
|
536
|
+
],
|
|
537
|
+
"returns": {
|
|
538
|
+
"type": "boolean",
|
|
539
|
+
"description": "`true` if the object has no own enumerable string-keyed properties"
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
],
|
|
543
|
+
"examples": [
|
|
544
|
+
{
|
|
545
|
+
"title": "Check if an object has no own string-keyed properties",
|
|
546
|
+
"description": "Returns true for `{}`. Symbol-keyed properties are not counted.",
|
|
547
|
+
"code": "isEmpty({}) // => true\nisEmpty({ a: 1 }) // => false\nisEmpty({ a: undefined }) // => false (key exists even if value is undefined)"
|
|
548
|
+
},
|
|
549
|
+
{
|
|
550
|
+
"title": "Symbol keys are not counted",
|
|
551
|
+
"description": "An object with only symbol-keyed properties is considered empty.",
|
|
552
|
+
"code": "const sym = Symbol('x');\nconst obj = { [sym]: 1 };\nisEmpty(obj) // => true (only string keys are counted)"
|
|
553
|
+
}
|
|
554
|
+
],
|
|
555
|
+
"sourceFile": "isEmpty.ts"
|
|
556
|
+
},
|
|
557
|
+
{
|
|
558
|
+
"name": "isNonEmpty",
|
|
559
|
+
"kind": "function",
|
|
560
|
+
"description": "Checks if a plain object has at least one own enumerable string-keyed property.\n\nSymbol-keyed properties are not counted. Use `Object.getOwnPropertySymbols`\nseparately if symbol keys matter for your use case.",
|
|
561
|
+
"since": "2.0.3",
|
|
562
|
+
"signatures": [
|
|
563
|
+
{
|
|
564
|
+
"signature": "isNonEmpty(value: Record<PropertyKey, unknown>): boolean",
|
|
565
|
+
"description": "Checks if a plain object has at least one own enumerable string-keyed property.\n\nSymbol-keyed properties are not counted. Use `Object.getOwnPropertySymbols`\nseparately if symbol keys matter for your use case.",
|
|
566
|
+
"params": [
|
|
567
|
+
{
|
|
568
|
+
"name": "value",
|
|
569
|
+
"type": "Record<PropertyKey, unknown>",
|
|
570
|
+
"description": "The object to check"
|
|
571
|
+
}
|
|
572
|
+
],
|
|
573
|
+
"returns": {
|
|
574
|
+
"type": "boolean",
|
|
575
|
+
"description": "`true` if the object has at least one own enumerable string-keyed property"
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
],
|
|
579
|
+
"examples": [
|
|
580
|
+
{
|
|
581
|
+
"title": "Check if an object has own string-keyed properties",
|
|
582
|
+
"description": "Returns true when at least one own enumerable string key is present.",
|
|
583
|
+
"code": "isNonEmpty({ a: 1 }) // => true\nisNonEmpty({ a: undefined }) // => true (key exists)\nisNonEmpty({}) // => false"
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
"title": "Guard before iterating object keys",
|
|
587
|
+
"description": "Use isNonEmpty before looping to avoid processing empty objects.",
|
|
588
|
+
"code": "function processConfig(config: Record<string, unknown>): void {\n if (!isNonEmpty(config)) {\n console.warn('Config is empty');\n return;\n }\n for (const key of Object.keys(config)) {\n // process each key\n }\n}"
|
|
589
|
+
}
|
|
590
|
+
],
|
|
591
|
+
"sourceFile": "isNonEmpty.ts"
|
|
592
|
+
},
|
|
480
593
|
{
|
|
481
594
|
"name": "map",
|
|
482
595
|
"kind": "function",
|
|
@@ -868,40 +981,89 @@
|
|
|
868
981
|
{
|
|
869
982
|
"name": "set",
|
|
870
983
|
"kind": "function",
|
|
871
|
-
"description": "Sets a value in an object
|
|
984
|
+
"description": "Sets a value in an object at the given path, creating intermediate objects as needed.\n\n**Three path forms are supported:**\n\n1. **Dot notation** (`string`) — segments split on `.` are kept as-is string keys.\n `\"layers.1.name\"` → keys `[\"layers\", \"1\", \"name\"]` (all strings, including `\"1\"`).\n\n2. **Bracket notation** (`string`) — `[n]` segments are parsed as numeric keys.\n `\"layers[1].name\"` → keys `[\"layers\", 1, \"name\"]` (index `1` becomes a number).\n Dot and bracket can be mixed freely: `\"a[0].b[2].c\"`.\n\n3. **Key array** (`PropertyKey[]`) — explicit array of `string | number | symbol` keys,\n no parsing performed. Enables full key-type control, including symbols:\n `[\"layers\", 1, Symbol('id')]`.\n\nIntermediate nodes that are absent, `null`, or not an object are replaced with `{}`.\nAny path containing a string segment equal to `__proto__`, `constructor`, or `prototype`\nis rejected and the original object is returned unchanged (prototype-pollution guard).",
|
|
872
985
|
"since": "1.9.0",
|
|
873
986
|
"signatures": [
|
|
874
987
|
{
|
|
875
988
|
"signature": "set(obj: Record<string, unknown>, path: string, value: unknown): Record<string, unknown>",
|
|
876
|
-
"description": "Sets a value in an object
|
|
989
|
+
"description": "Sets a value in an object at the given path, creating intermediate objects as needed.\n\n**Three path forms are supported:**\n\n1. **Dot notation** (`string`) — segments split on `.` are kept as-is string keys.\n `\"layers.1.name\"` → keys `[\"layers\", \"1\", \"name\"]` (all strings, including `\"1\"`).\n\n2. **Bracket notation** (`string`) — `[n]` segments are parsed as numeric keys.\n `\"layers[1].name\"` → keys `[\"layers\", 1, \"name\"]` (index `1` becomes a number).\n Dot and bracket can be mixed freely: `\"a[0].b[2].c\"`.\n\n3. **Key array** (`PropertyKey[]`) — explicit array of `string | number | symbol` keys,\n no parsing performed. Enables full key-type control, including symbols:\n `[\"layers\", 1, Symbol('id')]`.\n\nIntermediate nodes that are absent, `null`, or not an object are replaced with `{}`.\nAny path containing a string segment equal to `__proto__`, `constructor`, or `prototype`\nis rejected and the original object is returned unchanged (prototype-pollution guard).",
|
|
877
990
|
"params": [
|
|
878
991
|
{
|
|
879
992
|
"name": "obj",
|
|
880
993
|
"type": "Record<string, unknown>",
|
|
881
|
-
"description": "The object to
|
|
994
|
+
"description": "The object to mutate"
|
|
882
995
|
},
|
|
883
996
|
{
|
|
884
997
|
"name": "path",
|
|
885
998
|
"type": "string",
|
|
886
|
-
"description": "
|
|
999
|
+
"description": "Dot/bracket-notation string or explicit `PropertyKey[]`"
|
|
887
1000
|
},
|
|
888
1001
|
{
|
|
889
1002
|
"name": "value",
|
|
890
1003
|
"type": "unknown",
|
|
891
|
-
"description": "
|
|
1004
|
+
"description": "Value to assign at the path"
|
|
892
1005
|
}
|
|
893
1006
|
],
|
|
894
1007
|
"returns": {
|
|
895
1008
|
"type": "Record<string, unknown>",
|
|
896
|
-
"description": "The
|
|
1009
|
+
"description": "The mutated object (same reference)"
|
|
897
1010
|
}
|
|
1011
|
+
},
|
|
1012
|
+
{
|
|
1013
|
+
"signature": "set<T extends object, Path extends readonly PropertyKey[], V extends unknown>(obj: T, path: Path, value: V): DeepSet<T, Path, V>",
|
|
1014
|
+
"description": "Sets a value in an object at the given path, creating intermediate objects as needed.\n\n**Three path forms are supported:**\n\n1. **Dot notation** (`string`) — segments split on `.` are kept as-is string keys.\n `\"layers.1.name\"` → keys `[\"layers\", \"1\", \"name\"]` (all strings, including `\"1\"`).\n\n2. **Bracket notation** (`string`) — `[n]` segments are parsed as numeric keys.\n `\"layers[1].name\"` → keys `[\"layers\", 1, \"name\"]` (index `1` becomes a number).\n Dot and bracket can be mixed freely: `\"a[0].b[2].c\"`.\n\n3. **Key array** (`PropertyKey[]`) — explicit array of `string | number | symbol` keys,\n no parsing performed. Enables full key-type control, including symbols:\n `[\"layers\", 1, Symbol('id')]`.\n\nIntermediate nodes that are absent, `null`, or not an object are replaced with `{}`.\nAny path containing a string segment equal to `__proto__`, `constructor`, or `prototype`\nis rejected and the original object is returned unchanged (prototype-pollution guard).",
|
|
1015
|
+
"params": [
|
|
1016
|
+
{
|
|
1017
|
+
"name": "obj",
|
|
1018
|
+
"type": "T",
|
|
1019
|
+
"description": "The object to mutate"
|
|
1020
|
+
},
|
|
1021
|
+
{
|
|
1022
|
+
"name": "path",
|
|
1023
|
+
"type": "Path",
|
|
1024
|
+
"description": "Dot/bracket-notation string or explicit `PropertyKey[]`"
|
|
1025
|
+
},
|
|
1026
|
+
{
|
|
1027
|
+
"name": "value",
|
|
1028
|
+
"type": "V",
|
|
1029
|
+
"description": "Value to assign at the path"
|
|
1030
|
+
}
|
|
1031
|
+
],
|
|
1032
|
+
"returns": {
|
|
1033
|
+
"type": "DeepSet<T, Path, V>",
|
|
1034
|
+
"description": "The mutated object (same reference)"
|
|
1035
|
+
},
|
|
1036
|
+
"typeParameters": [
|
|
1037
|
+
{
|
|
1038
|
+
"name": "T",
|
|
1039
|
+
"constraint": "object"
|
|
1040
|
+
},
|
|
1041
|
+
{
|
|
1042
|
+
"name": "Path",
|
|
1043
|
+
"constraint": "readonly PropertyKey[]"
|
|
1044
|
+
},
|
|
1045
|
+
{
|
|
1046
|
+
"name": "V",
|
|
1047
|
+
"constraint": "unknown"
|
|
1048
|
+
}
|
|
1049
|
+
]
|
|
898
1050
|
}
|
|
899
1051
|
],
|
|
900
1052
|
"examples": [
|
|
901
1053
|
{
|
|
902
|
-
"title": "Set a nested property",
|
|
903
|
-
"description": "Creates intermediate objects as needed
|
|
904
|
-
"code": "set({}, 'a.b.c', 42)\n// => { a: { b: { c: 42 } } }"
|
|
1054
|
+
"title": "Set a nested property (dot notation)",
|
|
1055
|
+
"description": "Creates intermediate objects as needed. All segments are string keys — including numeric-looking ones like \"1\".",
|
|
1056
|
+
"code": "set({}, 'a.b.c', 42)\n// => { a: { b: { c: 42 } } }\n\nset({}, 'layers.1.name', 'bg')\n// => { layers: { '1': { name: 'bg' } } } // '1' is a string key"
|
|
1057
|
+
},
|
|
1058
|
+
{
|
|
1059
|
+
"title": "Set via bracket notation",
|
|
1060
|
+
"description": "Square-bracket indices become numeric keys. Useful when the path targets an array element.",
|
|
1061
|
+
"code": "const obj = { layers: [{}, { name: 'old' }] }\nset(obj, 'layers[1].name', 'new')\n// => { layers: [{}, { name: 'new' }] }"
|
|
1062
|
+
},
|
|
1063
|
+
{
|
|
1064
|
+
"title": "Set via key array (supports symbols)",
|
|
1065
|
+
"description": "Pass an explicit PropertyKey[] to bypass parsing. Supports string, number, and symbol keys.",
|
|
1066
|
+
"code": "const id = Symbol('id')\nset({}, ['user', id], 'alice')\n// => { user: { [id]: 'alice' } }"
|
|
905
1067
|
}
|
|
906
1068
|
],
|
|
907
1069
|
"sourceFile": "set.ts"
|
package/meta/examples.json
CHANGED
|
@@ -88,6 +88,11 @@
|
|
|
88
88
|
"title": "Return default for missing path",
|
|
89
89
|
"description": "Returns the default value when the path does not exist.",
|
|
90
90
|
"code": "get({ a: 1 }, 'b.c', 'default')\n// => 'default'"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"title": "Get via key array (supports symbols)",
|
|
94
|
+
"description": "Pass an explicit PropertyKey[] to bypass parsing. Supports string, number, and symbol keys.",
|
|
95
|
+
"code": "const id = Symbol('id')\nget({ [id]: 'alice' }, [id])\n// => 'alice'"
|
|
91
96
|
}
|
|
92
97
|
]
|
|
93
98
|
},
|
|
@@ -121,6 +126,36 @@
|
|
|
121
126
|
}
|
|
122
127
|
]
|
|
123
128
|
},
|
|
129
|
+
{
|
|
130
|
+
"name": "isEmpty",
|
|
131
|
+
"examples": [
|
|
132
|
+
{
|
|
133
|
+
"title": "Check if an object has no own string-keyed properties",
|
|
134
|
+
"description": "Returns true for `{}`. Symbol-keyed properties are not counted.",
|
|
135
|
+
"code": "isEmpty({}) // => true\nisEmpty({ a: 1 }) // => false\nisEmpty({ a: undefined }) // => false (key exists even if value is undefined)"
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
"title": "Symbol keys are not counted",
|
|
139
|
+
"description": "An object with only symbol-keyed properties is considered empty.",
|
|
140
|
+
"code": "const sym = Symbol('x');\nconst obj = { [sym]: 1 };\nisEmpty(obj) // => true (only string keys are counted)"
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"name": "isNonEmpty",
|
|
146
|
+
"examples": [
|
|
147
|
+
{
|
|
148
|
+
"title": "Check if an object has own string-keyed properties",
|
|
149
|
+
"description": "Returns true when at least one own enumerable string key is present.",
|
|
150
|
+
"code": "isNonEmpty({ a: 1 }) // => true\nisNonEmpty({ a: undefined }) // => true (key exists)\nisNonEmpty({}) // => false"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"title": "Guard before iterating object keys",
|
|
154
|
+
"description": "Use isNonEmpty before looping to avoid processing empty objects.",
|
|
155
|
+
"code": "function processConfig(config: Record<string, unknown>): void {\n if (!isNonEmpty(config)) {\n console.warn('Config is empty');\n return;\n }\n for (const key of Object.keys(config)) {\n // process each key\n }\n}"
|
|
156
|
+
}
|
|
157
|
+
]
|
|
158
|
+
},
|
|
124
159
|
{
|
|
125
160
|
"name": "map",
|
|
126
161
|
"examples": [
|
|
@@ -205,9 +240,19 @@
|
|
|
205
240
|
"name": "set",
|
|
206
241
|
"examples": [
|
|
207
242
|
{
|
|
208
|
-
"title": "Set a nested property",
|
|
209
|
-
"description": "Creates intermediate objects as needed
|
|
210
|
-
"code": "set({}, 'a.b.c', 42)\n// => { a: { b: { c: 42 } } }"
|
|
243
|
+
"title": "Set a nested property (dot notation)",
|
|
244
|
+
"description": "Creates intermediate objects as needed. All segments are string keys — including numeric-looking ones like \"1\".",
|
|
245
|
+
"code": "set({}, 'a.b.c', 42)\n// => { a: { b: { c: 42 } } }\n\nset({}, 'layers.1.name', 'bg')\n// => { layers: { '1': { name: 'bg' } } } // '1' is a string key"
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
"title": "Set via bracket notation",
|
|
249
|
+
"description": "Square-bracket indices become numeric keys. Useful when the path targets an array element.",
|
|
250
|
+
"code": "const obj = { layers: [{}, { name: 'old' }] }\nset(obj, 'layers[1].name', 'new')\n// => { layers: [{}, { name: 'new' }] }"
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
"title": "Set via key array (supports symbols)",
|
|
254
|
+
"description": "Pass an explicit PropertyKey[] to bypass parsing. Supports string, number, and symbol keys.",
|
|
255
|
+
"code": "const id = Symbol('id')\nset({}, ['user', id], 'alice')\n// => { user: { [id]: 'alice' } }"
|
|
211
256
|
}
|
|
212
257
|
]
|
|
213
258
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/object",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for object.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
6
|
"license": "LGPL-3.0",
|
|
@@ -36,6 +36,8 @@
|
|
|
36
36
|
"get",
|
|
37
37
|
"groupBy",
|
|
38
38
|
"invert",
|
|
39
|
+
"isEmpty",
|
|
40
|
+
"isNonEmpty",
|
|
39
41
|
"map",
|
|
40
42
|
"omit",
|
|
41
43
|
"pick",
|