@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.
Files changed (37) hide show
  1. package/dist/cjs/deep.cjs +197 -576
  2. package/dist/cjs/deep.cjs.map +1 -0
  3. package/dist/cjs/deep.d.cts +284 -0
  4. package/dist/cjs/index.cjs +18 -662
  5. package/dist/cjs/index.cjs.map +1 -0
  6. package/dist/cjs/index.d.cts +18 -0
  7. package/dist/cjs/internal.cjs +8 -582
  8. package/dist/cjs/internal.cjs.map +1 -0
  9. package/dist/cjs/internal.d.cts +7 -0
  10. package/dist/cjs/match.cjs +251 -343
  11. package/dist/cjs/match.cjs.map +1 -0
  12. package/dist/cjs/match.d.cts +140 -0
  13. package/dist/cjs/patch.cjs +121 -93
  14. package/dist/cjs/patch.cjs.map +1 -0
  15. package/dist/cjs/patch.d.cts +89 -0
  16. package/dist/cjs/path.cjs +114 -573
  17. package/dist/cjs/path.cjs.map +1 -0
  18. package/dist/cjs/path.d.cts +196 -0
  19. package/dist/cjs/protected.cjs +2 -17
  20. package/dist/cjs/protected.cjs.map +1 -0
  21. package/dist/cjs/selector.cjs +33 -574
  22. package/dist/cjs/selector.cjs.map +1 -0
  23. package/dist/cjs/selector.d.cts +47 -0
  24. package/dist/cjs/tuple.cjs +162 -71
  25. package/dist/cjs/tuple.cjs.map +1 -0
  26. package/dist/esm/protected.d.mts +17 -0
  27. package/dist/esm/tuple.d.mts +142 -0
  28. package/package.json +20 -14
  29. /package/dist/{types/protected.d.mts → cjs/protected.d.cts} +0 -0
  30. /package/dist/{types/tuple.d.mts → cjs/tuple.d.cts} +0 -0
  31. /package/dist/{types → esm}/deep.d.mts +0 -0
  32. /package/dist/{types → esm}/index.d.mts +0 -0
  33. /package/dist/{types → esm}/internal.d.mts +0 -0
  34. /package/dist/{types → esm}/match.d.mts +0 -0
  35. /package/dist/{types → esm}/patch.d.mts +0 -0
  36. /package/dist/{types → esm}/path.d.mts +0 -0
  37. /package/dist/{types → esm}/selector.d.mts +0 -0
