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