@mark1russell7/highways 0.1.5 → 0.2.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.
@@ -3,6 +3,7 @@ import type { CurveSpace } from "../curves/space.ts";
3
3
  import type { Point } from "../geom.ts";
4
4
  import type { Graph, Result, Router } from "../model.ts";
5
5
  import type { Boxed, Gap, RoomLink } from "../room/index.ts";
6
+ import type { Section } from "../sections.ts";
6
7
 
7
8
  /** The input of a curve strategy: one polyline and the context of its route. */
8
9
  export interface CurveInput {
@@ -25,11 +26,13 @@ export interface RoomInput {
25
26
  /**
26
27
  * The steps that have strategies, with the fixed input and output type of each step.
27
28
  *
29
+ * - `group`: a graph in, the tree of sections out. The router `sections` makes its hubs and its zoom levels from this tree.
28
30
  * - `router`: a graph in, the routes (and the `levels`, `bundles` and `runs`) out.
29
31
  * - `curve`: a polyline in, the segments of a curve out.
30
32
  * - `room`: the boxes and links of a board in, the gaps of the board with their need out.
31
33
  */
32
34
  export interface StepTypes {
35
+ readonly group: { readonly input: Graph; readonly output: Section };
33
36
  readonly router: { readonly input: Graph; readonly output: Result };
34
37
  readonly curve: { readonly input: CurveInput; readonly output: CurveSegment[] };
35
38
  readonly room: { readonly input: RoomInput; readonly output: Gap[] };
@@ -45,7 +48,7 @@ export type StepInput<S extends Step> = StepTypes[S]["input"];
45
48
  export type StepOutput<S extends Step> = StepTypes[S]["output"];
46
49
 
47
50
  /** The steps that have strategies, in the order of the pipeline. */
48
- export const STEPS: readonly Step[] = ["router", "curve", "room"];
51
+ export const STEPS: readonly Step[] = ["group", "router", "curve", "room"];
49
52
 
50
53
  /**
51
54
  * A strategy: one way to do one step, with a name and a description.
@@ -105,29 +108,30 @@ export function defineStrategy<S extends Step, O extends object>(definition: Str
105
108
  /**
106
109
  * A registry: the strategies of each step, in order. A registry is data. No import changes it.
107
110
  * A consumer makes its own registry with `createRegistry`, and adds a strategy with `register`.
111
+ * The list `group` is optional in the type, so a registry from version 0.1.5 stays a registry. `createRegistry` always gives it.
108
112
  */
109
- export type Registry = { readonly [S in Step]: readonly Strategy<S>[] };
113
+ export type Registry = { readonly [S in Exclude<Step, "group">]: readonly Strategy<S>[] } & { readonly group?: readonly Strategy<"group">[] };
110
114
 
111
115
  /** This function makes a registry from a list of strategies. It keeps their order. Two strategies with one step and one name are an error. */
112
116
  export function createRegistry(strategies: readonly AnyStrategy[]): Registry {
113
- const out: { [S in Step]: Strategy<S>[] } = { router: [], curve: [], room: [] };
117
+ const out: { [S in Step]: Strategy<S>[] } = { group: [], router: [], curve: [], room: [] };
114
118
  for (const s of strategies) {
115
119
  const list = out[s.step] as AnyStrategy[] | undefined;
116
120
  if (!list) throw new Error(`There is no step "${String(s.step)}". The steps are ${STEPS.join(", ")}.`);
117
121
  if (list.some((t) => t.name === s.name)) throw new Error(`The step "${s.step}" has two strategies with the name "${s.name}".`);
118
122
  list.push(s);
119
123
  }
120
- return Object.freeze({ router: Object.freeze(out.router), curve: Object.freeze(out.curve), room: Object.freeze(out.room) });
124
+ return Object.freeze({ group: Object.freeze(out.group), router: Object.freeze(out.router), curve: Object.freeze(out.curve), room: Object.freeze(out.room) });
121
125
  }
122
126
 
123
127
  /** This function gives a new registry with the strategies of `registry` and then `strategies`. It does not change `registry`. */
124
128
  export function register(registry: Registry, ...strategies: readonly AnyStrategy[]): Registry {
125
- return createRegistry([...registry.router, ...registry.curve, ...registry.room, ...strategies]);
129
+ return createRegistry([...(registry.group ?? []), ...registry.router, ...registry.curve, ...registry.room, ...strategies]);
126
130
  }
127
131
 
128
132
  /** This function gives the strategies of one step of a registry, in order. */
129
133
  export function listStrategies<S extends Step>(registry: Registry, step: S): readonly Strategy<S>[] {
130
- return registry[step];
134
+ return (registry[step] ?? []) as readonly Strategy<S>[];
131
135
  }
132
136
 
133
137
  /** This function gives the strategy of one step with a name, or `undefined` when the registry has no such strategy. */