package/dist/cjs/path.cjs CHANGED
@@ -1,585 +1,126 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/path.mts
21
- var path_exports = {};
22
- __export(path_exports, {
23
- Path: () => Path,
24
- getAt: () => getAt,
25
- patchAt: () => patchAt
26
- });
27
- module.exports = __toCommonJS(path_exports);
28
-
29
- // src/match.mts
30
- var import_base = require("@rimbu/base");
31
- function match(source, matcher, failureLog) {
32
- return matchEntry(source, source, source, matcher, failureLog);
33
- }
34
- function matchEntry(source, parent, root, matcher, failureLog) {
35
- if (Object.is(source, matcher)) {
36
- return true;
37
- }
38
- if (matcher === null || matcher === void 0) {
39
- failureLog?.push(
40
- `value ${JSON.stringify(source)} did not match matcher ${matcher}`
41
- );
42
- return false;
43
- }
44
- if (typeof source === "function") {
45
- const result = Object.is(source, matcher);
46
- if (!result) {
47
- failureLog?.push(
48
- `both value and matcher are functions, but they do not have the same reference`
49
- );
50
- }
51
- return result;
52
- }
53
- if (typeof matcher === "function") {
54
- const matcherResult = matcher(source, parent, root);
55
- if (typeof matcherResult === "boolean") {
56
- if (!matcherResult) {
57
- failureLog?.push(
58
- `function matcher returned false for value ${JSON.stringify(source)}`
59
- );
60
- }
61
- return matcherResult;
62
- }
63
- return matchEntry(source, parent, root, matcherResult, failureLog);
64
- }
65
- if ((0, import_base.isPlainObj)(source)) {
66
- return matchPlainObj(source, parent, root, matcher, failureLog);
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.patchAt = exports.getAt = exports.Path = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var internal_cjs_1 = require("./internal.cjs");
6
+ var Path;
7
+ (function (Path) {
8
+ /**
9
+ * Regular expression used to split a path string into tokens.
10
+ */
11
+ Path.stringSplitRegex = /\?\.|\.|\[|\]/g;
12
+ /**
13
+ * Return the given `path` string split into an array of subpaths.
14
+ * @param path - the input string path
15
+ */
16
+ function stringSplit(path) {
17
+ return path.split(Path.stringSplitRegex);
18
+ }
19
+ Path.stringSplit = stringSplit;
20
+ })(Path || (exports.Path = Path = {}));
21
+ /**
22
+ * Returns the value resulting from selecting the given `path` in the given `source` object.
23
+ * It supports optional chaining for nullable values or values that may be undefined, and also
24
+ * for accessing objects inside an array.
25
+ * There is currently no support for forcing non-null (the `!` operator).
26
+ * @typeparam T - the object type to select in
27
+ * @typeparam P - a Path in object type T
28
+ * @param source - the object to select in
29
+ * @param path - the path into the object
30
+ * @example
31
+ * ```ts
32
+ * const value = { a: { b: { c: [{ d: 5 }, { d: 6 }] } } }
33
+ * Deep.getAt(value, 'a.b');
34
+ * // => { c: 5 }
35
+ * Deep.getAt(value, 'a.b.c');
36
+ * // => [{ d: 5 }, { d: 5 }]
37
+ * Deep.getAt(value, 'a.b.c[1]');
38
+ * // => { d: 6 }
39
+ * Deep.getAt(value, 'a.b.c[1]?.d');
40
+ * // => 6
41
+ * ```
42
+ */
43
+ function getAt(source, path) {
44
+ var e_1, _a;
45
+ if (path === '') {
46
+ // empty path always directly returns source value
47
+ return source;
48
+ }
49
+ var items = Path.stringSplit(path);
50
+ // start with `source` as result value
51
+ var result = source;
52
+ try {
53
+ for (var items_1 = tslib_1.__values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
54
+ var item = items_1_1.value;
55
+ if (undefined === item || item === '' || item === '[') {
56
+ // ignore irrelevant items
57
+ continue;
58
+ }
59
+ if (undefined === result || null === result) {
60
+ // optional chaining assumed and no value available, skip rest of path and return undefined
61
+ return undefined;
62
+ }
63
+ // set current result to subpath value
64
+ result = result[item];
292
65
  }
293
- }
294
- if (!onePassed) {
295
- failureLog?.push(`in compound "single": no matches succeeded`);
296
- }
297
- return onePassed;
298
66
  }
299
- case "some": {
300
- while (++index < length) {
301
- const result = matchEntry(
302
- source,
303
- parent,
304
- root,
305
- compound[index],
306
- failureLog
307
- );
308
- if (result) {
309
- return true;
67
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
68
+ finally {
69
+ try {
70
+ if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
310
71
  }
311
- }
312
- failureLog?.push(`in compound "some": no matches succeeded`);
313
- return false;
72
+ finally { if (e_1) throw e_1.error; }
314
73
  }
315
- }
74
+ return result;
316
75
  }
317
- function matchTraversal(source, root, matchType, matcher, failureLog) {
318
- let index = -1;
319
- const length = source.length;
320
- switch (matchType) {
321
- case "someItem": {
322
- while (++index < length) {
323
- if (matchEntry(source[index], source, root, matcher, failureLog)) {
324
- return true;
76
+ exports.getAt = getAt;
77
+ /**
78
+ * Patches the value at the given path in the source to the given value.
79
+ * Because the path to update must exist in the `source` object, optional
80
+ * chaining and array indexing is not allowed.
81
+ * @param source - the object to update
82
+ * @param path - the path in the object to update
83
+ * @param patchItem - the patch for the value at the given path
84
+ * @example
85
+ * ```ts
86
+ * const value = { a: { b: { c: 5 } } };
87
+ * Deep.patchAt(value, 'a.b.c', v => v + 5);
88
+ * // => { a: { b: { c: 6 } } }
89
+ * ```
90
+ */
91
+ function patchAt(source, path, patchItem) {
92
+ if (path === '') {
93
+ return internal_cjs_1.Deep.patch(source, patchItem);
94
+ }
95
+ var items = Path.stringSplit(path);
96
+ // creates a patch object based on the current path
97
+ function createPatchPart(index, target) {
98
+ var _a;
99
+ if (index === items.length) {
100
+ // processed all items, return the input `patchItem`
101
+ return patchItem;
325
102
  }
326
- }
327
- failureLog?.push(
328
- `in array traversal "someItem": no items matched given matcher`
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;
103
+ var item = items[index];
104
+ if (undefined === item || item === '') {
105
+ // empty items can be ignored
106
+ return createPatchPart(index + 1, target);
339
107
  }
340
- }
341
- return true;
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;
108
+ if (item === '[') {
109
+ // next item is array index, set arrayMode to true
110
+ return createPatchPart(index + 1, target);
350
111
  }
351
- }
352
- return true;
353
- }
354
- case "singleItem": {
355
- let singleMatched = false;
356
- while (++index < length) {
357
- if (matchEntry(source[index], source, root, matcher, failureLog)) {
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;
112
+ // create object with subPart as property key, and the restuls of processing next parts as value
113
+ var result = (_a = {},
114
+ _a[item] = createPatchPart(index + 1, target[item]),
115
+ _a);
116
+ if (Array.isArray(target)) {
117
+ // target in source object is array/tuple, so the patch should be object
118
+ return result;
365
119
  }
366
- }
367
- if (!singleMatched) {
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;
120
+ // target in source is not an array, so it patch should be an array
121
+ return [result];
575
122
  }
576
- return [result];
577
- }
578
- return deep_exports.patch(source, createPatchPart(0, source));
123
+ return internal_cjs_1.Deep.patch(source, createPatchPart(0, source));
579
124
  }
580
- // Annotate the CommonJS export names for ESM import in node:
581
- 0 && (module.exports = {
582
- Path,
583
- getAt,
584
- patchAt
585
- });
125
+ exports.patchAt = patchAt;
126
+ //# sourceMappingURL=path.cjs.map