@czap/quantizer 0.1.0

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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/dist/animated-quantizer.d.ts +108 -0
  4. package/dist/animated-quantizer.d.ts.map +1 -0
  5. package/dist/animated-quantizer.js +196 -0
  6. package/dist/animated-quantizer.js.map +1 -0
  7. package/dist/evaluate.d.ts +79 -0
  8. package/dist/evaluate.d.ts.map +1 -0
  9. package/dist/evaluate.js +128 -0
  10. package/dist/evaluate.js.map +1 -0
  11. package/dist/index.d.ts +18 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +16 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/memo-cache.d.ts +35 -0
  16. package/dist/memo-cache.d.ts.map +1 -0
  17. package/dist/memo-cache.js +39 -0
  18. package/dist/memo-cache.js.map +1 -0
  19. package/dist/quantizer.d.ts +223 -0
  20. package/dist/quantizer.d.ts.map +1 -0
  21. package/dist/quantizer.js +260 -0
  22. package/dist/quantizer.js.map +1 -0
  23. package/dist/schemas.d.ts +44 -0
  24. package/dist/schemas.d.ts.map +1 -0
  25. package/dist/schemas.js +46 -0
  26. package/dist/schemas.js.map +1 -0
  27. package/dist/testing.d.ts +15 -0
  28. package/dist/testing.d.ts.map +1 -0
  29. package/dist/testing.js +15 -0
  30. package/dist/testing.js.map +1 -0
  31. package/dist/transition.d.ts +67 -0
  32. package/dist/transition.d.ts.map +1 -0
  33. package/dist/transition.js +49 -0
  34. package/dist/transition.js.map +1 -0
  35. package/package.json +58 -0
  36. package/src/animated-quantizer.ts +272 -0
  37. package/src/evaluate.ts +160 -0
  38. package/src/index.ts +26 -0
  39. package/src/memo-cache.ts +58 -0
  40. package/src/quantizer.ts +503 -0
  41. package/src/schemas.ts +50 -0
  42. package/src/testing.ts +15 -0
  43. package/src/transition.ts +97 -0
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Transition configuration for state crossings.
3
+ * Maps `from->to` state pairs to duration/easing/delay configs.
4
+ *
5
+ * @module
6
+ */
7
+
8
+ import type { Boundary, StateUnion, Quantizer, Easing, Millis } from '@czap/core';
9
+ import { Millis as mkMillis } from '@czap/core';
10
+
11
+ /**
12
+ * Per-transition animation parameters.
13
+ *
14
+ * Used by {@link AnimatedQuantizer} to drive interpolation between two
15
+ * state output records. `duration` of `0` produces an instantaneous snap.
16
+ */
17
+ export interface TransitionConfig {
18
+ /** Animation duration in milliseconds (branded via {@link Millis}). */
19
+ readonly duration: Millis;
20
+ /** Easing function applied to progress; defaults to linear. */
21
+ readonly easing?: Easing.Fn;
22
+ /** Delay before the animation begins, in milliseconds. */
23
+ readonly delay?: Millis;
24
+ }
25
+
26
+ /**
27
+ * State-transition map keyed by `"from->to"` literal or `"*"` wildcard.
28
+ *
29
+ * Lookup resolves exact keys first, then the wildcard, then falls back to
30
+ * an instantaneous transition (duration: 0).
31
+ */
32
+ export interface TransitionMap<_S extends string = string> {
33
+ /** Wildcard fallback applied when no exact `from->to` key matches. */
34
+ readonly '*'?: TransitionConfig;
35
+ /** Exact `"from->to"` transition key. */
36
+ readonly [key: `${string}->${string}`]: TransitionConfig;
37
+ }
38
+
39
+ /**
40
+ * Resolver that maps a boundary crossing to its {@link TransitionConfig}.
41
+ *
42
+ * Produced by {@link Transition.for}; consumed by {@link AnimatedQuantizer}
43
+ * during animation loop setup.
44
+ */
45
+ export interface Transition<B extends Boundary.Shape> {
46
+ /** The raw transition map used to create this resolver. */
47
+ readonly config: TransitionMap<StateUnion<B> & string>;
48
+ /** Resolve the transition config for a specific `from -> to` state pair. */
49
+ getTransition(from: StateUnion<B>, to: StateUnion<B>): TransitionConfig;
50
+ }
51
+
52
+ const DEFAULT_TRANSITION: TransitionConfig = {
53
+ duration: mkMillis(0),
54
+ };
55
+
56
+ /**
57
+ * Build a Transition resolver for a given quantizer and transition map.
58
+ *
59
+ * Resolution order:
60
+ * 1. Exact match: `"stateA->stateB"`
61
+ * 2. Wildcard: `"*"`
62
+ * 3. Fallback: instant transition (duration: 0)
63
+ */
64
+ function createTransition<B extends Boundary.Shape>(
65
+ _quantizer: Quantizer<B>,
66
+ transitionConfig: TransitionMap<StateUnion<B> & string>,
67
+ ): Transition<B> {
68
+ return {
69
+ config: transitionConfig,
70
+ getTransition(from: StateUnion<B>, to: StateUnion<B>): TransitionConfig {
71
+ // Exact match first. The key is typed as the template-literal pattern
72
+ // declared on TransitionMap, so we can index directly.
73
+ const exactKey = `${from as string}->${to as string}` as const;
74
+ const exact = transitionConfig[exactKey];
75
+ if (exact !== undefined) return exact;
76
+
77
+ // Wildcard fallback
78
+ const wildcard = transitionConfig['*'];
79
+ if (wildcard !== undefined) return wildcard;
80
+
81
+ // No transition configured -- instant
82
+ return DEFAULT_TRANSITION;
83
+ },
84
+ };
85
+ }
86
+
87
+ /**
88
+ * Transition resolver namespace.
89
+ *
90
+ * `Transition.for(quantizer, map)` produces a {@link Transition} that looks
91
+ * up animation parameters by `from->to` state pairs. Consumed by
92
+ * {@link AnimatedQuantizer} for interpolation setup.
93
+ */
94
+ export const Transition = {
95
+ /** Build a {@link Transition} resolver for the given quantizer and transition map. */
96
+ for: createTransition,
97
+ } as const;