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