@rimbu/deep 2.0.0 → 2.0.2
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 +37 -27
- package/dist/bun/deep.mts +2 -2
- package/dist/bun/match.mts +27 -25
- package/dist/bun/patch.mts +10 -9
- package/dist/bun/path.mts +94 -95
- package/dist/bun/protected.mts +18 -17
- package/dist/bun/selector.mts +22 -20
- package/dist/bun/tuple.mts +1 -1
- package/dist/cjs/deep.cjs +197 -576
- package/dist/cjs/deep.cjs.map +1 -0
- package/dist/cjs/deep.d.cts +284 -0
- package/dist/cjs/index.cjs +18 -662
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +18 -0
- package/dist/cjs/internal.cjs +8 -582
- package/dist/cjs/internal.cjs.map +1 -0
- package/dist/cjs/internal.d.cts +7 -0
- package/dist/cjs/match.cjs +250 -343
- package/dist/cjs/match.cjs.map +1 -0
- package/dist/cjs/match.d.cts +140 -0
- package/dist/cjs/patch.cjs +120 -93
- package/dist/cjs/patch.cjs.map +1 -0
- package/dist/cjs/patch.d.cts +89 -0
- package/dist/cjs/path.cjs +114 -573
- package/dist/cjs/path.cjs.map +1 -0
- package/dist/cjs/path.d.cts +199 -0
- package/dist/cjs/protected.cjs +2 -17
- package/dist/cjs/protected.cjs.map +1 -0
- package/dist/cjs/selector.cjs +32 -574
- package/dist/cjs/selector.cjs.map +1 -0
- package/dist/cjs/selector.d.cts +47 -0
- package/dist/cjs/tuple.cjs +162 -71
- package/dist/cjs/tuple.cjs.map +1 -0
- package/dist/esm/match.mjs.map +1 -1
- package/dist/esm/patch.mjs.map +1 -1
- package/dist/{types → esm}/path.d.mts +4 -1
- package/dist/esm/path.mjs.map +1 -1
- package/dist/esm/protected.d.mts +17 -0
- package/dist/esm/selector.mjs.map +1 -1
- package/dist/esm/tuple.d.mts +142 -0
- package/package.json +20 -14
- package/src/deep.mts +2 -2
- package/src/match.mts +27 -25
- package/src/patch.mts +10 -9
- package/src/path.mts +94 -95
- package/src/protected.mts +18 -17
- package/src/selector.mts +22 -20
- package/src/tuple.mts +1 -1
- /package/dist/{types/protected.d.mts → cjs/protected.d.cts} +0 -0
- /package/dist/{types/tuple.d.mts → cjs/tuple.d.cts} +0 -0
- /package/dist/{types → esm}/deep.d.mts +0 -0
- /package/dist/{types → esm}/index.d.mts +0 -0
- /package/dist/{types → esm}/internal.d.mts +0 -0
- /package/dist/{types → esm}/match.d.mts +0 -0
- /package/dist/{types → esm}/patch.d.mts +0 -0
- /package/dist/{types → esm}/selector.d.mts +0 -0
package/dist/cjs/path.cjs
CHANGED
|
@@ -1,585 +1,126 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
if (Array.isArray(source)) {
|
|
69
|
-
return matchArr(source, parent, root, matcher, failureLog);
|
|
70
|
-
}
|
|
71
|
-
failureLog?.push(
|
|
72
|
-
`value ${JSON.stringify(
|
|
73
|
-
source
|
|
74
|
-
)} does not match given matcher ${JSON.stringify(matcher)}`
|
|
75
|
-
);
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
function matchArr(source, parent, root, matcher, failureLog) {
|
|
79
|
-
if (Array.isArray(matcher)) {
|
|
80
|
-
const length = source.length;
|
|
81
|
-
if (length !== matcher.length) {
|
|
82
|
-
failureLog?.push(
|
|
83
|
-
`array lengths are not equal: value length ${source.length} !== matcher length ${matcher.length}`
|
|
84
|
-
);
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
let index = -1;
|
|
88
|
-
while (++index < length) {
|
|
89
|
-
if (!matchEntry(source[index], source, root, matcher[index], failureLog)) {
|
|
90
|
-
failureLog?.push(
|
|
91
|
-
`index ${index} does not match with value ${JSON.stringify(
|
|
92
|
-
source[index]
|
|
93
|
-
)} and matcher ${matcher[index]}`
|
|
94
|
-
);
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
if (typeof matcher === "object" && null !== matcher) {
|
|
101
|
-
if (`every` in matcher) {
|
|
102
|
-
return matchCompound(
|
|
103
|
-
source,
|
|
104
|
-
parent,
|
|
105
|
-
root,
|
|
106
|
-
["every", ...matcher.every],
|
|
107
|
-
failureLog
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
if (`some` in matcher) {
|
|
111
|
-
return matchCompound(
|
|
112
|
-
source,
|
|
113
|
-
parent,
|
|
114
|
-
root,
|
|
115
|
-
["some", ...matcher.some],
|
|
116
|
-
failureLog
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
if (`none` in matcher) {
|
|
120
|
-
return matchCompound(
|
|
121
|
-
source,
|
|
122
|
-
parent,
|
|
123
|
-
root,
|
|
124
|
-
["none", ...matcher.none],
|
|
125
|
-
failureLog
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
if (`single` in matcher) {
|
|
129
|
-
return matchCompound(
|
|
130
|
-
source,
|
|
131
|
-
parent,
|
|
132
|
-
root,
|
|
133
|
-
["single", ...matcher.single],
|
|
134
|
-
failureLog
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
if (`someItem` in matcher) {
|
|
138
|
-
return matchTraversal(
|
|
139
|
-
source,
|
|
140
|
-
root,
|
|
141
|
-
"someItem",
|
|
142
|
-
matcher.someItem,
|
|
143
|
-
failureLog
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
if (`everyItem` in matcher) {
|
|
147
|
-
return matchTraversal(
|
|
148
|
-
source,
|
|
149
|
-
root,
|
|
150
|
-
"everyItem",
|
|
151
|
-
matcher.everyItem,
|
|
152
|
-
failureLog
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
if (`noneItem` in matcher) {
|
|
156
|
-
return matchTraversal(
|
|
157
|
-
source,
|
|
158
|
-
root,
|
|
159
|
-
"noneItem",
|
|
160
|
-
matcher.noneItem,
|
|
161
|
-
failureLog
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
if (`singleItem` in matcher) {
|
|
165
|
-
return matchTraversal(
|
|
166
|
-
source,
|
|
167
|
-
root,
|
|
168
|
-
"singleItem",
|
|
169
|
-
matcher.singleItem,
|
|
170
|
-
failureLog
|
|
171
|
-
);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
for (const index in matcher) {
|
|
175
|
-
const matcherAtIndex = matcher[index];
|
|
176
|
-
if (!(index in source)) {
|
|
177
|
-
failureLog?.push(
|
|
178
|
-
`index ${index} does not exist in source ${JSON.stringify(
|
|
179
|
-
source
|
|
180
|
-
)} but should match matcher ${JSON.stringify(matcherAtIndex)}`
|
|
181
|
-
);
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
const result = matchEntry(
|
|
185
|
-
source[index],
|
|
186
|
-
source,
|
|
187
|
-
root,
|
|
188
|
-
matcherAtIndex,
|
|
189
|
-
failureLog
|
|
190
|
-
);
|
|
191
|
-
if (!result) {
|
|
192
|
-
failureLog?.push(
|
|
193
|
-
`index ${index} does not match with value ${JSON.stringify(
|
|
194
|
-
source[index]
|
|
195
|
-
)} and matcher ${JSON.stringify(matcherAtIndex)}`
|
|
196
|
-
);
|
|
197
|
-
return false;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
function matchPlainObj(source, parent, root, matcher, failureLog) {
|
|
203
|
-
if (Array.isArray(matcher)) {
|
|
204
|
-
return matchCompound(source, parent, root, matcher, failureLog);
|
|
205
|
-
}
|
|
206
|
-
for (const key in matcher) {
|
|
207
|
-
if (!(key in source)) {
|
|
208
|
-
failureLog?.push(
|
|
209
|
-
`key ${key} is specified in matcher but not present in value ${JSON.stringify(
|
|
210
|
-
source
|
|
211
|
-
)}`
|
|
212
|
-
);
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
const result = matchEntry(
|
|
216
|
-
source[key],
|
|
217
|
-
source,
|
|
218
|
-
root,
|
|
219
|
-
matcher[key],
|
|
220
|
-
failureLog
|
|
221
|
-
);
|
|
222
|
-
if (!result) {
|
|
223
|
-
failureLog?.push(
|
|
224
|
-
`key ${key} does not match in value ${JSON.stringify(
|
|
225
|
-
source[key]
|
|
226
|
-
)} with matcher ${JSON.stringify(matcher[key])}`
|
|
227
|
-
);
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
return true;
|
|
232
|
-
}
|
|
233
|
-
function matchCompound(source, parent, root, compound, failureLog) {
|
|
234
|
-
const matchType = compound[0];
|
|
235
|
-
const length = compound.length;
|
|
236
|
-
let index = 0;
|
|
237
|
-
switch (matchType) {
|
|
238
|
-
case "every": {
|
|
239
|
-
while (++index < length) {
|
|
240
|
-
const result = matchEntry(
|
|
241
|
-
source,
|
|
242
|
-
parent,
|
|
243
|
-
root,
|
|
244
|
-
compound[index],
|
|
245
|
-
failureLog
|
|
246
|
-
);
|
|
247
|
-
if (!result) {
|
|
248
|
-
failureLog?.push(
|
|
249
|
-
`in compound "every": match at index ${index} failed`
|
|
250
|
-
);
|
|
251
|
-
return false;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return true;
|
|
255
|
-
}
|
|
256
|
-
case "none": {
|
|
257
|
-
while (++index < length) {
|
|
258
|
-
const result = matchEntry(
|
|
259
|
-
source,
|
|
260
|
-
parent,
|
|
261
|
-
root,
|
|
262
|
-
compound[index],
|
|
263
|
-
failureLog
|
|
264
|
-
);
|
|
265
|
-
if (result) {
|
|
266
|
-
failureLog?.push(
|
|
267
|
-
`in compound "none": match at index ${index} succeeded`
|
|
268
|
-
);
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
case "single": {
|
|
275
|
-
let onePassed = false;
|
|
276
|
-
while (++index < length) {
|
|
277
|
-
const result = matchEntry(
|
|
278
|
-
source,
|
|
279
|
-
parent,
|
|
280
|
-
root,
|
|
281
|
-
compound[index],
|
|
282
|
-
failureLog
|
|
283
|
-
);
|
|
284
|
-
if (result) {
|
|
285
|
-
if (onePassed) {
|
|
286
|
-
failureLog?.push(
|
|
287
|
-
`in compound "single": multiple matches succeeded`
|
|
288
|
-
);
|
|
289
|
-
return false;
|
|
290
|
-
}
|
|
291
|
-
onePassed = true;
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Path = void 0;
|
|
4
|
+
exports.getAt = getAt;
|
|
5
|
+
exports.patchAt = patchAt;
|
|
6
|
+
var tslib_1 = require("tslib");
|
|
7
|
+
var internal_cjs_1 = require("./internal.cjs");
|
|
8
|
+
var Path;
|
|
9
|
+
(function (Path) {
|
|
10
|
+
/**
|
|
11
|
+
* Regular expression used to split a path string into tokens.
|
|
12
|
+
*/
|
|
13
|
+
Path.stringSplitRegex = /\?\.|\.|\[|\]/g;
|
|
14
|
+
/**
|
|
15
|
+
* Return the given `path` string split into an array of subpaths.
|
|
16
|
+
* @param path - the input string path
|
|
17
|
+
*/
|
|
18
|
+
function stringSplit(path) {
|
|
19
|
+
return path.split(Path.stringSplitRegex);
|
|
20
|
+
}
|
|
21
|
+
Path.stringSplit = stringSplit;
|
|
22
|
+
})(Path || (exports.Path = Path = {}));
|
|
23
|
+
/**
|
|
24
|
+
* Returns the value resulting from selecting the given `path` in the given `source` object.
|
|
25
|
+
* It supports optional chaining for nullable values or values that may be undefined, and also
|
|
26
|
+
* for accessing objects inside an array.
|
|
27
|
+
* There is currently no support for forcing non-null (the `!` operator).
|
|
28
|
+
* @typeparam T - the object type to select in
|
|
29
|
+
* @typeparam P - a Path in object type T
|
|
30
|
+
* @param source - the object to select in
|
|
31
|
+
* @param path - the path into the object
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const value = { a: { b: { c: [{ d: 5 }, { d: 6 }] } } }
|
|
35
|
+
* Deep.getAt(value, 'a.b');
|
|
36
|
+
* // => { c: 5 }
|
|
37
|
+
* Deep.getAt(value, 'a.b.c');
|
|
38
|
+
* // => [{ d: 5 }, { d: 5 }]
|
|
39
|
+
* Deep.getAt(value, 'a.b.c[1]');
|
|
40
|
+
* // => { d: 6 }
|
|
41
|
+
* Deep.getAt(value, 'a.b.c[1]?.d');
|
|
42
|
+
* // => 6
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function getAt(source, path) {
|
|
46
|
+
var e_1, _a;
|
|
47
|
+
if (path === '') {
|
|
48
|
+
// empty path always directly returns source value
|
|
49
|
+
return source;
|
|
50
|
+
}
|
|
51
|
+
var items = Path.stringSplit(path);
|
|
52
|
+
// start with `source` as result value
|
|
53
|
+
var result = source;
|
|
54
|
+
try {
|
|
55
|
+
for (var items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
|
|
56
|
+
var item = items_1_1.value;
|
|
57
|
+
if (undefined === item || item === '' || item === '[') {
|
|
58
|
+
// ignore irrelevant items
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (undefined === result || null === result) {
|
|
62
|
+
// optional chaining assumed and no value available, skip rest of path and return undefined
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
// set current result to subpath value
|
|
66
|
+
result = result[item];
|
|
292
67
|
}
|
|
293
|
-
}
|
|
294
|
-
if (!onePassed) {
|
|
295
|
-
failureLog?.push(`in compound "single": no matches succeeded`);
|
|
296
|
-
}
|
|
297
|
-
return onePassed;
|
|
298
68
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
parent,
|
|
304
|
-
root,
|
|
305
|
-
compound[index],
|
|
306
|
-
failureLog
|
|
307
|
-
);
|
|
308
|
-
if (result) {
|
|
309
|
-
return true;
|
|
69
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
70
|
+
finally {
|
|
71
|
+
try {
|
|
72
|
+
if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
|
|
310
73
|
}
|
|
311
|
-
|
|
312
|
-
failureLog?.push(`in compound "some": no matches succeeded`);
|
|
313
|
-
return false;
|
|
74
|
+
finally { if (e_1) throw e_1.error; }
|
|
314
75
|
}
|
|
315
|
-
|
|
76
|
+
return result;
|
|
316
77
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Patches the value at the given path in the source to the given value.
|
|
80
|
+
* Because the path to update must exist in the `source` object, optional
|
|
81
|
+
* chaining and array indexing is not allowed.
|
|
82
|
+
* @param source - the object to update
|
|
83
|
+
* @param path - the path in the object to update
|
|
84
|
+
* @param patchItem - the patch for the value at the given path
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* const value = { a: { b: { c: 5 } } };
|
|
88
|
+
* Deep.patchAt(value, 'a.b.c', v => v + 5);
|
|
89
|
+
* // => { a: { b: { c: 6 } } }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
function patchAt(source, path, patchItem) {
|
|
93
|
+
if (path === '') {
|
|
94
|
+
return internal_cjs_1.Deep.patch(source, patchItem);
|
|
95
|
+
}
|
|
96
|
+
var items = Path.stringSplit(path);
|
|
97
|
+
// creates a patch object based on the current path
|
|
98
|
+
function createPatchPart(index, target) {
|
|
99
|
+
var _a;
|
|
100
|
+
if (index === items.length) {
|
|
101
|
+
// processed all items, return the input `patchItem`
|
|
102
|
+
return patchItem;
|
|
325
103
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
return false;
|
|
331
|
-
}
|
|
332
|
-
case "everyItem": {
|
|
333
|
-
while (++index < length) {
|
|
334
|
-
if (!matchEntry(source[index], source, root, matcher, failureLog)) {
|
|
335
|
-
failureLog?.push(
|
|
336
|
-
`in array traversal "everyItem": at least one item did not match given matcher`
|
|
337
|
-
);
|
|
338
|
-
return false;
|
|
104
|
+
var item = items[index];
|
|
105
|
+
if (undefined === item || item === '') {
|
|
106
|
+
// empty items can be ignored
|
|
107
|
+
return createPatchPart(index + 1, target);
|
|
339
108
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
case "noneItem": {
|
|
344
|
-
while (++index < length) {
|
|
345
|
-
if (matchEntry(source[index], source, root, matcher, failureLog)) {
|
|
346
|
-
failureLog?.push(
|
|
347
|
-
`in array traversal "noneItem": at least one item matched given matcher`
|
|
348
|
-
);
|
|
349
|
-
return false;
|
|
109
|
+
if (item === '[') {
|
|
110
|
+
// next item is array index, set arrayMode to true
|
|
111
|
+
return createPatchPart(index + 1, target);
|
|
350
112
|
}
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
if (singleMatched) {
|
|
359
|
-
failureLog?.push(
|
|
360
|
-
`in array traversal "singleItem": more than one item matched given matcher`
|
|
361
|
-
);
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
singleMatched = true;
|
|
113
|
+
// create object with subPart as property key, and the restuls of processing next parts as value
|
|
114
|
+
var result = (_a = {},
|
|
115
|
+
_a[item] = createPatchPart(index + 1, target[item]),
|
|
116
|
+
_a);
|
|
117
|
+
if (Array.isArray(target)) {
|
|
118
|
+
// target in source object is array/tuple, so the patch should be object
|
|
119
|
+
return result;
|
|
365
120
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
failureLog?.push(
|
|
369
|
-
`in array traversal "singleItem": no item matched given matcher`
|
|
370
|
-
);
|
|
371
|
-
return false;
|
|
372
|
-
}
|
|
373
|
-
return true;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// src/deep.mts
|
|
379
|
-
var deep_exports = {};
|
|
380
|
-
__export(deep_exports, {
|
|
381
|
-
getAt: () => getAt,
|
|
382
|
-
getAtWith: () => getAtWith,
|
|
383
|
-
match: () => match,
|
|
384
|
-
matchAt: () => matchAt,
|
|
385
|
-
matchAtWith: () => matchAtWith,
|
|
386
|
-
matchWith: () => matchWith,
|
|
387
|
-
patch: () => patch,
|
|
388
|
-
patchAt: () => patchAt,
|
|
389
|
-
patchAtWith: () => patchAtWith,
|
|
390
|
-
patchWith: () => patchWith,
|
|
391
|
-
protect: () => protect,
|
|
392
|
-
select: () => select,
|
|
393
|
-
selectAt: () => selectAt,
|
|
394
|
-
selectAtWith: () => selectAtWith,
|
|
395
|
-
selectWith: () => selectWith,
|
|
396
|
-
withType: () => withType
|
|
397
|
-
});
|
|
398
|
-
|
|
399
|
-
// src/patch.mts
|
|
400
|
-
var import_base2 = require("@rimbu/base");
|
|
401
|
-
function patch(value, patchItem) {
|
|
402
|
-
return patchEntry(value, value, value, patchItem);
|
|
403
|
-
}
|
|
404
|
-
function patchEntry(value, parent, root, patchItem) {
|
|
405
|
-
if (Object.is(value, patchItem)) {
|
|
406
|
-
return value;
|
|
407
|
-
}
|
|
408
|
-
if (typeof value === "function") {
|
|
409
|
-
return patchItem;
|
|
410
|
-
}
|
|
411
|
-
if (typeof patchItem === "function") {
|
|
412
|
-
const item = patchItem(value, parent, root);
|
|
413
|
-
return patchEntry(value, parent, root, item);
|
|
414
|
-
}
|
|
415
|
-
if ((0, import_base2.isPlainObj)(value)) {
|
|
416
|
-
return patchPlainObj(value, root, patchItem);
|
|
417
|
-
}
|
|
418
|
-
if (Array.isArray(value)) {
|
|
419
|
-
return patchArr(value, root, patchItem);
|
|
420
|
-
}
|
|
421
|
-
return patchItem;
|
|
422
|
-
}
|
|
423
|
-
function patchPlainObj(value, root, patchItem) {
|
|
424
|
-
if (!Array.isArray(patchItem)) {
|
|
425
|
-
return patchItem;
|
|
426
|
-
}
|
|
427
|
-
const result = { ...value };
|
|
428
|
-
let anyChange = false;
|
|
429
|
-
for (const entry of patchItem) {
|
|
430
|
-
const currentRoot = value === root ? { ...result } : root;
|
|
431
|
-
const parent = { ...result };
|
|
432
|
-
for (const key in entry) {
|
|
433
|
-
const currentValue = result[key];
|
|
434
|
-
const newValue = patchEntry(
|
|
435
|
-
currentValue,
|
|
436
|
-
parent,
|
|
437
|
-
currentRoot,
|
|
438
|
-
entry[key]
|
|
439
|
-
);
|
|
440
|
-
if (!Object.is(currentValue, newValue)) {
|
|
441
|
-
anyChange = true;
|
|
442
|
-
result[key] = newValue;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
if (anyChange) {
|
|
447
|
-
return result;
|
|
448
|
-
}
|
|
449
|
-
return value;
|
|
450
|
-
}
|
|
451
|
-
function patchArr(value, root, patchItem) {
|
|
452
|
-
if (Array.isArray(patchItem)) {
|
|
453
|
-
return patchItem;
|
|
454
|
-
}
|
|
455
|
-
const result = [...value];
|
|
456
|
-
let anyChange = false;
|
|
457
|
-
for (const index in patchItem) {
|
|
458
|
-
const numIndex = index;
|
|
459
|
-
const currentValue = result[numIndex];
|
|
460
|
-
const newValue = patchEntry(
|
|
461
|
-
currentValue,
|
|
462
|
-
value,
|
|
463
|
-
root,
|
|
464
|
-
patchItem[index]
|
|
465
|
-
);
|
|
466
|
-
if (!Object.is(newValue, currentValue)) {
|
|
467
|
-
anyChange = true;
|
|
468
|
-
result[numIndex] = newValue;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
if (anyChange) {
|
|
472
|
-
return result;
|
|
473
|
-
}
|
|
474
|
-
return value;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
// src/selector.mts
|
|
478
|
-
function select(source, selector) {
|
|
479
|
-
if (typeof selector === "function") {
|
|
480
|
-
return selector(source);
|
|
481
|
-
} else if (typeof selector === "string") {
|
|
482
|
-
return deep_exports.getAt(source, selector);
|
|
483
|
-
} else if (Array.isArray(selector)) {
|
|
484
|
-
return selector.map((s) => select(source, s));
|
|
485
|
-
}
|
|
486
|
-
const result = {};
|
|
487
|
-
for (const key in selector) {
|
|
488
|
-
result[key] = select(source, selector[key]);
|
|
489
|
-
}
|
|
490
|
-
return result;
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
// src/deep.mts
|
|
494
|
-
function protect(source) {
|
|
495
|
-
return source;
|
|
496
|
-
}
|
|
497
|
-
function getAtWith(path) {
|
|
498
|
-
return (source) => deep_exports.getAt(source, path);
|
|
499
|
-
}
|
|
500
|
-
function patchWith(patchItem) {
|
|
501
|
-
return (source) => deep_exports.patch(source, patchItem);
|
|
502
|
-
}
|
|
503
|
-
function patchAtWith(path, patchItem) {
|
|
504
|
-
return (source) => deep_exports.patchAt(source, path, patchItem);
|
|
505
|
-
}
|
|
506
|
-
function matchWith(matcher) {
|
|
507
|
-
return (source) => deep_exports.match(source, matcher);
|
|
508
|
-
}
|
|
509
|
-
function matchAt(source, path, matcher) {
|
|
510
|
-
return deep_exports.match(deep_exports.getAt(source, path), matcher);
|
|
511
|
-
}
|
|
512
|
-
function matchAtWith(path, matcher) {
|
|
513
|
-
return (source) => deep_exports.matchAt(source, path, matcher);
|
|
514
|
-
}
|
|
515
|
-
function selectWith(selector) {
|
|
516
|
-
return (source) => deep_exports.select(source, selector);
|
|
517
|
-
}
|
|
518
|
-
function selectAt(source, path, selector) {
|
|
519
|
-
return deep_exports.select(deep_exports.getAt(source, path), selector);
|
|
520
|
-
}
|
|
521
|
-
function selectAtWith(path, selector) {
|
|
522
|
-
return (source) => deep_exports.selectAt(source, path, selector);
|
|
523
|
-
}
|
|
524
|
-
function withType() {
|
|
525
|
-
return deep_exports;
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
// src/path.mts
|
|
529
|
-
var Path;
|
|
530
|
-
((Path2) => {
|
|
531
|
-
Path2.stringSplitRegex = /\?\.|\.|\[|\]/g;
|
|
532
|
-
function stringSplit(path) {
|
|
533
|
-
return path.split(Path2.stringSplitRegex);
|
|
534
|
-
}
|
|
535
|
-
Path2.stringSplit = stringSplit;
|
|
536
|
-
})(Path || (Path = {}));
|
|
537
|
-
function getAt(source, path) {
|
|
538
|
-
if (path === "") {
|
|
539
|
-
return source;
|
|
540
|
-
}
|
|
541
|
-
const items = Path.stringSplit(path);
|
|
542
|
-
let result = source;
|
|
543
|
-
for (const item of items) {
|
|
544
|
-
if (void 0 === item || item === "" || item === "[") {
|
|
545
|
-
continue;
|
|
546
|
-
}
|
|
547
|
-
if (void 0 === result || null === result) {
|
|
548
|
-
return void 0;
|
|
549
|
-
}
|
|
550
|
-
result = result[item];
|
|
551
|
-
}
|
|
552
|
-
return result;
|
|
553
|
-
}
|
|
554
|
-
function patchAt(source, path, patchItem) {
|
|
555
|
-
if (path === "") {
|
|
556
|
-
return deep_exports.patch(source, patchItem);
|
|
557
|
-
}
|
|
558
|
-
const items = Path.stringSplit(path);
|
|
559
|
-
function createPatchPart(index, target) {
|
|
560
|
-
if (index === items.length) {
|
|
561
|
-
return patchItem;
|
|
562
|
-
}
|
|
563
|
-
const item = items[index];
|
|
564
|
-
if (void 0 === item || item === "") {
|
|
565
|
-
return createPatchPart(index + 1, target);
|
|
566
|
-
}
|
|
567
|
-
if (item === "[") {
|
|
568
|
-
return createPatchPart(index + 1, target);
|
|
569
|
-
}
|
|
570
|
-
const result = {
|
|
571
|
-
[item]: createPatchPart(index + 1, target[item])
|
|
572
|
-
};
|
|
573
|
-
if (Array.isArray(target)) {
|
|
574
|
-
return result;
|
|
121
|
+
// target in source is not an array, so it patch should be an array
|
|
122
|
+
return [result];
|
|
575
123
|
}
|
|
576
|
-
return
|
|
577
|
-
}
|
|
578
|
-
return deep_exports.patch(source, createPatchPart(0, source));
|
|
124
|
+
return internal_cjs_1.Deep.patch(source, createPatchPart(0, source));
|
|
579
125
|
}
|
|
580
|
-
|
|
581
|
-
0 && (module.exports = {
|
|
582
|
-
Path,
|
|
583
|
-
getAt,
|
|
584
|
-
patchAt
|
|
585
|
-
});
|
|
126
|
+
//# sourceMappingURL=path.cjs.map
|