@mark1russell7/guidebook-plans 0.1.3 → 0.1.5

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/src/steps.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  import { checkUnits, resultOf } from "@mark1russell7/guidebook";
4
4
  import type { CitedUnit, Finding, GateResult, Source, SourceId, UseContext } from "@mark1russell7/guidebook";
5
5
  import type { Phase } from "./ladder.ts";
6
+ import type { Society } from "./master.ts";
6
7
 
7
8
  /**
8
9
  * One action of a plan. A step belongs to a key and to a phase.
@@ -26,8 +27,15 @@ export interface Step<P extends string = Phase> {
26
27
  readonly uses?: readonly string[];
27
28
  /** The order of the step in its phase, from 1. */
28
29
  readonly order: number;
29
- /** The sources of the step. Each step has one or more. */
30
+ /** The sources of the step. A step with no `reasoning` has one or more. */
30
31
  readonly sources: readonly SourceId[];
32
+ /**
33
+ * The reasoning of a strategy step from first principles, for a step that no source tells.
34
+ * A step with a number in its text (a dose, a time or a safety limit) still cites a source.
35
+ */
36
+ readonly reasoning?: string;
37
+ /** The states of society that the step is for. No `society` means each state. */
38
+ readonly society?: readonly Society[];
31
39
  }
32
40
 
33
41
  /** A marked gap: a key and a phase that have no step on purpose, with the reason. */
@@ -48,16 +56,24 @@ export interface Cell<P extends string = Phase> {
48
56
 
49
57
  const byOrder = <P extends string>(a: Step<P>, b: Step<P>): number => a.order - b.order || a.id.localeCompare(b.id);
50
58
 
59
+ /** The options of the steps query. */
60
+ export interface StepsForOptions {
61
+ /** Only the steps for this state of society. A step with no `society` is for each state. */
62
+ readonly society?: Society;
63
+ }
64
+
51
65
  /**
52
66
  * This function gives the steps whose `on` is in `on` and whose phase is in `phases`.
53
67
  * The sequence is by `on`, then by `phases`, then by `order`. A step that matches two entries of `on` comes one time.
54
68
  */
55
- export function stepsFor<P extends string>(library: readonly Step<P>[], on: readonly string[], phases: readonly P[]): readonly Step<P>[] {
69
+ export function stepsFor<P extends string>(library: readonly Step<P>[], on: readonly string[], phases: readonly P[], options: StepsForOptions = {}): readonly Step<P>[] {
70
+ const { society } = options;
71
+ const fits = (step: Step<P>): boolean => society === undefined || step.society === undefined || step.society.includes(society);
56
72
  const out: Step<P>[] = [];
57
73
  const seen = new Set<string>();
58
74
  for (const key of on) {
59
75
  for (const phase of phases) {
60
- const hits = library.filter((step) => step.on === key && step.phase === phase && !seen.has(step.id)).sort(byOrder);
76
+ const hits = library.filter((step) => step.on === key && step.phase === phase && !seen.has(step.id) && fits(step)).sort(byOrder);
61
77
  for (const step of hits) {
62
78
  seen.add(step.id);
63
79
  out.push(step);
@@ -88,6 +104,8 @@ export const STEP_FINDING_CODES = [
88
104
  "duplicate-id",
89
105
  "empty-text",
90
106
  "no-source",
107
+ "number-without-source",
108
+ "empty-reasoning",
91
109
  "missing-source",
92
110
  "use-refused",
93
111
  "unknown-phase",
@@ -137,7 +155,11 @@ export function checkSteps<P extends string>(steps: readonly Step<P>[], sources:
137
155
  if (ids.has(step.id)) error("duplicate-id", "step", step.id, `Two steps have the id "${step.id}".`);
138
156
  ids.add(step.id);
139
157
  if (step.do.trim() === "") error("empty-text", "step", step.id, `The step "${step.id}" has no text.`);
140
- if (step.sources.length === 0) error("no-source", "step", step.id, `The step "${step.id}" cites no source.`);
158
+ if (step.reasoning !== undefined && step.reasoning.trim() === "") error("empty-reasoning", "step", step.id, `The step "${step.id}" has an empty reasoning.`);
159
+ if (step.sources.length === 0) {
160
+ if (step.reasoning === undefined) error("no-source", "step", step.id, `The step "${step.id}" cites no source and gives no reasoning.`);
161
+ else if (/[0-9]/.test(step.do)) error("number-without-source", "step", step.id, `The step "${step.id}" has a number, so it needs a source.`);
162
+ }
141
163
  if (!phases.has(step.phase)) error("unknown-phase", "step", step.id, `The step "${step.id}" has the unknown phase "${step.phase}".`);
142
164
  if (!Number.isInteger(step.order) || step.order < 1) error("bad-order", "step", step.id, `The step "${step.id}" has the order ${String(step.order)}. It needs an integer from 1.`);
143
165
  const cell = `${step.on}\u0000${step.phase}\u0000${String(step.order)}`;