@doeixd/machine 0.0.19 → 0.0.20

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/README.md CHANGED
@@ -658,6 +658,77 @@ const message = matchMachine(machine, "status", {
658
658
 
659
659
  TypeScript enforces exhaustive checking - you must handle all cases!
660
660
 
661
+ #### `createMatcher(...cases)`
662
+
663
+ The `createMatcher` utility provides a more advanced, reusable way to define pattern matching logic for your state machines. It creates a single object that provides type guards, exhaustive pattern matching, and simple state checking all in one.
664
+
665
+ ```typescript
666
+ import { createMatcher, classCase, discriminantCase, forContext } from "@doeixd/machine";
667
+
668
+ // 1. Define the matcher
669
+ const match = createMatcher(
670
+ classCase('idle', IdleMachine),
671
+ classCase('loading', LoadingMachine),
672
+ classCase('success', SuccessMachine)
673
+ );
674
+
675
+ // 2. Use it for Type Guards
676
+ if (match.is.loading(machine)) {
677
+ // machine is narrowed to LoadingMachine
678
+ console.log(machine.context.url);
679
+ }
680
+
681
+ // 3. Use it for Exhaustive Pattern Matching
682
+ const result = match.when(machine).is(
683
+ match.case.idle(() => "Ready"),
684
+ match.case.loading(() => "Loading..."),
685
+ match.case.success((m) => `Data: ${m.context.data}`),
686
+ match.exhaustive
687
+ );
688
+
689
+ // 4. Use it for Simple Matching
690
+ const state = match(machine); // 'idle' | 'loading' | 'success' | null
691
+ ```
692
+
693
+ **Advanced Context Matching:**
694
+
695
+ For discriminated unions in context, use the `forContext` helper for maximum type safety:
696
+
697
+ ```typescript
698
+ type FetchContext =
699
+ | { status: 'idle' }
700
+ | { status: 'success'; data: string };
701
+
702
+ const builder = forContext<FetchContext>();
703
+
704
+ const match = createMatcher(
705
+ builder.case('idle', 'status', 'idle'),
706
+ builder.case('success', 'status', 'success')
707
+ );
708
+
709
+ if (match.is.success(machine)) {
710
+ // machine.context is narrowed to { status: 'success'; data: string }
711
+ console.log(machine.context.data);
712
+ }
713
+ ```
714
+
715
+ **Explicit vs. Inferred Return Types:**
716
+
717
+ ```typescript
718
+ // Implicit: helper infers return type union and enforces exhaustiveness
719
+ const res1 = match.when(machine).is(
720
+ match.case.idle(() => 1),
721
+ match.case.success(() => 2),
722
+ match.exhaustive
723
+ ); // number - Exhaustive check ENABLED
724
+
725
+ // Explicit generic: You specify return type, exhaustiveness check is optional (but recommended)
726
+ const res2 = match.when(machine).is<string>(
727
+ match.case.idle(() => "Idle")
728
+ ); // string - Exhaustive check DISABLED (useful for partial matching)
729
+ ```
730
+
731
+
661
732
  #### `hasState<M, K, V>(machine, key, value)`
662
733
 
663
734
  Type guard for state checking with type narrowing.