@rimbu/deep 2.0.4 → 2.0.6

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.
@@ -1,702 +0,0 @@
1
- import {
2
- type IsAnyFunc,
3
- type IsArray,
4
- isPlainObj,
5
- type IsPlainObj,
6
- type NotIterable,
7
- } from '@rimbu/base';
8
- import type { Protected } from './internal.mts';
9
- import type { Tuple } from './tuple.mts';
10
-
11
- /**
12
- * The type to determine the allowed input values for the `match` function.
13
- * @typeparam T - the type of value to match
14
- * @typeparam C - utility type
15
- */
16
- export type Match<T, C extends Partial<T> = Partial<T>> = Match.Entry<
17
- T,
18
- C,
19
- T,
20
- T
21
- >;
22
-
23
- export namespace Match {
24
- /**
25
- * Determines the various allowed match types for given type `T`.
26
- * @typeparam T - the input value type
27
- * @typeparam C - utility type
28
- * @typeparam P - the parent type
29
- * @typeparam R - the root object type
30
- */
31
- export type Entry<T, C, P, R> =
32
- IsAnyFunc<T> extends true
33
- ? // function can only be directly matched
34
- T
35
- : IsPlainObj<T> extends true
36
- ? // determine allowed match values for object
37
- Match.WithResult<T, P, R, Match.Obj<T, C, P, R>>
38
- : IsArray<T> extends true
39
- ? // determine allowed match values for array or tuple
40
- | Match.Arr<T, C, P, R>
41
- | Match.Entry<T[number & keyof T], C[number & keyof C], P, R>[]
42
- | Match.Func<
43
- T,
44
- P,
45
- R,
46
- | Match.Arr<T, C, P, R>
47
- | Match.Entry<
48
- T[number & keyof T],
49
- C[number & keyof C],
50
- P,
51
- R
52
- >[]
53
- >
54
- : // only accept values with same interface
55
- Match.WithResult<T, P, R, { [K in keyof C]: C[K & keyof T] }>;
56
-
57
- /**
58
- * The type that determines allowed matchers for objects.
59
- * @typeparam T - the input value type
60
- * @typeparam C - utility type
61
- * @typeparam P - the parent type
62
- * @typeparam R - the root object type
63
- */
64
- export type Obj<T, C, P, R> =
65
- | Match.ObjProps<T, C, R>
66
- | Match.CompoundForObj<T, C, P, R>;
67
-
68
- /**
69
- * The type to determine allowed matchers for object properties.
70
- * @typeparam T - the input value type
71
- * @typeparam C - utility type
72
- * @typeparam R - the root object type
73
- */
74
- export type ObjProps<T, C, R> = {
75
- [K in keyof C]?: K extends keyof T ? Match.Entry<T[K], C[K], T, R> : never;
76
- };
77
-
78
- /**
79
- * The type that determines allowed matchers for arrays/tuples.
80
- * @typeparam T - the input value type
81
- * @typeparam C - utility type
82
- * @typeparam P - the parent type
83
- * @typeparam R - the root object type
84
- */
85
- export type Arr<T, C, P, R> =
86
- | C
87
- | Match.CompoundForArr<T, C, P, R>
88
- | Match.TraversalForArr<T, C, R>
89
- | (Match.TupIndices<T, C, R> & {
90
- [K in Match.CompoundType | Match.ArrayTraversalType]?: never;
91
- });
92
-
93
- /**
94
- * A type that either directly results in result type `S` or is a function taking the value, parent, and root values, and
95
- * returns a value of type `S`.
96
- * @typeparam T - the input value type
97
- * @typeparam P - the parent type
98
- * @typeparam R - the root object type
99
- * @typeparam S - the result type
100
- */
101
- export type WithResult<T, P, R, S> = S | Match.Func<T, P, R, S>;
102
-
103
- /**
104
- * Type used to determine the allowed function types. Always includes booleans.
105
- * @typeparam T - the input value type
106
- * @typeparam P - the parent type
107
- * @typeparam R - the root object type
108
- * @typeparam S - the allowed return value type
109
- */
110
- export type Func<T, P, R, S> = (
111
- current: Protected<T>,
112
- parent: Protected<P>,
113
- root: Protected<R>
114
- ) => boolean | S;
115
-
116
- /**
117
- * Type used to indicate an object containing matches for tuple indices.
118
- * @typeparam T - the input value type
119
- * @typeparam C - utility type
120
- * @typeparam R - the root object type
121
- */
122
- export type TupIndices<T, C, R> = {
123
- [K in Tuple.KeysOf<C>]?: Match.Entry<T[K & keyof T], C[K], T, R>;
124
- } & NotIterable;
125
-
126
- /**
127
- * Compound keys used to indicate the type of compound.
128
- */
129
- export type CompoundType = 'every' | 'some' | 'none' | 'single';
130
-
131
- /**
132
- * Keys used to indicate an array match traversal.
133
- */
134
- export type ArrayTraversalType = `${CompoundType}Item`;
135
-
136
- /**
137
- * Compound matcher for objects, represented as an array starting with a compound type keyword.
138
- * @typeparam T - the input value type
139
- * @typeparam C - utility type
140
- * @typeparam P - the parent type
141
- * @typeparam R - the root object type
142
- */
143
- export type CompoundForObj<T, C, P, R> = [
144
- Match.CompoundType,
145
- ...Match.Entry<T, C, P, R>[],
146
- ];
147
-
148
- /**
149
- * Defines an object containing exactly one `CompoundType` key, having an array of matchers.
150
- * @typeparam T - the input value type
151
- * @typeparam C - utility type
152
- * @typeparam P - the parent type
153
- * @typeparam R - the root object type
154
- */
155
- export type CompoundForArr<T, C, P, R> = {
156
- [K in Match.CompoundType]: {
157
- [K2 in Match.CompoundType]?: K2 extends K
158
- ? Match.Entry<T, C, P, R>[]
159
- : never;
160
- };
161
- }[Match.CompoundType];
162
-
163
- /**
164
- * Defines an object containing exactly one `TraversalType` key, having a matcher for the array element type.
165
- * @typeparam T - the input value type
166
- * @typeparam C - utility type
167
- * @typeparam R - the root object type
168
- */
169
- export type TraversalForArr<T, C, R> = {
170
- [K in Match.ArrayTraversalType]: {
171
- [K2 in Match.ArrayTraversalType]?: K2 extends K
172
- ? Match.Entry<T[number & keyof T], C[number & keyof C], T, R>
173
- : never;
174
- };
175
- }[Match.ArrayTraversalType];
176
-
177
- /**
178
- * Utility type for collecting match failure reasons
179
- */
180
- export type FailureLog = string[];
181
- }
182
-
183
- /**
184
- * Returns true if the given `value` object matches the given `matcher`, false otherwise.
185
- * @typeparam T - the input value type
186
- * @typeparam C - utility type
187
- * @param source - the value to match (should be a plain object)
188
- * @param matcher - a matcher object or a function taking the matcher API and returning a match object
189
- * @param failureLog - (optional) a string array that can be passed to collect reasons why the match failed
190
- * @example
191
- * ```ts
192
- * const input = { a: 1, b: { c: true, d: 'a' } }
193
- * match(input, { a: 1 }) // => true
194
- * match(input, { a: 2 }) // => false
195
- * match(input, { a: (v) => v > 10 }) // => false
196
- * match(input, { b: { c: true }}) // => true
197
- * match(input, ['every', { a: (v) => v > 0 }, { b: { c: true } }]) // => true
198
- * match(input, { b: { c: (v, parent, root) => v && parent.d.length > 0 && root.a > 0 } })
199
- * // => true
200
- * ```
201
- */
202
- export function match<T, C extends Partial<T> = Partial<T>>(
203
- source: T,
204
- matcher: Match<T, C>,
205
- failureLog?: Match.FailureLog
206
- ): boolean {
207
- return matchEntry(source, source, source, matcher as any, failureLog);
208
- }
209
-
210
- /**
211
- * Match a generic match entry against the given source.
212
- */
213
- function matchEntry<T, C, P, R>(
214
- source: T,
215
- parent: P,
216
- root: R,
217
- matcher: Match.Entry<T, C, P, R>,
218
- failureLog?: Match.FailureLog
219
- ): boolean {
220
- if (Object.is(source, matcher)) {
221
- // value and target are exactly the same, always will be true
222
- return true;
223
- }
224
-
225
- if (matcher === null || matcher === undefined) {
226
- // these matchers can only be direct matches, and previously it was determined that
227
- // they are not equal
228
- failureLog?.push(
229
- `value ${JSON.stringify(source)} did not match matcher ${matcher}`
230
- );
231
-
232
- return false;
233
- }
234
-
235
- if (typeof source === 'function') {
236
- // function source values can only be directly matched
237
- const result = Object.is(source, matcher);
238
-
239
- if (!result) {
240
- failureLog?.push(
241
- `both value and matcher are functions, but they do not have the same reference`
242
- );
243
- }
244
-
245
- return result;
246
- }
247
-
248
- if (typeof matcher === 'function') {
249
- // resolve match function first
250
- const matcherResult = matcher(source, parent, root);
251
-
252
- if (typeof matcherResult === 'boolean') {
253
- // function resulted in a direct match result
254
-
255
- if (!matcherResult) {
256
- failureLog?.push(
257
- `function matcher returned false for value ${JSON.stringify(source)}`
258
- );
259
- }
260
-
261
- return matcherResult;
262
- }
263
-
264
- // function resulted in a value that needs to be further matched
265
- return matchEntry(source, parent, root, matcherResult, failureLog);
266
- }
267
-
268
- if (isPlainObj(source)) {
269
- // source ia a plain object, can be partially matched
270
- return matchPlainObj(source, parent, root, matcher as any, failureLog);
271
- }
272
-
273
- if (Array.isArray(source)) {
274
- // source is an array
275
- return matchArr(source, parent, root, matcher as any, failureLog);
276
- }
277
-
278
- // already determined above that the source and matcher are not equal
279
-
280
- failureLog?.push(
281
- `value ${JSON.stringify(source)} does not match given matcher ${JSON.stringify(matcher)}`
282
- );
283
-
284
- return false;
285
- }
286
-
287
- /**
288
- * Match an array matcher against the given source.
289
- */
290
- function matchArr<T extends any[], C, P, R>(
291
- source: T,
292
- parent: P,
293
- root: R,
294
- matcher: Match.Arr<T, C, P, R>,
295
- failureLog?: Match.FailureLog
296
- ): boolean {
297
- if (Array.isArray(matcher)) {
298
- // directly compare array contents
299
- const length = source.length;
300
-
301
- if (length !== matcher.length) {
302
- // if lengths not equal, arrays are not equal
303
-
304
- failureLog?.push(
305
- `array lengths are not equal: value length ${source.length} !== matcher length ${matcher.length}`
306
- );
307
-
308
- return false;
309
- }
310
-
311
- // loop over arrays, matching every value
312
- let index = -1;
313
- while (++index < length) {
314
- if (
315
- !matchEntry(source[index], source, root, matcher[index], failureLog)
316
- ) {
317
- // item did not match, return false
318
-
319
- failureLog?.push(
320
- `index ${index} does not match with value ${JSON.stringify(
321
- source[index]
322
- )} and matcher ${matcher[index]}`
323
- );
324
-
325
- return false;
326
- }
327
- }
328
-
329
- // all items are equal
330
- return true;
331
- }
332
-
333
- // matcher is plain object
334
-
335
- if (typeof matcher === 'object' && null !== matcher) {
336
- if (`every` in matcher) {
337
- return matchCompound(
338
- source,
339
- parent,
340
- root,
341
- ['every', ...(matcher.every as any)],
342
- failureLog
343
- );
344
- }
345
- if (`some` in matcher) {
346
- return matchCompound(
347
- source,
348
- parent,
349
- root,
350
- ['some', ...(matcher.some as any)],
351
- failureLog
352
- );
353
- }
354
- if (`none` in matcher) {
355
- return matchCompound(
356
- source,
357
- parent,
358
- root,
359
- ['none', ...(matcher.none as any)],
360
- failureLog
361
- );
362
- }
363
- if (`single` in matcher) {
364
- return matchCompound(
365
- source,
366
- parent,
367
- root,
368
- ['single', ...(matcher.single as any)],
369
- failureLog
370
- );
371
- }
372
- if (`someItem` in matcher) {
373
- return matchTraversal(
374
- source,
375
- root,
376
- 'someItem',
377
- matcher.someItem as any,
378
- failureLog
379
- );
380
- }
381
- if (`everyItem` in matcher) {
382
- return matchTraversal(
383
- source,
384
- root,
385
- 'everyItem',
386
- matcher.everyItem as any,
387
- failureLog
388
- );
389
- }
390
- if (`noneItem` in matcher) {
391
- return matchTraversal(
392
- source,
393
- root,
394
- 'noneItem',
395
- matcher.noneItem as any,
396
- failureLog
397
- );
398
- }
399
- if (`singleItem` in matcher) {
400
- return matchTraversal(
401
- source,
402
- root,
403
- 'singleItem',
404
- matcher.singleItem as any,
405
- failureLog
406
- );
407
- }
408
- }
409
-
410
- // matcher is plain object with index keys
411
-
412
- for (const index in matcher as any) {
413
- const matcherAtIndex = (matcher as any)[index];
414
-
415
- if (!(index in source)) {
416
- // source does not have item at given index
417
-
418
- failureLog?.push(
419
- `index ${index} does not exist in source ${JSON.stringify(
420
- source
421
- )} but should match matcher ${JSON.stringify(matcherAtIndex)}`
422
- );
423
-
424
- return false;
425
- }
426
-
427
- // match the source item at the given index
428
- const result = matchEntry(
429
- (source as any)[index],
430
- source,
431
- root,
432
- matcherAtIndex,
433
- failureLog
434
- );
435
-
436
- if (!result) {
437
- // item did not match
438
-
439
- failureLog?.push(
440
- `index ${index} does not match with value ${JSON.stringify(
441
- (source as any)[index]
442
- )} and matcher ${JSON.stringify(matcherAtIndex)}`
443
- );
444
-
445
- return false;
446
- }
447
- }
448
-
449
- // all items match
450
-
451
- return true;
452
- }
453
-
454
- /**
455
- * Match an object matcher against the given source.
456
- */
457
- function matchPlainObj<T extends object, C, P, R>(
458
- source: T,
459
- parent: P,
460
- root: R,
461
- matcher: Match.Obj<T, C, P, R>,
462
- failureLog?: Match.FailureLog
463
- ): boolean {
464
- if (Array.isArray(matcher)) {
465
- // the matcher is of compound type
466
- return matchCompound(source, parent, root, matcher as any, failureLog);
467
- }
468
-
469
- // partial object props matcher
470
-
471
- for (const key in matcher) {
472
- if (!(key in source)) {
473
- // the source does not have the given key
474
-
475
- failureLog?.push(
476
- `key ${key} is specified in matcher but not present in value ${JSON.stringify(source)}`
477
- );
478
-
479
- return false;
480
- }
481
-
482
- // match the source value at the given key with the matcher at given key
483
- const result = matchEntry(
484
- (source as any)[key],
485
- source,
486
- root,
487
- matcher[key],
488
- failureLog
489
- );
490
-
491
- if (!result) {
492
- failureLog?.push(
493
- `key ${key} does not match in value ${JSON.stringify(
494
- (source as any)[key]
495
- )} with matcher ${JSON.stringify(matcher[key])}`
496
- );
497
- return false;
498
- }
499
- }
500
-
501
- // all properties match
502
-
503
- return true;
504
- }
505
-
506
- /**
507
- * Match a compound matcher against the given source.
508
- */
509
- function matchCompound<T, C, P, R>(
510
- source: T,
511
- parent: P,
512
- root: R,
513
- compound: [Match.CompoundType, ...Match.Entry<T, C, P, R>[]],
514
- failureLog?: Match.FailureLog
515
- ): boolean {
516
- // first item indicates compound match type
517
- const matchType = compound[0];
518
-
519
- const length = compound.length;
520
-
521
- // start at index 1
522
- let index = 0;
523
-
524
- type Entry = Match.Entry<T, C, P, R>;
525
-
526
- switch (matchType) {
527
- case 'every': {
528
- while (++index < length) {
529
- // if any item does not match, return false
530
- const result = matchEntry(
531
- source,
532
- parent,
533
- root,
534
- compound[index] as Entry,
535
- failureLog
536
- );
537
-
538
- if (!result) {
539
- failureLog?.push(
540
- `in compound "every": match at index ${index} failed`
541
- );
542
-
543
- return false;
544
- }
545
- }
546
-
547
- return true;
548
- }
549
- case 'none': {
550
- // if any item matches, return false
551
- while (++index < length) {
552
- const result = matchEntry(
553
- source,
554
- parent,
555
- root,
556
- compound[index] as Entry,
557
- failureLog
558
- );
559
-
560
- if (result) {
561
- failureLog?.push(
562
- `in compound "none": match at index ${index} succeeded`
563
- );
564
-
565
- return false;
566
- }
567
- }
568
-
569
- return true;
570
- }
571
- case 'single': {
572
- // if not exactly one item matches, return false
573
- let onePassed = false;
574
-
575
- while (++index < length) {
576
- const result = matchEntry(
577
- source,
578
- parent,
579
- root,
580
- compound[index] as Entry,
581
- failureLog
582
- );
583
-
584
- if (result) {
585
- if (onePassed) {
586
- failureLog?.push(
587
- `in compound "single": multiple matches succeeded`
588
- );
589
-
590
- return false;
591
- }
592
-
593
- onePassed = true;
594
- }
595
- }
596
-
597
- if (!onePassed) {
598
- failureLog?.push(`in compound "single": no matches succeeded`);
599
- }
600
-
601
- return onePassed;
602
- }
603
- case 'some': {
604
- // if any item matches, return true
605
- while (++index < length) {
606
- const result = matchEntry(
607
- source,
608
- parent,
609
- root,
610
- compound[index] as Entry,
611
- failureLog
612
- );
613
-
614
- if (result) {
615
- return true;
616
- }
617
- }
618
-
619
- failureLog?.push(`in compound "some": no matches succeeded`);
620
-
621
- return false;
622
- }
623
- }
624
- }
625
-
626
- function matchTraversal<T extends any[], C extends any[], R>(
627
- source: T,
628
- root: R,
629
- matchType: Match.ArrayTraversalType,
630
- matcher: Match.Entry<T[keyof T], C[keyof C], T, R>,
631
- failureLog?: Match.FailureLog
632
- ): boolean {
633
- let index = -1;
634
- const length = source.length;
635
-
636
- switch (matchType) {
637
- case 'someItem': {
638
- while (++index < length) {
639
- if (matchEntry(source[index], source, root, matcher, failureLog)) {
640
- return true;
641
- }
642
- }
643
-
644
- failureLog?.push(
645
- `in array traversal "someItem": no items matched given matcher`
646
- );
647
-
648
- return false;
649
- }
650
- case 'everyItem': {
651
- while (++index < length) {
652
- if (!matchEntry(source[index], source, root, matcher, failureLog)) {
653
- failureLog?.push(
654
- `in array traversal "everyItem": at least one item did not match given matcher`
655
- );
656
- return false;
657
- }
658
- }
659
-
660
- return true;
661
- }
662
- case 'noneItem': {
663
- while (++index < length) {
664
- if (matchEntry(source[index], source, root, matcher, failureLog)) {
665
- failureLog?.push(
666
- `in array traversal "noneItem": at least one item matched given matcher`
667
- );
668
- return false;
669
- }
670
- }
671
-
672
- return true;
673
- }
674
- case 'singleItem': {
675
- let singleMatched = false;
676
-
677
- while (++index < length) {
678
- if (matchEntry(source[index], source, root, matcher, failureLog)) {
679
- if (singleMatched) {
680
- failureLog?.push(
681
- `in array traversal "singleItem": more than one item matched given matcher`
682
- );
683
-
684
- return false;
685
- }
686
-
687
- singleMatched = true;
688
- }
689
- }
690
-
691
- if (!singleMatched) {
692
- failureLog?.push(
693
- `in array traversal "singleItem": no item matched given matcher`
694
- );
695
-
696
- return false;
697
- }
698
-
699
- return true;
700
- }
701
- }
702
- }