@mark1russell7/layout-contract 0.9.1 → 0.9.2

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/layout-contract",
4
- "version": "0.9.1",
4
+ "version": "0.9.2",
5
5
  "description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
6
6
  "keywords": [
7
7
  "layout",
@@ -0,0 +1,94 @@
1
+ // The manifest of a corpus, `layout-corpus/1`, and the one writer of its text (`docs/CORPUS.md`).
2
+ // The bench of each engine and the joint bench write the same form: one line for each field, and one line for each item of a list.
3
+ import type { DeclaredClaim } from "./corpus.ts";
4
+
5
+ /** The schema of a corpus manifest. */
6
+ export const CORPUS_MANIFEST_SCHEMA = "layout-corpus/1";
7
+
8
+ /** The held-out split of a portfolio report: a seed, or `all` when each input is held out. */
9
+ export type HeldOut = number | "all";
10
+
11
+ /** The claims of a corpus that each bench reads. `declared` is the count of the inputs of each declared property. */
12
+ export interface ManifestClaims {
13
+ readonly declared?: DeclaredClaim;
14
+ }
15
+
16
+ /**
17
+ * A corpus manifest: the name, the specs of the inputs and, as an option, the held-out split, the claims and the inputs.
18
+ * `Spec`, `Claims` and `Input` are the types of the bench that writes the manifest.
19
+ */
20
+ export interface CorpusManifest<Spec = unknown, Claims extends object = ManifestClaims, Input = unknown> {
21
+ readonly schema: typeof CORPUS_MANIFEST_SCHEMA;
22
+ readonly corpus: string;
23
+ readonly heldOut?: HeldOut;
24
+ readonly claims?: Claims;
25
+ readonly specs: readonly Spec[];
26
+ readonly inputs?: readonly Input[];
27
+ }
28
+
29
+ /**
30
+ * The form of a JSON value on one line.
31
+ * With `spaced`, a space comes after each colon and each comma. With `compact`, the text is the text of `JSON.stringify`.
32
+ */
33
+ export type JsonStyle = "spaced" | "compact";
34
+
35
+ /** This function gives a JSON value on one line, in the style `style`. The keys of an object keep their order. */
36
+ export function lineJson(value: unknown, style: JsonStyle = "spaced"): string {
37
+ if (style === "compact") return JSON.stringify(value);
38
+ if (Array.isArray(value)) return `[${value.map((v) => lineJson(v)).join(", ")}]`;
39
+ if (value !== null && typeof value === "object") return `{${Object.entries(value).map(([k, v]) => `${JSON.stringify(k)}: ${lineJson(v)}`).join(", ")}}`;
40
+ return JSON.stringify(value);
41
+ }
42
+
43
+ /** One field of a manifest text. */
44
+ export interface ManifestField {
45
+ readonly key: string;
46
+ /** The value. With `rows`, it is an array, and each item gets its own line. */
47
+ readonly value: unknown;
48
+ readonly rows?: boolean;
49
+ /** The style of the value or of each item. The default is the style of the options. */
50
+ readonly style?: JsonStyle;
51
+ }
52
+
53
+ /** The options of `manifestText`. */
54
+ export interface ManifestTextOptions {
55
+ /** The style of each field with no style of its own. The default is `spaced`. */
56
+ readonly style?: JsonStyle;
57
+ /** The text of a list with no item: `[]` (the default), or `[` and `]` with an empty line between them. */
58
+ readonly emptyRows?: "brackets" | "line";
59
+ }
60
+
61
+ /** This function gives the text of a manifest: `{`, one line for each field in the order of `fields`, `}` and a newline. */
62
+ export function manifestText(fields: readonly ManifestField[], options: ManifestTextOptions = {}): string {
63
+ const keys = new Set<string>();
64
+ const lines = fields.map((field, i) => {
65
+ if (keys.has(field.key)) throw new RangeError(`manifestText: the key ${field.key} occurs two times.`);
66
+ keys.add(field.key);
67
+ const style = field.style ?? options.style ?? "spaced";
68
+ const end = i === fields.length - 1 ? "" : ",";
69
+ const head = ` ${JSON.stringify(field.key)}: `;
70
+ if (field.rows !== true) return `${head}${lineJson(field.value, style)}${end}`;
71
+ if (!Array.isArray(field.value)) throw new RangeError(`manifestText: the field ${field.key} has rows, so its value is an array.`);
72
+ if (field.value.length === 0 && options.emptyRows !== "line") return `${head}[]${end}`;
73
+ return `${head}[\n${field.value.map((item) => ` ${lineJson(item, style)}`).join(",\n")}\n ]${end}`;
74
+ });
75
+ return `{\n${lines.join("\n")}\n}\n`;
76
+ }
77
+
78
+ /**
79
+ * This function gives the text of a corpus manifest.
80
+ * The fields come in the order `schema`, `corpus`, `heldOut`, `claims`, `specs` and `inputs`. A field with no value is not in the text.
81
+ * `specs` and `inputs` have one line for each item.
82
+ */
83
+ export function corpusManifestText<Spec, Claims extends object, Input>(manifest: CorpusManifest<Spec, Claims, Input>, options: ManifestTextOptions = {}): string {
84
+ if (manifest.schema !== CORPUS_MANIFEST_SCHEMA) throw new RangeError(`corpusManifestText: the schema is ${String(manifest.schema)}, not ${CORPUS_MANIFEST_SCHEMA}.`);
85
+ const fields: ManifestField[] = [
86
+ { key: "schema", value: manifest.schema },
87
+ { key: "corpus", value: manifest.corpus },
88
+ ];
89
+ if (manifest.heldOut !== undefined) fields.push({ key: "heldOut", value: manifest.heldOut });
90
+ if (manifest.claims !== undefined) fields.push({ key: "claims", value: manifest.claims });
91
+ fields.push({ key: "specs", value: manifest.specs, rows: true });
92
+ if (manifest.inputs !== undefined) fields.push({ key: "inputs", value: manifest.inputs, rows: true });
93
+ return manifestText(fields, options);
94
+ }
package/src/index.ts CHANGED
@@ -15,6 +15,7 @@ export * from "./edges.ts";
15
15
  export * from "./hash.ts";
16
16
  export * from "./portfolio.ts";
17
17
  export * from "./corpus.ts";
18
+ export * from "./corpus-manifest.ts";
18
19
  export * from "./strategy-space.ts";
19
20
  export * from "./strategy-catalog.ts";
20
21
  export * from "./verdict.ts";