@tolki/path 0.0.0-stage → 1.0.0
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 +21 -0
- package/README.md +4 -2
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/path.d.ts +448 -0
- package/dist/path.d.ts.map +1 -0
- package/dist/path.js +684 -0
- package/dist/pluck.d.ts +68 -0
- package/dist/pluck.d.ts.map +1 -0
- package/dist/pluck.js +62 -0
- package/package.json +48 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Abraham Arango
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center"><img src="https://raw.githubusercontent.com/abetwothree/tolki/refs/heads/master/docs/vitepress/public/tolki-logo-short.svg" width="50%" alt="Laravel Tolki JS Logo"></p>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# Tolki JS Path Package
|
|
4
|
+
|
|
5
|
+
This package provides path utility functions used by other Tolki packages. Usage of this package directly is not recommended, as they are subject to change without notice.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { MAX_UNDOT_INDEX as a, dotFlatten as s, dotFlattenArray as d, dotFlattenObject as l, forgetKeys as r, forgetKeysArray as u, forgetKeysObject as o, getMixedValue as c, getNestedValue as n, getObjectValue as h, getRaw as x, hasMixed as i, hasObjectKey as P, hasPath as b, isCanonicalUndotIndex as g, parseSegments as p, pushMixed as y, pushWithPath as m, setImmutable as O, setMixed as j, setMixedImmutable as A, setObjectValue as M, undotExpand as f, undotExpandArray as k, undotExpandObject as E } from "./path.js";
|
|
2
|
+
import { explodePluckPath as V, getPluckWildcardValues as I, hasPluckPath as F, isArrayAccess as N, isEnumerable as D, readPluckKey as U, resolvePluckPath as W } from "./pluck.js";
|
|
3
|
+
export {
|
|
4
|
+
a as MAX_UNDOT_INDEX,
|
|
5
|
+
s as dotFlatten,
|
|
6
|
+
d as dotFlattenArray,
|
|
7
|
+
l as dotFlattenObject,
|
|
8
|
+
V as explodePluckPath,
|
|
9
|
+
r as forgetKeys,
|
|
10
|
+
u as forgetKeysArray,
|
|
11
|
+
o as forgetKeysObject,
|
|
12
|
+
c as getMixedValue,
|
|
13
|
+
n as getNestedValue,
|
|
14
|
+
h as getObjectValue,
|
|
15
|
+
I as getPluckWildcardValues,
|
|
16
|
+
x as getRaw,
|
|
17
|
+
i as hasMixed,
|
|
18
|
+
P as hasObjectKey,
|
|
19
|
+
b as hasPath,
|
|
20
|
+
F as hasPluckPath,
|
|
21
|
+
N as isArrayAccess,
|
|
22
|
+
g as isCanonicalUndotIndex,
|
|
23
|
+
D as isEnumerable,
|
|
24
|
+
p as parseSegments,
|
|
25
|
+
y as pushMixed,
|
|
26
|
+
m as pushWithPath,
|
|
27
|
+
U as readPluckKey,
|
|
28
|
+
W as resolvePluckPath,
|
|
29
|
+
O as setImmutable,
|
|
30
|
+
j as setMixed,
|
|
31
|
+
A as setMixedImmutable,
|
|
32
|
+
M as setObjectValue,
|
|
33
|
+
f as undotExpand,
|
|
34
|
+
k as undotExpandArray,
|
|
35
|
+
E as undotExpandObject
|
|
36
|
+
};
|
package/dist/path.d.ts
ADDED
|
@@ -0,0 +1,448 @@
|
|
|
1
|
+
import { ArrayItems, MapData, MapEntryValue, PathKey, PathKeys, UndotObjectValue, UndotValue } from '@tolki/types';
|
|
2
|
+
/**
|
|
3
|
+
* Parse a key into segments for mixed array/object path traversal.
|
|
4
|
+
* Converts dot notation strings and numbers into path segments that can be
|
|
5
|
+
* either numeric indices (for arrays) or string keys (for objects). A segment
|
|
6
|
+
* is an index only when PHP would store it as an integer key, so "01" stays a string.
|
|
7
|
+
*
|
|
8
|
+
* @param key - The key to parse (number, string, null, or undefined).
|
|
9
|
+
* @returns Array of path segments, or null if invalid.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* Parse different key types
|
|
14
|
+
* parseSegments(5); -> [5]
|
|
15
|
+
* parseSegments("1.2.3"); -> [1, 2, 3] (numeric segments)
|
|
16
|
+
* parseSegments("user.name"); -> ["user", "name"] (string segments)
|
|
17
|
+
* parseSegments("0.user.1.name"); -> [0, "user", 1, "name"] (mixed segments)
|
|
18
|
+
* parseSegments("0.01"); -> [0, "01"] (a non-canonical index is a string key)
|
|
19
|
+
* parseSegments(null); -> []
|
|
20
|
+
*/
|
|
21
|
+
export declare function parseSegments(key: PathKey): (number | string)[] | null;
|
|
22
|
+
/**
|
|
23
|
+
* Check if a path exists in a nested array/object structure.
|
|
24
|
+
* Traverses the structure using dot notation with mixed array indices and object keys.
|
|
25
|
+
*
|
|
26
|
+
* @param root - The root structure to search in.
|
|
27
|
+
* @param key - The path to check (number, string, null, or undefined).
|
|
28
|
+
* @returns True if the path exists, false otherwise.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
*
|
|
32
|
+
* Check path existence in arrays
|
|
33
|
+
* hasPath([['a', 'b'], ['c', 'd']], "0.1"); -> true
|
|
34
|
+
* hasPath([['a', 'b']], "1.0"); -> false
|
|
35
|
+
* hasPath(['x', 'y'], 1); -> true
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
*
|
|
39
|
+
* Check path existence in objects
|
|
40
|
+
* hasPath([{name: 'John', age: 30}], "0.name"); -> true
|
|
41
|
+
* hasPath({user: {profile: {name: 'Jane'}}}, "user.profile.name"); -> true
|
|
42
|
+
* hasPath({items: ['a', 'b']}, "items.1"); -> true
|
|
43
|
+
* hasPath([{0: 'x'}], "0.0"); -> true
|
|
44
|
+
*/
|
|
45
|
+
export declare function hasPath<TValue, TKey extends PropertyKey = PropertyKey>(root: TValue[] | Record<TKey, TValue>, key: PathKey): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Get a value from a nested array/object structure using dot notation.
|
|
48
|
+
* Returns an object indicating whether the value was found and its value.
|
|
49
|
+
*
|
|
50
|
+
* @param root - The root structure to search in.
|
|
51
|
+
* @param key - The path to retrieve (number, string, null, or undefined).
|
|
52
|
+
* @returns Object with found status and value.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
*
|
|
56
|
+
* Get values from arrays
|
|
57
|
+
* getRaw([['a', 'b'], ['c']], "0.1"); -> { found: true, value: 'b' }
|
|
58
|
+
* getRaw([['a']], "1.0"); -> { found: false }
|
|
59
|
+
* getRaw(['x', 'y'], null); -> { found: true, value: ['x', 'y'] }
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
*
|
|
63
|
+
* Get values from objects
|
|
64
|
+
* getRaw([{name: 'John', age: 30}], "0.name"); -> { found: true, value: 'John' }
|
|
65
|
+
* getRaw({user: {profile: {name: 'Jane'}}}, "user.profile.name"); -> { found: true, value: 'Jane' }
|
|
66
|
+
* getRaw({items: ['a', 'b']}, "items.1"); -> { found: true, value: 'b' }
|
|
67
|
+
* getRaw([{0: 'x'}], "0.0"); -> { found: true, value: 'x' }
|
|
68
|
+
*/
|
|
69
|
+
export declare function getRaw<TValue, TKey extends PropertyKey = PropertyKey>(root: TValue[] | Record<TKey, TValue>, key: PathKey): {
|
|
70
|
+
found: boolean;
|
|
71
|
+
value?: unknown;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Remove items from an array or object using dot notation keys (immutable).
|
|
75
|
+
* Creates a new structure with specified items removed, supporting nested paths.
|
|
76
|
+
*
|
|
77
|
+
* @param data - The data to remove items from.
|
|
78
|
+
* @param keys - The key(s) to remove (number, string, or array of keys).
|
|
79
|
+
* @returns A new structure with the specified items removed.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
*
|
|
83
|
+
* Remove keys from objects and arrays
|
|
84
|
+
* forgetKeys({a: 1, b: 2}, 'a'); -> {b: 2}
|
|
85
|
+
* forgetKeys(['x', 'y', 'z'], 1); -> ['x', 'z']
|
|
86
|
+
* forgetKeys([{name: 'John', age: 30}], '0.age'); -> [{name: 'John'}]
|
|
87
|
+
*/
|
|
88
|
+
export declare function forgetKeys<TValue, TKey extends PropertyKey = PropertyKey>(data: Record<TKey, TValue> | ArrayItems<TValue>, keys: PathKeys): Record<TKey, TValue> | TValue[];
|
|
89
|
+
/**
|
|
90
|
+
* Remove properties from an object using dot notation keys (immutable).
|
|
91
|
+
* Creates a new object with specified properties removed, supporting nested paths.
|
|
92
|
+
* Mirrors Laravel's fixed Arr::forget behavior:
|
|
93
|
+
* - Each key is resolved from the top level of the object.
|
|
94
|
+
* - A key that exists literally at the top level (even if it contains dots)
|
|
95
|
+
* is removed literally instead of being dot-traversed.
|
|
96
|
+
* - A null key names the '' key, and a float is looked up by its string form,
|
|
97
|
+
* as Arr::exists casts them; a float that names a literal key removes its
|
|
98
|
+
* integer part instead, as unset casts it.
|
|
99
|
+
* - If an intermediate path segment is missing or not traversable, the key
|
|
100
|
+
* is skipped entirely and nothing is removed for that key.
|
|
101
|
+
* - Traversal descends into both plain objects and arrays, the two shapes
|
|
102
|
+
* that stand in for a PHP array. Class instances and built-ins such as
|
|
103
|
+
* Date are not traversable and are left untouched. A Map is traversed as the
|
|
104
|
+
* PHP array it stands for and loses the key without renumbering the others.
|
|
105
|
+
* When the final container is an array, a valid in-bounds integer leaf
|
|
106
|
+
* removes the item (via splice); otherwise the key is a no-op.
|
|
107
|
+
*
|
|
108
|
+
* @param data - The object to remove properties from.
|
|
109
|
+
* @param keys - The key(s) to remove (string or array of strings).
|
|
110
|
+
* @returns A new object with the specified properties removed.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
*
|
|
114
|
+
* Remove object properties
|
|
115
|
+
* forgetKeysObject({a: 1, b: {c: 2, d: 3}}, 'a'); -> {b: {c: 2, d: 3}}
|
|
116
|
+
* forgetKeysObject({user: {name: 'John', age: 30}}, 'user.age'); -> {user: {name: 'John'}}
|
|
117
|
+
* forgetKeysObject({a: 1, b: 2, c: 3}, ['a', 'c']); -> {b: 2}
|
|
118
|
+
* forgetKeysObject({'products.desk': {price: 100}}, 'products.desk'); -> {}
|
|
119
|
+
* forgetKeysObject({a: {c: 5}}, 'a.b.c'); -> {a: {c: 5}}
|
|
120
|
+
* forgetKeysObject({products: {desk: ['a', 'b']}}, 'products.desk.0'); -> {products: {desk: ['b']}}
|
|
121
|
+
*/
|
|
122
|
+
export declare function forgetKeysObject<TValue, TKey extends PropertyKey = PropertyKey>(data: Record<TKey, TValue>, keys: PathKeys): Record<TKey, TValue>;
|
|
123
|
+
/**
|
|
124
|
+
* Remove one or many array items from a copy of the given array using "dot" notation, walking lists and plain objects.
|
|
125
|
+
*
|
|
126
|
+
* @param data - The array to remove items from.
|
|
127
|
+
* @param keys - The key(s) to remove (number, string, or array of keys).
|
|
128
|
+
* @returns A new array with the specified items removed.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
*
|
|
132
|
+
* Remove items by keys
|
|
133
|
+
* forgetKeysArray(['a', 'b', 'c'], 1); -> ['a', 'c']
|
|
134
|
+
* forgetKeysArray([['x', 'y'], ['z']], "0.1"); -> [['x'], ['z']]
|
|
135
|
+
* forgetKeysArray(['a', 'b', 'c'], [0, 2]); -> ['b']
|
|
136
|
+
* forgetKeysArray([{a: 1, b: 2}, {a: 3}], ['0.a', '1.a']); -> [{b: 2}, {}]
|
|
137
|
+
*/
|
|
138
|
+
export declare function forgetKeysArray<TValue>(data: ArrayItems<TValue>, keys: PathKeys): TValue[];
|
|
139
|
+
/**
|
|
140
|
+
* Set a value in an array using dot notation (immutable).
|
|
141
|
+
* Creates a new array with the value set at the specified path.
|
|
142
|
+
*
|
|
143
|
+
* @param data - The array to set the value in.
|
|
144
|
+
* @param key - The path where to set the value (number, string, null, or undefined).
|
|
145
|
+
* @param value - The value to set.
|
|
146
|
+
* @returns A new array with the value set at the specified path.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
*
|
|
150
|
+
* Set values using dot notation
|
|
151
|
+
* setImmutable(['a', 'b'], 1, 'x'); -> ['a', 'x']
|
|
152
|
+
* setImmutable([['old']], "0.0", 'new'); -> [['new']]
|
|
153
|
+
* setImmutable([], 0, 'first'); -> ['first']
|
|
154
|
+
*/
|
|
155
|
+
export declare function setImmutable<TValue>(data: ArrayItems<TValue> | unknown, key: PathKey, value: TValue): TValue[];
|
|
156
|
+
/**
|
|
157
|
+
* Push an item into an array using "dot" notation, walking lists, plain objects and Maps and replacing anything else
|
|
158
|
+
* along the path, as Arr::set does.
|
|
159
|
+
*
|
|
160
|
+
* @param data - The array to push values into.
|
|
161
|
+
* @param key - The path where to push values (number, string, null, or undefined).
|
|
162
|
+
* @param values - The values to push.
|
|
163
|
+
* @returns The modified array with values pushed at the specified path.
|
|
164
|
+
* @throws InvalidArgumentException if the value at the key is not an array, a plain object or a Map.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
*
|
|
168
|
+
* Push values using dot notation
|
|
169
|
+
* pushWithPath(['a'], null, 'b', 'c'); -> ['a', 'b', 'c']
|
|
170
|
+
* pushWithPath([['x']], "0", 'y'); -> [['x', 'y']]
|
|
171
|
+
* pushWithPath([], "0", 'first'); -> [['first']]
|
|
172
|
+
* pushWithPath([{x: [1]}], "0.x", 2); -> [{x: [1, 2]}]
|
|
173
|
+
* pushWithPath([{x: {k: 1}}], "0.x", 2); -> [{x: {k: 1, 0: 2}}]
|
|
174
|
+
* pushWithPath([null], "0.child", 'v'); -> [{child: ['v']}]
|
|
175
|
+
*/
|
|
176
|
+
export declare function pushWithPath<TValue>(data: TValue[] | unknown, key: PathKey, ...values: TValue[]): TValue[];
|
|
177
|
+
/**
|
|
178
|
+
* Flatten a nested structure into a flat object with dot notation keys.
|
|
179
|
+
* Converts nested arrays and plain objects into a single-level object with path-based keys;
|
|
180
|
+
* a non-empty Map is walked in its insertion order; any other object (a class instance or Date)
|
|
181
|
+
* is kept whole as a value.
|
|
182
|
+
*
|
|
183
|
+
* @param data - The data to flatten.
|
|
184
|
+
* @param prepend - Optional string to prepend to all keys.
|
|
185
|
+
* @param depth - Optional maximum depth to flatten (default is Infinity).
|
|
186
|
+
* @returns A flat object with dot-notated keys.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
*
|
|
190
|
+
* Flatten mixed structures
|
|
191
|
+
* dotFlatten({a: {b: 1}, c: [2, 3]}); -> {'a.b': 1, 'c.0': 2, 'c.1': 3}
|
|
192
|
+
* dotFlatten(['x', {y: 'z'}], 'prefix'); -> {prefix0: 'x', 'prefix1.y': 'z'}
|
|
193
|
+
*/
|
|
194
|
+
export declare function dotFlatten<TValue, TKey extends PropertyKey = PropertyKey>(data: Record<TKey, TValue> | ArrayItems<TValue> | unknown, prepend?: string, depth?: number): Record<PropertyKey, TValue>;
|
|
195
|
+
/**
|
|
196
|
+
* Flatten a nested object structure into a flat object with dot notation keys.
|
|
197
|
+
* Converts nested objects into a single-level object with path-based keys. A Map, at the root
|
|
198
|
+
* or nested, is read in its insertion order.
|
|
199
|
+
*
|
|
200
|
+
* @param data - The object or Map to flatten.
|
|
201
|
+
* @param prepend - Optional string to prepend to all keys.
|
|
202
|
+
* @param depth - Optional maximum depth to flatten (default is Infinity).
|
|
203
|
+
* @returns A flat object with dot-notated keys.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
*
|
|
207
|
+
* Flatten nested objects
|
|
208
|
+
* dotFlattenObject({a: {b: {c: 1}}}); -> {'a.b.c': 1}
|
|
209
|
+
* dotFlattenObject({user: {name: 'John'}}, 'data'); -> {'datauser.name': 'John'}
|
|
210
|
+
*/
|
|
211
|
+
export declare function dotFlattenObject<TValue, TKey extends PropertyKey = PropertyKey>(data: Record<TKey, TValue> | unknown, prepend?: string, depth?: number): Record<PropertyKey, TValue>;
|
|
212
|
+
/**
|
|
213
|
+
* Flatten a nested array structure into a flat object with dot notation keys.
|
|
214
|
+
* Converts nested arrays into a single-level object with path-based keys.
|
|
215
|
+
*
|
|
216
|
+
* @param data - The array to flatten.
|
|
217
|
+
* @param prepend - Optional string to prepend to all keys.
|
|
218
|
+
* @param depth - Optional maximum depth to flatten (default is Infinity).
|
|
219
|
+
* @returns A flat object with dot-notated keys.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
*
|
|
223
|
+
* Flatten nested arrays
|
|
224
|
+
* dotFlattenArray(['a', ['b', 'c']]); -> { '0': 'a', '1.0': 'b', '1.1': 'c' }
|
|
225
|
+
* dotFlattenArray([['x']], "prefix"); -> { 'prefix0.0': 'x' }
|
|
226
|
+
*/
|
|
227
|
+
export declare function dotFlattenArray<TValue>(data: ArrayItems<TValue> | unknown, prepend?: string, depth?: number): Record<PropertyKey, TValue>;
|
|
228
|
+
/**
|
|
229
|
+
* Expand a flat object with dot notation keys into a nested structure.
|
|
230
|
+
*
|
|
231
|
+
* Dispatches to {@link undotExpandObject} for a plain-object or Map `map`, a Map read in its insertion order;
|
|
232
|
+
* nested consecutive-integer containers become real arrays, but the root always stays an object.
|
|
233
|
+
*
|
|
234
|
+
* @param map - The flat object or Map with dot-notated keys.
|
|
235
|
+
* @returns A nested structure (array or object).
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
*
|
|
239
|
+
* undotExpand({'a.b.c': 1, 'a.d': 2}); -> {a: {b: {c: 1}, d: 2}}
|
|
240
|
+
* undotExpand(new Map([['a.b', 1]])); -> {a: {b: 1}}
|
|
241
|
+
*/
|
|
242
|
+
export declare function undotExpand<TMap>(map: MapData<TMap>): Record<string, UndotObjectValue<MapEntryValue<TMap>>>;
|
|
243
|
+
export declare function undotExpand<TValue, TKey extends PropertyKey = PropertyKey>(map: Record<TKey, TValue>): TValue[] | Record<TKey, TValue>;
|
|
244
|
+
/**
|
|
245
|
+
* Expand a flat object with dot notation keys into a nested object structure.
|
|
246
|
+
*
|
|
247
|
+
* Nested containers whose own keys are the consecutive integers `0..n-1` are
|
|
248
|
+
* rebuilt as real arrays (see {@link promoteConsecutiveIntegerContainers}); the
|
|
249
|
+
* root always stays a plain object. A Map is read in its insertion order.
|
|
250
|
+
*
|
|
251
|
+
* @param map - The flat object or Map with dot-notated keys.
|
|
252
|
+
* @returns A nested object structure.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
*
|
|
256
|
+
* undotExpandObject({'user.name': 'John', 'user.age': 30}); -> {user: {name: 'John', age: 30}}
|
|
257
|
+
* undotExpandObject(new Map([['a.b', 1], ['c', 2]])); -> {a: {b: 1}, c: 2}
|
|
258
|
+
*/
|
|
259
|
+
export declare function undotExpandObject<TMap>(map: MapData<TMap>): Record<string, UndotObjectValue<MapEntryValue<TMap>>>;
|
|
260
|
+
export declare function undotExpandObject<TValue, TKey extends PropertyKey = PropertyKey>(map: Record<TKey, TValue>): Record<TKey, TValue>;
|
|
261
|
+
/**
|
|
262
|
+
* Ceiling for a numeric `undot` segment: 2**24 keeps a fully materialized
|
|
263
|
+
* array under ~134MB. `Arr.undot` also caps each container's own max index,
|
|
264
|
+
* summed once per container, since N such containers cost N x ~134MB.
|
|
265
|
+
*/
|
|
266
|
+
export declare const MAX_UNDOT_INDEX: number;
|
|
267
|
+
/**
|
|
268
|
+
* Whether `segment` is a canonical decimal integer PHP would treat as an
|
|
269
|
+
* array key, and small enough to safely become a real array index.
|
|
270
|
+
*
|
|
271
|
+
* @param segment - The path segment to check.
|
|
272
|
+
* @returns True if `segment` is a safe, canonical array index.
|
|
273
|
+
*/
|
|
274
|
+
export declare function isCanonicalUndotIndex(segment: string): boolean;
|
|
275
|
+
/**
|
|
276
|
+
* Expand a flat object with dot notation keys into a nested array structure.
|
|
277
|
+
* Converts a flattened object back into its original nested array form. A Map is read
|
|
278
|
+
* in its insertion order.
|
|
279
|
+
*
|
|
280
|
+
* @param map - The flat object or Map with dot-notated keys.
|
|
281
|
+
* @returns A nested array structure.
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
*
|
|
285
|
+
* Expand flat object to nested arrays
|
|
286
|
+
* undotExpandArray({ '0': 'a', '1.0': 'b', '1.1': 'c' }); -> ['a', ['b', 'c']]
|
|
287
|
+
* undotExpandArray({ '0.0.0': 'deep' }); -> [[['deep']]]
|
|
288
|
+
* undotExpandArray(new Map([['1', 'b'], ['0', 'a']])); -> ['a', 'b']
|
|
289
|
+
*/
|
|
290
|
+
export declare function undotExpandArray<TMap>(map: MapData<TMap>): UndotValue<MapEntryValue<TMap>>[];
|
|
291
|
+
export declare function undotExpandArray<TValue, TKey extends PropertyKey = PropertyKey>(map: Record<TKey, TValue>): TValue[];
|
|
292
|
+
/**
|
|
293
|
+
* Helper function to get nested values using mixed dot notation.
|
|
294
|
+
* Supports both numeric indices for arrays and property names for objects.
|
|
295
|
+
* This utility enables traversal of complex nested structures where arrays
|
|
296
|
+
* and objects can be intermixed at any level.
|
|
297
|
+
*
|
|
298
|
+
* @param The object or array to traverse.
|
|
299
|
+
* @param path - The dot-notation path to traverse (e.g., "0.items.1.name").
|
|
300
|
+
* @returns The value at the specified path, or undefined if not found.
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
*
|
|
304
|
+
* Array with nested objects
|
|
305
|
+
* getNestedValue([{items: ['x', 'y']}], '0.items.1'); -> 'y'
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
*
|
|
309
|
+
* Object with nested arrays
|
|
310
|
+
* getNestedValue({users: [{name: 'John'}, {name: 'Jane'}]}, 'users.1.name'); -> 'Jane'
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
*
|
|
314
|
+
* Mixed nesting
|
|
315
|
+
* getNestedValue([{data: {values: [1, 2, 3]}}], '0.data.values.2'); -> 3
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
*
|
|
319
|
+
* A Map, read by the key PHP stores
|
|
320
|
+
* getNestedValue({m: new Map([[2, 'c'], [0, 'a']])}, 'm.0'); -> 'a'
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
*
|
|
324
|
+
* Path not found
|
|
325
|
+
* getNestedValue([{items: ['x']}], '0.items.5'); -> undefined
|
|
326
|
+
*/
|
|
327
|
+
export declare function getNestedValue<TReturn>(obj: unknown, path: PropertyKey): TReturn | undefined;
|
|
328
|
+
/**
|
|
329
|
+
* Get a value from a nested structure using mixed dot notation.
|
|
330
|
+
* Supports both numeric indices for arrays and property names for objects.
|
|
331
|
+
* This function bridges between the existing numeric-only getRaw function
|
|
332
|
+
* and the new mixed notation support.
|
|
333
|
+
*
|
|
334
|
+
* @param data - The data to search in.
|
|
335
|
+
* @param key - The dot-notation key.
|
|
336
|
+
* @param defaultValue - Default value if not found.
|
|
337
|
+
* @returns The found value or default.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
*
|
|
341
|
+
* Get values with mixed notation
|
|
342
|
+
* getMixedValue([{name: 'John'}], '0.name'); -> 'John'
|
|
343
|
+
* getMixedValue(['a', 'b'], 1); -> 'b'
|
|
344
|
+
* getMixedValue([], '0', 'default'); -> 'default'
|
|
345
|
+
*/
|
|
346
|
+
export declare function getMixedValue<TValue, TDefault = null>(data: ArrayItems<TValue> | unknown, key: PathKey, defaultValue?: TDefault | (() => TDefault) | null): TValue | TDefault | null;
|
|
347
|
+
/**
|
|
348
|
+
* Set a value in an array using mixed array/object dot notation (mutable version).
|
|
349
|
+
* Supports both numeric array indices and object property names in paths.
|
|
350
|
+
*
|
|
351
|
+
* @param The root array to modify.
|
|
352
|
+
* @param key - The path where to set the value.
|
|
353
|
+
* @param value - The value to set.
|
|
354
|
+
* @returns The modified original array.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
*
|
|
358
|
+
* setMixed([{ name: "John" }], "0.age", 30); -> [{ name: "John", age: 30 }]
|
|
359
|
+
* setMixed([], "user.name", "John"); -> [{ user: { name: "John" } }]
|
|
360
|
+
*/
|
|
361
|
+
export declare function setMixed<TValue>(arr: TValue[], key: PathKey, value: unknown): TValue[];
|
|
362
|
+
/**
|
|
363
|
+
* Push values to an array at the specified mixed path.
|
|
364
|
+
* Supports both numeric array indices and object property names in paths.
|
|
365
|
+
*
|
|
366
|
+
* @param data - The data to push values into.
|
|
367
|
+
* @param key - The path where to push (supports mixed array/object paths).
|
|
368
|
+
* @param values - The values to push.
|
|
369
|
+
* @returns The modified array.
|
|
370
|
+
*
|
|
371
|
+
* @example
|
|
372
|
+
*
|
|
373
|
+
* pushMixed([], '0', 'value'); -> [['value']]
|
|
374
|
+
* pushMixed([{items: []}], '0.items', 'new'); -> [{items: ['new']}]
|
|
375
|
+
*/
|
|
376
|
+
export declare function pushMixed<TValue>(data: TValue[] | unknown, key: PathKey, ...values: TValue[]): TValue[];
|
|
377
|
+
/**
|
|
378
|
+
* Set a value in an array using mixed array/object dot notation (immutable version).
|
|
379
|
+
* Supports both numeric array indices and object property names in paths.
|
|
380
|
+
*
|
|
381
|
+
* The copy is deep through arrays, plain objects and Maps, which stand for PHP arrays; a Map stays a Map in its key
|
|
382
|
+
* order. A class instance or a `Date` is aliased into the result rather than copied, because PHP holds an object by
|
|
383
|
+
* handle where it copies an array by value — copying one's entries flattened it into a plain object.
|
|
384
|
+
* Such a value is shared with the caller's input; the write path itself is never shared.
|
|
385
|
+
*
|
|
386
|
+
* @param data - The data to set the value in.
|
|
387
|
+
* @param key - The path where to set the value.
|
|
388
|
+
* @param value - The value to set.
|
|
389
|
+
* @returns A new array with the value set.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
*
|
|
393
|
+
* setMixedImmutable([{ name: "John" }], "0.age", 30); -> [{ name: "John", age: 30 }]
|
|
394
|
+
* setMixedImmutable([], "user.name", "John"); -> [{ user: { name: "John" } }]
|
|
395
|
+
*/
|
|
396
|
+
export declare function setMixedImmutable<TValue>(data: ArrayItems<TValue> | unknown, key: PathKey, value: TValue): TValue[];
|
|
397
|
+
/**
|
|
398
|
+
* Check if a key exists using mixed array/object dot notation.
|
|
399
|
+
*
|
|
400
|
+
* A literal key wins over path traversal even when it contains dots; this
|
|
401
|
+
* fast path only applies to plain objects, since a JS array's `in` operator
|
|
402
|
+
* also climbs `Array.prototype`.
|
|
403
|
+
*
|
|
404
|
+
* @param data - The data to check.
|
|
405
|
+
* @param key - The path to check.
|
|
406
|
+
* @returns True if the path exists, false otherwise.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
*
|
|
410
|
+
* hasMixed([{ name: "John" }], "0.name"); -> true
|
|
411
|
+
*/
|
|
412
|
+
export declare function hasMixed(data: unknown, key: PathKey): boolean;
|
|
413
|
+
/**
|
|
414
|
+
* Get a value from an object using dot notation.
|
|
415
|
+
* This is an object-specific version that handles object property access.
|
|
416
|
+
*
|
|
417
|
+
* A literal key wins over dot-path traversal even when it contains dots,
|
|
418
|
+
* and a literal key whose value is `undefined` still counts as found.
|
|
419
|
+
*
|
|
420
|
+
* @param obj - The object to get the value from.
|
|
421
|
+
* @param key - The key or dot-notated path.
|
|
422
|
+
* @param defaultValue - The default value if the key is not found.
|
|
423
|
+
* @returns The value or the default value.
|
|
424
|
+
*/
|
|
425
|
+
export declare function getObjectValue<TValue, TKey extends PropertyKey = PropertyKey, TReturn = unknown, TDefault = null>(obj: Record<TKey, TValue> | unknown, key: PathKey, defaultValue?: TDefault | (() => TDefault) | null): TReturn | TDefault | null;
|
|
426
|
+
/**
|
|
427
|
+
* Set a value in an object using dot notation.
|
|
428
|
+
* This is an object-specific version that creates a new object.
|
|
429
|
+
*
|
|
430
|
+
* @param obj - The object to set the value in.
|
|
431
|
+
* @param key - The key or dot-notated path.
|
|
432
|
+
* @param value - The value to set.
|
|
433
|
+
* @returns A new object with the value set.
|
|
434
|
+
*/
|
|
435
|
+
export declare function setObjectValue<TValue, TKey extends PropertyKey = PropertyKey>(obj: Record<TKey, TValue>, key: PathKey | null, value: unknown): Record<TKey, TValue>;
|
|
436
|
+
/**
|
|
437
|
+
* Check if a key exists in an object using dot notation.
|
|
438
|
+
*
|
|
439
|
+
* A literal key that itself contains dots wins over path traversal:
|
|
440
|
+
* `{ "products.desk": {} }` must report the literal key as found without
|
|
441
|
+
* ever treating it as `products` -> `desk`.
|
|
442
|
+
*
|
|
443
|
+
* @param obj - The object to check.
|
|
444
|
+
* @param key - The key or dot-notated path.
|
|
445
|
+
* @returns True if the key exists, false otherwise.
|
|
446
|
+
*/
|
|
447
|
+
export declare function hasObjectKey<TValue, TKey extends PropertyKey = PropertyKey>(obj: Record<TKey, TValue> | unknown, key: PathKey): boolean;
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