@rimbu/deep 0.14.5 → 1.0.0-alpha.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.
Files changed (86) hide show
  1. package/{src/deep.ts → dist/bun/deep.mts} +7 -7
  2. package/{src/index.ts → dist/bun/index.mts} +5 -5
  3. package/dist/bun/internal.mts +8 -0
  4. package/{src/match.ts → dist/bun/match.mts} +6 -6
  5. package/{src/patch.ts → dist/bun/patch.mts} +9 -4
  6. package/{src/path.ts → dist/bun/path.mts} +2 -2
  7. package/{src/selector.ts → dist/bun/selector.mts} +1 -1
  8. package/dist/cjs/deep.cjs +590 -0
  9. package/dist/cjs/index.cjs +663 -0
  10. package/dist/cjs/internal.cjs +583 -0
  11. package/dist/cjs/match.cjs +376 -0
  12. package/dist/cjs/patch.cjs +105 -0
  13. package/dist/cjs/path.cjs +585 -0
  14. package/dist/cjs/protected.cjs +18 -0
  15. package/dist/cjs/selector.cjs +581 -0
  16. package/dist/cjs/tuple.cjs +73 -0
  17. package/dist/{module/deep.js → esm/deep.mjs} +6 -6
  18. package/dist/esm/deep.mjs.map +1 -0
  19. package/dist/{module/index.js → esm/index.mjs} +5 -5
  20. package/dist/esm/index.mjs.map +1 -0
  21. package/dist/esm/internal.mjs +5 -0
  22. package/dist/esm/internal.mjs.map +1 -0
  23. package/dist/{module/match.js → esm/match.mjs} +21 -21
  24. package/dist/esm/match.mjs.map +1 -0
  25. package/dist/{module/patch.js → esm/patch.mjs} +5 -5
  26. package/dist/esm/patch.mjs.map +1 -0
  27. package/dist/{module/path.js → esm/path.mjs} +2 -2
  28. package/dist/esm/path.mjs.map +1 -0
  29. package/dist/esm/protected.mjs +2 -0
  30. package/dist/esm/protected.mjs.map +1 -0
  31. package/dist/{module/selector.js → esm/selector.mjs} +2 -2
  32. package/dist/esm/selector.mjs.map +1 -0
  33. package/dist/{module/tuple.js → esm/tuple.mjs} +1 -1
  34. package/dist/esm/tuple.mjs.map +1 -0
  35. package/dist/types/{deep.d.ts → deep.d.mts} +6 -6
  36. package/dist/types/{index.d.ts → index.d.mts} +5 -5
  37. package/dist/types/internal.d.mts +7 -0
  38. package/dist/types/{match.d.ts → match.d.mts} +3 -3
  39. package/dist/types/{patch.d.ts → patch.d.mts} +3 -3
  40. package/dist/types/{path.d.ts → path.d.mts} +2 -2
  41. package/dist/types/{selector.d.ts → selector.d.mts} +1 -1
  42. package/package.json +29 -23
  43. package/src/deep.mts +364 -0
  44. package/src/index.mts +23 -0
  45. package/src/internal.mts +8 -0
  46. package/src/match.mts +700 -0
  47. package/src/patch.mts +262 -0
  48. package/src/path.mts +430 -0
  49. package/src/protected.mts +30 -0
  50. package/src/selector.mts +90 -0
  51. package/src/tuple.mts +197 -0
  52. package/dist/main/deep.js +0 -211
  53. package/dist/main/deep.js.map +0 -1
  54. package/dist/main/index.js +0 -19
  55. package/dist/main/index.js.map +0 -1
  56. package/dist/main/internal.js +0 -9
  57. package/dist/main/internal.js.map +0 -1
  58. package/dist/main/match.js +0 -284
  59. package/dist/main/match.js.map +0 -1
  60. package/dist/main/patch.js +0 -133
  61. package/dist/main/patch.js.map +0 -1
  62. package/dist/main/path.js +0 -126
  63. package/dist/main/path.js.map +0 -1
  64. package/dist/main/protected.js +0 -3
  65. package/dist/main/protected.js.map +0 -1
  66. package/dist/main/selector.js +0 -40
  67. package/dist/main/selector.js.map +0 -1
  68. package/dist/main/tuple.js +0 -164
  69. package/dist/main/tuple.js.map +0 -1
  70. package/dist/module/deep.js.map +0 -1
  71. package/dist/module/index.js.map +0 -1
  72. package/dist/module/internal.js +0 -4
  73. package/dist/module/internal.js.map +0 -1
  74. package/dist/module/match.js.map +0 -1
  75. package/dist/module/patch.js.map +0 -1
  76. package/dist/module/path.js.map +0 -1
  77. package/dist/module/protected.js +0 -2
  78. package/dist/module/protected.js.map +0 -1
  79. package/dist/module/selector.js.map +0 -1
  80. package/dist/module/tuple.js.map +0 -1
  81. package/dist/types/internal.d.ts +0 -7
  82. package/src/internal.ts +0 -8
  83. /package/{src/protected.ts → dist/bun/protected.mts} +0 -0
  84. /package/{src/tuple.ts → dist/bun/tuple.mts} +0 -0
  85. /package/dist/types/{protected.d.ts → protected.d.mts} +0 -0
  86. /package/dist/types/{tuple.d.ts → tuple.d.mts} +0 -0
@@ -0,0 +1,585 @@
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: () => Path2,
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;
292
+ }
293
+ }
294
+ if (!onePassed) {
295
+ failureLog?.push(`in compound "single": no matches succeeded`);
296
+ }
297
+ return onePassed;
298
+ }
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;
310
+ }
311
+ }
312
+ failureLog?.push(`in compound "some": no matches succeeded`);
313
+ return false;
314
+ }
315
+ }
316
+ }
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;
325
+ }
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;
339
+ }
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;
350
+ }
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;
365
+ }
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 Path2;
530
+ ((Path3) => {
531
+ Path3.stringSplitRegex = /\?\.|\.|\[|\]/g;
532
+ function stringSplit(path) {
533
+ return path.split(Path3.stringSplitRegex);
534
+ }
535
+ Path3.stringSplit = stringSplit;
536
+ })(Path2 || (Path2 = {}));
537
+ function getAt(source, path) {
538
+ if (path === "") {
539
+ return source;
540
+ }
541
+ const items = Path2.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 = Path2.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;
575
+ }
576
+ return [result];
577
+ }
578
+ return deep_exports.patch(source, createPatchPart(0, source));
579
+ }
580
+ // Annotate the CommonJS export names for ESM import in node:
581
+ 0 && (module.exports = {
582
+ Path,
583
+ getAt,
584
+ patchAt
585
+ });
@@ -0,0 +1,18 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/protected.mts
17
+ var protected_exports = {};
18
+ module.exports = __toCommonJS(protected_exports);