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