@mark1russell7/guidebook 0.0.0-bootstrap.0 → 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Russell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,73 @@
1
1
  # @mark1russell7/guidebook
2
2
 
3
- This version is a placeholder. The release workflow of https://github.com/mark1russell7/guidebook publishes the real versions. Use a later version.
3
+ The core of a guidebook of cited knowledge. Each unit of text has its source, and each source has its license. The source gate examines the two.
4
+
5
+ ```sh
6
+ pnpm add @mark1russell7/guidebook
7
+ ```
8
+
9
+ The core names no topic. Each topic is an add-on package with its own records. Examples are steps of a plan and topics of a library.
10
+
11
+ ## The records
12
+
13
+ | Record | What it holds |
14
+ | --- | --- |
15
+ | `Source` | A page or a document: its `id`, `title`, `url`, `license`, the date `checked` of the last read, and the optional name `by` |
16
+ | `CitedUnit` | A unit of text: its `id`, its `use`, its `text`, the `id` of its `source`, and an optional `locator` in the source |
17
+
18
+ A unit has one of three uses:
19
+
20
+ - `quote`: the text of the source, word for word.
21
+ - `summary`: the text in the words of the guidebook.
22
+ - `fact`: a value with a citation.
23
+
24
+ ## The licenses
25
+
26
+ `LICENSE_RULES` gives the rule of each license. `permits(license, use, context)` tells if a license permits a use. The use `bundle` is an offline copy of the full source.
27
+
28
+ | License | `quote` | `bundle` | `summary` and `fact` | Credit line | Layer of a quote |
29
+ | --- | --- | --- | --- | --- | --- |
30
+ | `us-public-domain`, `public-domain`, `cc0` | yes | yes | yes | no | `main` |
31
+ | `cc-by-4.0` | yes | yes | yes | yes | `main` |
32
+ | `cc-by-sa-4.0`, `cc-by-sa-3.0` | yes | yes | yes | yes | `share-alike` |
33
+ | `cc-by-nc-sa-3.0` | no | only when not commercial | yes | yes | `share-alike` |
34
+ | `all-rights-reserved` | no | no | yes | no | `main` |
35
+ | `invented` | only in a fixture | only in a fixture | only in a fixture | no | `main` |
36
+
37
+ A US federal work is in the public domain under 17 USC 105. A summary and a fact are the words of the guidebook, so each real license permits them.
38
+
39
+ ## The source gate
40
+
41
+ ```ts
42
+ import { assertGate, creditOf, gate } from "@mark1russell7/guidebook";
43
+
44
+ const sources = [{ id: "water", title: "Water", url: "https://www.ready.gov/water", license: "us-public-domain", checked: "2026-11-21" }] as const;
45
+ const units = [{ id: "water-gallon", use: "fact", text: "Store 1 gallon of water for each person for each day.", source: "water" }] as const;
46
+
47
+ const result = gate(units, sources, { at: "2026-11-21" });
48
+ result.ok; // true
49
+ assertGate(units, sources); // throws an error that names each finding of the level "error"
50
+ creditOf(sources[0]); // "Water. https://www.ready.gov/water. US public domain (17 USC 105) (...). Checked 2026-11-21."
51
+ ```
52
+
53
+ The gate gives each finding as data, with a level, a code, the record and a message:
54
+
55
+ | Code | Level | When |
56
+ | --- | --- | --- |
57
+ | `duplicate-id` | `error` | Two sources or two units have the same `id`. |
58
+ | `empty-title`, `empty-text` | `error` | A source has no title, or a unit has no text. |
59
+ | `bad-url` | `error` | The address of a source does not start with `https:` or `http:`. |
60
+ | `bad-date` | `error` | The check date is not a real date `YYYY-MM-DD`. |
61
+ | `unknown-license` | `error` | The license is not in `LICENSES`. |
62
+ | `checked-in-future` | `error` | The check date is after the date `at` of the gate. |
63
+ | `stale` | `warning` | The check is older than `maxAgeDays` (365 by default) before `at`. |
64
+ | `missing-source` | `error` | A unit cites a source that is not in the list. |
65
+ | `use-refused` | `error` | The license of the source refuses the use of the unit. |
66
+
67
+ `layerOf(unit, source)` gives the layer of a printed unit. A share-alike quote prints in its own layer, so the rest of the guidebook keeps its own license.
68
+
69
+ ## Limits
70
+
71
+ - The gate does not compare a quote with the text of its source. A job that reads the source does that, and then sets `checked`.
72
+ - The table holds the licenses of the first sources. A new license goes into `LICENSES` and `LICENSE_RULES` in a pull request.
73
+ - The rules are a reading of the license texts. They are not legal advice.
@@ -0,0 +1,141 @@
1
+ /** The licenses that the gate knows. The license `invented` is for test fixtures only. */
2
+ export declare const LICENSES: readonly ["us-public-domain", "public-domain", "cc0", "cc-by-4.0", "cc-by-sa-4.0", "cc-by-sa-3.0", "cc-by-nc-sa-3.0", "all-rights-reserved", "invented"];
3
+ /** A license of a source. The source gate refuses a use that the license does not permit. */
4
+ type License = (typeof LICENSES)[number];
5
+ /** The ways to use a source. A unit of text has one use, and an offline copy of the full source is a bundle. */
6
+ export declare const USES: readonly ["quote", "summary", "fact", "bundle"];
7
+ /**
8
+ * A use of a source.
9
+ * `quote` is the text word for word. `summary` is the text in the words of the guidebook. `fact` is a value with a citation.
10
+ * `bundle` is an offline copy of the full source.
11
+ */
12
+ type Use = (typeof USES)[number];
13
+ /** The layer of a printed quote. A share-alike quote prints in its own layer, with its credit and its license. */
14
+ type Layer = "main" | "share-alike";
15
+ /** The rule of one license. */
16
+ interface LicenseRule {
17
+ /** The name of the license that a credit line prints. */
18
+ readonly title: string;
19
+ /** The address of the text of the license, or an empty string when the license has no text. */
20
+ readonly url: string;
21
+ /** This field is true when a guidebook can print the text of the source word for word. */
22
+ readonly quote: boolean;
23
+ /** This field is true when a guidebook can keep an offline copy of the full source. */
24
+ readonly bundle: boolean;
25
+ /** This field is true when the license permits only a use that is not commercial. */
26
+ readonly nonCommercial: boolean;
27
+ /** The layer of a quote. A share-alike license needs its own layer, so the rest of the guidebook keeps its own license. */
28
+ readonly layer: Layer;
29
+ /** This field is true when a quote or a bundle must print the credit line of the source. */
30
+ readonly credit: boolean;
31
+ /** This field is false for the license `invented`. A real guidebook cites only real sources. */
32
+ readonly real: boolean;
33
+ }
34
+ /**
35
+ * The rule of each license.
36
+ *
37
+ * - A US federal work (17 USC 105), a work in the public domain and a CC0 work permit each use.
38
+ * - A CC BY work permits each use with a credit line. A CC BY-SA quote also needs its own layer.
39
+ * - A CC BY-NC-SA work permits a bundle only while the guidebook is not commercial. The gate refuses a quote of it.
40
+ * - A work with all rights reserved permits only a summary and a fact, with a citation.
41
+ */
42
+ export declare const LICENSE_RULES: Readonly<Record<License, LicenseRule>>;
43
+ /** The context of a use. A guidebook that is sold, or that shows paid advertisements, is commercial. */
44
+ interface UseContext {
45
+ /** This field is true when the guidebook is commercial. The default is false. */
46
+ readonly commercial?: boolean;
47
+ /** This field is true when the guidebook is a test fixture, so the license `invented` passes. The default is false. */
48
+ readonly fixture?: boolean;
49
+ }
50
+ /**
51
+ * This function tells if a license permits a use.
52
+ * A summary and a fact are the words of the guidebook, so each real license permits them.
53
+ * A non-commercial license refuses each use of its text in a commercial guidebook. A summary and a fact are not its text, so they stay.
54
+ */
55
+ export declare function permits(license: License, use: Use, context?: UseContext): boolean;
56
+ /** A date as `YYYY-MM-DD`. */
57
+ type IsoDate = string;
58
+ /** The `id` of a source. */
59
+ type SourceId = string;
60
+ /** A page or a document, with its license and the date of the last check. The license `invented` is for test fixtures only. */
61
+ interface Source {
62
+ readonly id: SourceId;
63
+ readonly title: string;
64
+ /** The address of the source, with `https:` or `http:`. */
65
+ readonly url: string;
66
+ readonly license: License;
67
+ /** The date when a person or a job last read the source and compared it with the units that cite it. */
68
+ readonly checked: IsoDate;
69
+ /** The name that a credit line gives, for example the agency or the authors. The default is the title. */
70
+ readonly by?: string;
71
+ }
72
+ /** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */
73
+ type UnitUse = Exclude<Use, "bundle">;
74
+ /**
75
+ * A cited unit: a quote (the text word for word), a summary (the words of the guidebook) or a fact (a value with a citation).
76
+ */
77
+ interface CitedUnit {
78
+ readonly id: string;
79
+ readonly use: UnitUse;
80
+ readonly text: string;
81
+ readonly source: SourceId;
82
+ /** The place of the text in the source, for example a section or a page. */
83
+ readonly locator?: string;
84
+ }
85
+ /**
86
+ * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.
87
+ * A guidebook prints it under each quote and each bundle whose license needs a credit.
88
+ */
89
+ export declare function creditOf(source: Source): string;
90
+ /** The codes of the findings of the gate. */
91
+ export declare const FINDING_CODES: readonly ["duplicate-id", "empty-title", "empty-text", "bad-url", "bad-date", "unknown-license", "checked-in-future", "stale", "missing-source", "use-refused"];
92
+ /** A code of a finding. */
93
+ type FindingCode = (typeof FINDING_CODES)[number];
94
+ /**
95
+ * A finding of a gate. A finding with the level `error` stops the gate. A `warning` tells of a review to do.
96
+ * An add-on gives its own codes `C` and its own kinds of record `R`.
97
+ */
98
+ interface Finding<C extends string = FindingCode, R extends string = "source" | "unit"> {
99
+ readonly level: "error" | "warning";
100
+ readonly code: C;
101
+ /** The kind of the record with the finding. */
102
+ readonly record: R;
103
+ /** The `id` of the record with the finding. */
104
+ readonly id: string;
105
+ readonly message: string;
106
+ }
107
+ /** The options of the gate. */
108
+ interface GateOptions extends UseContext {
109
+ /** The date of the gate. Without it, the gate does not examine the age of the checks. */
110
+ readonly at?: IsoDate;
111
+ /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */
112
+ readonly maxAgeDays?: number;
113
+ }
114
+ /** The result of a gate. */
115
+ interface GateResult<F extends Finding<string, string> = Finding> {
116
+ /** This field is true when the result has no finding with the level `error`. */
117
+ readonly ok: boolean;
118
+ readonly findings: readonly F[];
119
+ }
120
+ /** This function gives the result of a list of findings. */
121
+ export declare function resultOf<F extends Finding<string, string>>(findings: readonly F[]): GateResult<F>;
122
+ /** This function throws an error with each finding of the level `error`. The name of the gate starts the message. */
123
+ export declare function throwOnErrors(name: string, findings: readonly Finding<string, string>[]): void;
124
+ /** The number of milliseconds in one day. */
125
+ export declare const DAY_MS = 864e5;
126
+ /** This function gives the time of a date `YYYY-MM-DD` in milliseconds, or `undefined` when the date is not a real date. */
127
+ export declare function timeOf(date: string): number | undefined;
128
+ /** This function tells if a text is a web address that the gate accepts. */
129
+ export declare function isWebAddress(url: string): boolean;
130
+ /** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */
131
+ export declare function checkSources(sources: readonly Source[], options?: GateOptions): readonly Finding[];
132
+ /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
133
+ export declare function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options?: UseContext): readonly Finding[];
134
+ /** This function applies the gate to the sources and the units of a guidebook. */
135
+ export declare function gate(units: readonly CitedUnit[], sources: readonly Source[], options?: GateOptions): GateResult;
136
+ /** This function applies the gate, and throws an error with each finding of the level `error`. */
137
+ export declare function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options?: GateOptions): void;
138
+ /** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */
139
+ export declare function layerOf(unit: CitedUnit, source: Source): Layer;
140
+ export type { CitedUnit, Finding, FindingCode, GateOptions, GateResult, IsoDate, Layer, License, LicenseRule, Source, SourceId, UnitUse, Use, UseContext };
141
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts"],"mappings":";qBAIa;;KAaD,kBAAkB;;qBAGjB;;;;;;KAOD,cAAc;;KAGd;;UAGK;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA,OAAO;;WAEP;;WAEA;;;;;;;;;;qBAWE,eAAe,SAAS,OAAO,SAAS;;UAmEpC;;WAEN;;WAEA;;;;;;;wBAQK,QAAQ,SAAS,SAAS,KAAK,KAAK,UAAS;;KCrIjD;;KAGA;;UAGK;WACN,IAAI;WACJ;;WAEA;WACA,SAAS;;WAET,SAAS;;WAET;;;KAIC,UAAU,QAAQ;;;;UAKb;WACN;WACA,KAAK;WACL;WACA,QAAQ;;WAER;;;;;;wBAOK,SAAS,QAAQ;;qBCnCpB;;KAcD,sBAAsB;;;;;UAMjB,QAAQ,mBAAmB,aAAa;WAC9C;WACA,MAAM;;WAEN,QAAQ;;WAER;WACA;;;UAIM,oBAAoB;;WAE1B,KAAK;;WAEL;;;UAIM,WAAW,UAAU,0BAA0B;;WAErD;WACA,mBAAmB;;;wBAId,SAAS,UAAU,yBAAyB,mBAAmB,MAAM,WAAW;;wBAKhF,cAAc,cAAc,mBAAmB;;qBAMlD;;wBAIG,OAAO;;wBAeP,aAAa;;wBAKb,aAAa,kBAAkB,UAAU,UAAS,uBAA4B;;wBA8B9E,WAAW,gBAAgB,aAAa,kBAAkB,UAAU,UAAS,sBAA2B;;wBAwBxG,KAAK,gBAAgB,aAAa,kBAAkB,UAAU,UAAS,cAAmB;;wBAK1F,WAAW,gBAAgB,aAAa,kBAAkB,UAAU,UAAS;;wBAQ7E,QAAQ,MAAM,WAAW,QAAQ,SAAS"}
package/build/index.js ADDED
@@ -0,0 +1,271 @@
1
+ // src/license.ts
2
+ /** The licenses that the gate knows. The license `invented` is for test fixtures only. */
3
+ const LICENSES = [
4
+ "us-public-domain",
5
+ "public-domain",
6
+ "cc0",
7
+ "cc-by-4.0",
8
+ "cc-by-sa-4.0",
9
+ "cc-by-sa-3.0",
10
+ "cc-by-nc-sa-3.0",
11
+ "all-rights-reserved",
12
+ "invented"
13
+ ];
14
+ /** The ways to use a source. A unit of text has one use, and an offline copy of the full source is a bundle. */
15
+ const USES = [
16
+ "quote",
17
+ "summary",
18
+ "fact",
19
+ "bundle"
20
+ ];
21
+ /**
22
+ * The rule of each license.
23
+ *
24
+ * - A US federal work (17 USC 105), a work in the public domain and a CC0 work permit each use.
25
+ * - A CC BY work permits each use with a credit line. A CC BY-SA quote also needs its own layer.
26
+ * - A CC BY-NC-SA work permits a bundle only while the guidebook is not commercial. The gate refuses a quote of it.
27
+ * - A work with all rights reserved permits only a summary and a fact, with a citation.
28
+ */
29
+ const LICENSE_RULES = {
30
+ "us-public-domain": {
31
+ title: "US public domain (17 USC 105)",
32
+ url: "https://www.law.cornell.edu/uscode/text/17/105",
33
+ quote: true,
34
+ bundle: true,
35
+ nonCommercial: false,
36
+ layer: "main",
37
+ credit: false,
38
+ real: true
39
+ },
40
+ "public-domain": {
41
+ title: "Public domain",
42
+ url: "",
43
+ quote: true,
44
+ bundle: true,
45
+ nonCommercial: false,
46
+ layer: "main",
47
+ credit: false,
48
+ real: true
49
+ },
50
+ cc0: {
51
+ title: "CC0 1.0",
52
+ url: "https://creativecommons.org/publicdomain/zero/1.0/",
53
+ quote: true,
54
+ bundle: true,
55
+ nonCommercial: false,
56
+ layer: "main",
57
+ credit: false,
58
+ real: true
59
+ },
60
+ "cc-by-4.0": {
61
+ title: "CC BY 4.0",
62
+ url: "https://creativecommons.org/licenses/by/4.0/",
63
+ quote: true,
64
+ bundle: true,
65
+ nonCommercial: false,
66
+ layer: "main",
67
+ credit: true,
68
+ real: true
69
+ },
70
+ "cc-by-sa-4.0": {
71
+ title: "CC BY-SA 4.0",
72
+ url: "https://creativecommons.org/licenses/by-sa/4.0/",
73
+ quote: true,
74
+ bundle: true,
75
+ nonCommercial: false,
76
+ layer: "share-alike",
77
+ credit: true,
78
+ real: true
79
+ },
80
+ "cc-by-sa-3.0": {
81
+ title: "CC BY-SA 3.0",
82
+ url: "https://creativecommons.org/licenses/by-sa/3.0/",
83
+ quote: true,
84
+ bundle: true,
85
+ nonCommercial: false,
86
+ layer: "share-alike",
87
+ credit: true,
88
+ real: true
89
+ },
90
+ "cc-by-nc-sa-3.0": {
91
+ title: "CC BY-NC-SA 3.0",
92
+ url: "https://creativecommons.org/licenses/by-nc-sa/3.0/",
93
+ quote: false,
94
+ bundle: true,
95
+ nonCommercial: true,
96
+ layer: "share-alike",
97
+ credit: true,
98
+ real: true
99
+ },
100
+ "all-rights-reserved": {
101
+ title: "All rights reserved",
102
+ url: "",
103
+ quote: false,
104
+ bundle: false,
105
+ nonCommercial: false,
106
+ layer: "main",
107
+ credit: false,
108
+ real: true
109
+ },
110
+ invented: {
111
+ title: "Invented for a test",
112
+ url: "",
113
+ quote: false,
114
+ bundle: false,
115
+ nonCommercial: false,
116
+ layer: "main",
117
+ credit: false,
118
+ real: false
119
+ }
120
+ };
121
+ /**
122
+ * This function tells if a license permits a use.
123
+ * A summary and a fact are the words of the guidebook, so each real license permits them.
124
+ * A non-commercial license refuses each use of its text in a commercial guidebook. A summary and a fact are not its text, so they stay.
125
+ */
126
+ function permits(license, use, context = {}) {
127
+ const rule = LICENSE_RULES[license];
128
+ if (!rule.real) return context.fixture === true;
129
+ if (use === "summary" || use === "fact") return true;
130
+ if (rule.nonCommercial && context.commercial === true) return false;
131
+ return use === "quote" ? rule.quote : rule.bundle;
132
+ }
133
+ // src/source.ts
134
+ /**
135
+ * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.
136
+ * A guidebook prints it under each quote and each bundle whose license needs a credit.
137
+ */
138
+ function creditOf(source) {
139
+ const rule = LICENSE_RULES[source.license];
140
+ const name = source.by === void 0 || source.by === source.title ? source.title : `${source.by}: ${source.title}`;
141
+ const license = rule.url === "" ? rule.title : `${rule.title} (${rule.url})`;
142
+ return `${name}. ${source.url}. ${license}. Checked ${source.checked}.`;
143
+ }
144
+ // src/gate.ts
145
+ /** The codes of the findings of the gate. */
146
+ const FINDING_CODES = [
147
+ "duplicate-id",
148
+ "empty-title",
149
+ "empty-text",
150
+ "bad-url",
151
+ "bad-date",
152
+ "unknown-license",
153
+ "checked-in-future",
154
+ "stale",
155
+ "missing-source",
156
+ "use-refused"
157
+ ];
158
+ /** This function gives the result of a list of findings. */
159
+ function resultOf(findings) {
160
+ return {
161
+ ok: findings.every((finding) => finding.level !== "error"),
162
+ findings
163
+ };
164
+ }
165
+ /** This function throws an error with each finding of the level `error`. The name of the gate starts the message. */
166
+ function throwOnErrors(name, findings) {
167
+ const lines = findings.filter((finding) => finding.level === "error").map((finding) => `- ${finding.code}: ${finding.message}`);
168
+ if (lines.length > 0) throw new Error(`The ${name} found ${lines.length} errors:\n${lines.join("\n")}`);
169
+ }
170
+ /** The number of milliseconds in one day. */
171
+ const DAY_MS = 864e5;
172
+ const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
173
+ /** This function gives the time of a date `YYYY-MM-DD` in milliseconds, or `undefined` when the date is not a real date. */
174
+ function timeOf(date) {
175
+ if (!ISO_DATE.test(date)) return void 0;
176
+ const time = Date.parse(`${date}T00:00:00Z`);
177
+ if (Number.isNaN(time)) return void 0;
178
+ return new Date(time).toISOString().slice(0, 10) === date ? time : void 0;
179
+ }
180
+ function isLicense(value) {
181
+ return typeof value === "string" && LICENSES.includes(value);
182
+ }
183
+ /** An address with `https:` or `http:`, a host, and no space. The core uses no platform type, so it does not use `URL`. */
184
+ const WEB_ADDRESS = /^https?:\/\/[a-z0-9.-]+(?::\d+)?(?:[/?#]\S*)?$/iu;
185
+ /** This function tells if a text is a web address that the gate accepts. */
186
+ function isWebAddress(url) {
187
+ return WEB_ADDRESS.test(url);
188
+ }
189
+ /** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */
190
+ function checkSources(sources, options = {}) {
191
+ const out = [];
192
+ const error = (code, id, message) => {
193
+ out.push({
194
+ level: "error",
195
+ code,
196
+ record: "source",
197
+ id,
198
+ message
199
+ });
200
+ };
201
+ const at = options.at === void 0 ? void 0 : timeOf(options.at);
202
+ if (options.at !== void 0 && at === void 0) throw new RangeError(`checkSources: the date "${options.at}" of the gate is not a date YYYY-MM-DD.`);
203
+ const maxAge = options.maxAgeDays ?? 365;
204
+ const seen = /* @__PURE__ */ new Set();
205
+ for (const source of sources) {
206
+ if (seen.has(source.id)) error("duplicate-id", source.id, `Two sources have the id "${source.id}".`);
207
+ seen.add(source.id);
208
+ if (source.title.trim() === "") error("empty-title", source.id, `The source "${source.id}" has no title.`);
209
+ if (!isWebAddress(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". It needs an address with https: or http:.`);
210
+ if (!isLicense(source.license)) error("unknown-license", source.id, `The source "${source.id}" has the unknown license "${String(source.license)}".`);
211
+ const checked = timeOf(source.checked);
212
+ if (checked === void 0) error("bad-date", source.id, `The source "${source.id}" has the check date "${source.checked}". It needs a date YYYY-MM-DD.`);
213
+ else if (at !== void 0) {
214
+ if (checked > at) error("checked-in-future", source.id, `The source "${source.id}" has a check date after the date of the gate.`);
215
+ const days = (at - checked) / DAY_MS;
216
+ if (days > maxAge) out.push({
217
+ level: "warning",
218
+ code: "stale",
219
+ record: "source",
220
+ id: source.id,
221
+ message: `The last check of the source "${source.id}" is ${days} days old. Read the source again.`
222
+ });
223
+ }
224
+ }
225
+ return out;
226
+ }
227
+ /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
228
+ function checkUnits(units, sources, options = {}) {
229
+ const out = [];
230
+ const error = (code, id, message) => {
231
+ out.push({
232
+ level: "error",
233
+ code,
234
+ record: "unit",
235
+ id,
236
+ message
237
+ });
238
+ };
239
+ const byId = new Map(sources.map((source) => [source.id, source]));
240
+ const seen = /* @__PURE__ */ new Set();
241
+ for (const unit of units) {
242
+ if (seen.has(unit.id)) error("duplicate-id", unit.id, `Two units have the id "${unit.id}".`);
243
+ seen.add(unit.id);
244
+ if (unit.text.trim() === "") error("empty-text", unit.id, `The unit "${unit.id}" has no text.`);
245
+ const source = byId.get(unit.source);
246
+ if (source === void 0) {
247
+ error("missing-source", unit.id, `The unit "${unit.id}" cites the source "${unit.source}", but the list does not have it.`);
248
+ continue;
249
+ }
250
+ if (isLicense(source.license) && !permits(source.license, unit.use, options)) error("use-refused", unit.id, `The license "${source.license}" of the source "${source.id}" refuses the use "${unit.use}" of the unit "${unit.id}".`);
251
+ }
252
+ return out;
253
+ }
254
+ /** This function applies the gate to the sources and the units of a guidebook. */
255
+ function gate(units, sources, options = {}) {
256
+ return resultOf([...checkSources(sources, options), ...checkUnits(units, sources, options)]);
257
+ }
258
+ /** This function applies the gate, and throws an error with each finding of the level `error`. */
259
+ function assertGate(units, sources, options = {}) {
260
+ const result = gate(units, sources, options);
261
+ if (result.ok) return;
262
+ const lines = result.findings.filter((finding) => finding.level === "error").map((finding) => `- ${finding.code}: ${finding.message}`);
263
+ throw new Error(`The source gate found ${lines.length} errors:\n${lines.join("\n")}`);
264
+ }
265
+ /** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */
266
+ function layerOf(unit, source) {
267
+ return unit.use === "quote" ? LICENSE_RULES[source.license].layer : "main";
268
+ }
269
+ export { DAY_MS, FINDING_CODES, LICENSES, LICENSE_RULES, USES, assertGate, checkSources, checkUnits, creditOf, gate, isWebAddress, layerOf, permits, resultOf, throwOnErrors, timeOf };
270
+
271
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts"],"sourcesContent":["// The licenses of the sources, and the rule of each license.\n// The rules come from the text of each license. A rule tells what a guidebook can do with the text of a source.\n\n/** The licenses that the gate knows. The license `invented` is for test fixtures only. */\nexport const LICENSES = [\n \"us-public-domain\",\n \"public-domain\",\n \"cc0\",\n \"cc-by-4.0\",\n \"cc-by-sa-4.0\",\n \"cc-by-sa-3.0\",\n \"cc-by-nc-sa-3.0\",\n \"all-rights-reserved\",\n \"invented\",\n] as const;\n\n/** A license of a source. The source gate refuses a use that the license does not permit. */\nexport type License = (typeof LICENSES)[number];\n\n/** The ways to use a source. A unit of text has one use, and an offline copy of the full source is a bundle. */\nexport const USES = [\"quote\", \"summary\", \"fact\", \"bundle\"] as const;\n\n/**\n * A use of a source.\n * `quote` is the text word for word. `summary` is the text in the words of the guidebook. `fact` is a value with a citation.\n * `bundle` is an offline copy of the full source.\n */\nexport type Use = (typeof USES)[number];\n\n/** The layer of a printed quote. A share-alike quote prints in its own layer, with its credit and its license. */\nexport type Layer = \"main\" | \"share-alike\";\n\n/** The rule of one license. */\nexport interface LicenseRule {\n /** The name of the license that a credit line prints. */\n readonly title: string;\n /** The address of the text of the license, or an empty string when the license has no text. */\n readonly url: string;\n /** This field is true when a guidebook can print the text of the source word for word. */\n readonly quote: boolean;\n /** This field is true when a guidebook can keep an offline copy of the full source. */\n readonly bundle: boolean;\n /** This field is true when the license permits only a use that is not commercial. */\n readonly nonCommercial: boolean;\n /** The layer of a quote. A share-alike license needs its own layer, so the rest of the guidebook keeps its own license. */\n readonly layer: Layer;\n /** This field is true when a quote or a bundle must print the credit line of the source. */\n readonly credit: boolean;\n /** This field is false for the license `invented`. A real guidebook cites only real sources. */\n readonly real: boolean;\n}\n\n/**\n * The rule of each license.\n *\n * - A US federal work (17 USC 105), a work in the public domain and a CC0 work permit each use.\n * - A CC BY work permits each use with a credit line. A CC BY-SA quote also needs its own layer.\n * - A CC BY-NC-SA work permits a bundle only while the guidebook is not commercial. The gate refuses a quote of it.\n * - A work with all rights reserved permits only a summary and a fact, with a citation.\n */\nexport const LICENSE_RULES: Readonly<Record<License, LicenseRule>> = {\n \"us-public-domain\": {\n title: \"US public domain (17 USC 105)\",\n url: \"https://www.law.cornell.edu/uscode/text/17/105\",\n quote: true,\n bundle: true,\n nonCommercial: false,\n layer: \"main\",\n credit: false,\n real: true,\n },\n \"public-domain\": { title: \"Public domain\", url: \"\", quote: true, bundle: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n cc0: {\n title: \"CC0 1.0\",\n url: \"https://creativecommons.org/publicdomain/zero/1.0/\",\n quote: true,\n bundle: true,\n nonCommercial: false,\n layer: \"main\",\n credit: false,\n real: true,\n },\n \"cc-by-4.0\": {\n title: \"CC BY 4.0\",\n url: \"https://creativecommons.org/licenses/by/4.0/\",\n quote: true,\n bundle: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"cc-by-sa-4.0\": {\n title: \"CC BY-SA 4.0\",\n url: \"https://creativecommons.org/licenses/by-sa/4.0/\",\n quote: true,\n bundle: true,\n nonCommercial: false,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"cc-by-sa-3.0\": {\n title: \"CC BY-SA 3.0\",\n url: \"https://creativecommons.org/licenses/by-sa/3.0/\",\n quote: true,\n bundle: true,\n nonCommercial: false,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"cc-by-nc-sa-3.0\": {\n title: \"CC BY-NC-SA 3.0\",\n url: \"https://creativecommons.org/licenses/by-nc-sa/3.0/\",\n quote: false,\n bundle: true,\n nonCommercial: true,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"all-rights-reserved\": { title: \"All rights reserved\", url: \"\", quote: false, bundle: false, nonCommercial: false, layer: \"main\", credit: false, real: true },\n invented: { title: \"Invented for a test\", url: \"\", quote: false, bundle: false, nonCommercial: false, layer: \"main\", credit: false, real: false },\n};\n\n/** The context of a use. A guidebook that is sold, or that shows paid advertisements, is commercial. */\nexport interface UseContext {\n /** This field is true when the guidebook is commercial. The default is false. */\n readonly commercial?: boolean;\n /** This field is true when the guidebook is a test fixture, so the license `invented` passes. The default is false. */\n readonly fixture?: boolean;\n}\n\n/**\n * This function tells if a license permits a use.\n * A summary and a fact are the words of the guidebook, so each real license permits them.\n * A non-commercial license refuses each use of its text in a commercial guidebook. A summary and a fact are not its text, so they stay.\n */\nexport function permits(license: License, use: Use, context: UseContext = {}): boolean {\n const rule = LICENSE_RULES[license];\n if (!rule.real) return context.fixture === true;\n if (use === \"summary\" || use === \"fact\") return true;\n if (rule.nonCommercial && context.commercial === true) return false;\n return use === \"quote\" ? rule.quote : rule.bundle;\n}\n","// The records of the core: a source and a cited unit of text.\n\nimport { LICENSE_RULES } from \"./license.ts\";\nimport type { License, Use } from \"./license.ts\";\n\n/** A date as `YYYY-MM-DD`. */\nexport type IsoDate = string;\n\n/** The `id` of a source. */\nexport type SourceId = string;\n\n/** A page or a document, with its license and the date of the last check. The license `invented` is for test fixtures only. */\nexport interface Source {\n readonly id: SourceId;\n readonly title: string;\n /** The address of the source, with `https:` or `http:`. */\n readonly url: string;\n readonly license: License;\n /** The date when a person or a job last read the source and compared it with the units that cite it. */\n readonly checked: IsoDate;\n /** The name that a credit line gives, for example the agency or the authors. The default is the title. */\n readonly by?: string;\n}\n\n/** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */\nexport type UnitUse = Exclude<Use, \"bundle\">;\n\n/**\n * A cited unit: a quote (the text word for word), a summary (the words of the guidebook) or a fact (a value with a citation).\n */\nexport interface CitedUnit {\n readonly id: string;\n readonly use: UnitUse;\n readonly text: string;\n readonly source: SourceId;\n /** The place of the text in the source, for example a section or a page. */\n readonly locator?: string;\n}\n\n/**\n * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.\n * A guidebook prints it under each quote and each bundle whose license needs a credit.\n */\nexport function creditOf(source: Source): string {\n const rule = LICENSE_RULES[source.license];\n const name = source.by === undefined || source.by === source.title ? source.title : `${source.by}: ${source.title}`;\n const license = rule.url === \"\" ? rule.title : `${rule.title} (${rule.url})`;\n return `${name}. ${source.url}. ${license}. Checked ${source.checked}.`;\n}\n","// The source gate: the checks of the sources and the cited units of a guidebook.\n// The gate gives each finding as data. A caller prints the findings, or stops with `assertGate`.\n\nimport { LICENSE_RULES, LICENSES, permits } from \"./license.ts\";\nimport type { Layer, License, UseContext } from \"./license.ts\";\nimport type { CitedUnit, IsoDate, Source, SourceId } from \"./source.ts\";\n\n/** The codes of the findings of the gate. */\nexport const FINDING_CODES = [\n \"duplicate-id\",\n \"empty-title\",\n \"empty-text\",\n \"bad-url\",\n \"bad-date\",\n \"unknown-license\",\n \"checked-in-future\",\n \"stale\",\n \"missing-source\",\n \"use-refused\",\n] as const;\n\n/** A code of a finding. */\nexport type FindingCode = (typeof FINDING_CODES)[number];\n\n/**\n * A finding of a gate. A finding with the level `error` stops the gate. A `warning` tells of a review to do.\n * An add-on gives its own codes `C` and its own kinds of record `R`.\n */\nexport interface Finding<C extends string = FindingCode, R extends string = \"source\" | \"unit\"> {\n readonly level: \"error\" | \"warning\";\n readonly code: C;\n /** The kind of the record with the finding. */\n readonly record: R;\n /** The `id` of the record with the finding. */\n readonly id: string;\n readonly message: string;\n}\n\n/** The options of the gate. */\nexport interface GateOptions extends UseContext {\n /** The date of the gate. Without it, the gate does not examine the age of the checks. */\n readonly at?: IsoDate;\n /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */\n readonly maxAgeDays?: number;\n}\n\n/** The result of a gate. */\nexport interface GateResult<F extends Finding<string, string> = Finding> {\n /** This field is true when the result has no finding with the level `error`. */\n readonly ok: boolean;\n readonly findings: readonly F[];\n}\n\n/** This function gives the result of a list of findings. */\nexport function resultOf<F extends Finding<string, string>>(findings: readonly F[]): GateResult<F> {\n return { ok: findings.every((finding) => finding.level !== \"error\"), findings };\n}\n\n/** This function throws an error with each finding of the level `error`. The name of the gate starts the message. */\nexport function throwOnErrors(name: string, findings: readonly Finding<string, string>[]): void {\n const lines = findings.filter((finding) => finding.level === \"error\").map((finding) => `- ${finding.code}: ${finding.message}`);\n if (lines.length > 0) throw new Error(`The ${name} found ${lines.length} errors:\\n${lines.join(\"\\n\")}`);\n}\n\n/** The number of milliseconds in one day. */\nexport const DAY_MS = 86_400_000;\nconst ISO_DATE = /^\\d{4}-\\d{2}-\\d{2}$/;\n\n/** This function gives the time of a date `YYYY-MM-DD` in milliseconds, or `undefined` when the date is not a real date. */\nexport function timeOf(date: string): number | undefined {\n if (!ISO_DATE.test(date)) return undefined;\n const time = Date.parse(`${date}T00:00:00Z`);\n if (Number.isNaN(time)) return undefined;\n return new Date(time).toISOString().slice(0, 10) === date ? time : undefined;\n}\n\nfunction isLicense(value: unknown): value is License {\n return typeof value === \"string\" && (LICENSES as readonly string[]).includes(value);\n}\n\n/** An address with `https:` or `http:`, a host, and no space. The core uses no platform type, so it does not use `URL`. */\nconst WEB_ADDRESS = /^https?:\\/\\/[a-z0-9.-]+(?::\\d+)?(?:[/?#]\\S*)?$/iu;\n\n/** This function tells if a text is a web address that the gate accepts. */\nexport function isWebAddress(url: string): boolean {\n return WEB_ADDRESS.test(url);\n}\n\n/** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */\nexport function checkSources(sources: readonly Source[], options: GateOptions = {}): readonly Finding[] {\n const out: Finding[] = [];\n const error = (code: FindingCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"source\", id, message });\n };\n const at = options.at === undefined ? undefined : timeOf(options.at);\n if (options.at !== undefined && at === undefined) throw new RangeError(`checkSources: the date \"${options.at}\" of the gate is not a date YYYY-MM-DD.`);\n const maxAge = options.maxAgeDays ?? 365;\n const seen = new Set<SourceId>();\n for (const source of sources) {\n if (seen.has(source.id)) error(\"duplicate-id\", source.id, `Two sources have the id \"${source.id}\".`);\n seen.add(source.id);\n if (source.title.trim() === \"\") error(\"empty-title\", source.id, `The source \"${source.id}\" has no title.`);\n if (!isWebAddress(source.url)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the address \"${source.url}\". It needs an address with https: or http:.`);\n if (!isLicense(source.license)) error(\"unknown-license\", source.id, `The source \"${source.id}\" has the unknown license \"${String(source.license)}\".`);\n const checked = timeOf(source.checked);\n if (checked === undefined) {\n error(\"bad-date\", source.id, `The source \"${source.id}\" has the check date \"${source.checked}\". It needs a date YYYY-MM-DD.`);\n } else if (at !== undefined) {\n if (checked > at) error(\"checked-in-future\", source.id, `The source \"${source.id}\" has a check date after the date of the gate.`);\n const days = (at - checked) / DAY_MS;\n if (days > maxAge) {\n out.push({ level: \"warning\", code: \"stale\", record: \"source\", id: source.id, message: `The last check of the source \"${source.id}\" is ${days} days old. Read the source again.` });\n }\n }\n }\n return out;\n}\n\n/** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */\nexport function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options: UseContext = {}): readonly Finding[] {\n const out: Finding[] = [];\n const error = (code: FindingCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"unit\", id, message });\n };\n const byId = new Map(sources.map((source) => [source.id, source] as const));\n const seen = new Set<string>();\n for (const unit of units) {\n if (seen.has(unit.id)) error(\"duplicate-id\", unit.id, `Two units have the id \"${unit.id}\".`);\n seen.add(unit.id);\n if (unit.text.trim() === \"\") error(\"empty-text\", unit.id, `The unit \"${unit.id}\" has no text.`);\n const source = byId.get(unit.source);\n if (source === undefined) {\n error(\"missing-source\", unit.id, `The unit \"${unit.id}\" cites the source \"${unit.source}\", but the list does not have it.`);\n continue;\n }\n if (isLicense(source.license) && !permits(source.license, unit.use, options)) {\n error(\"use-refused\", unit.id, `The license \"${source.license}\" of the source \"${source.id}\" refuses the use \"${unit.use}\" of the unit \"${unit.id}\".`);\n }\n }\n return out;\n}\n\n/** This function applies the gate to the sources and the units of a guidebook. */\nexport function gate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): GateResult {\n return resultOf([...checkSources(sources, options), ...checkUnits(units, sources, options)]);\n}\n\n/** This function applies the gate, and throws an error with each finding of the level `error`. */\nexport function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): void {\n const result = gate(units, sources, options);\n if (result.ok) return;\n const lines = result.findings.filter((finding) => finding.level === \"error\").map((finding) => `- ${finding.code}: ${finding.message}`);\n throw new Error(`The source gate found ${lines.length} errors:\\n${lines.join(\"\\n\")}`);\n}\n\n/** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */\nexport function layerOf(unit: CitedUnit, source: Source): Layer {\n return unit.use === \"quote\" ? LICENSE_RULES[source.license].layer : \"main\";\n}\n"],"mappings":"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"}
package/package.json CHANGED
@@ -1,13 +1,62 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package",
2
3
  "name": "@mark1russell7/guidebook",
3
- "version": "0.0.0-bootstrap.0",
4
- "description": "A placeholder that lets npm trust the release workflow of mark1russell7/guidebook. Use a later version.",
4
+ "version": "0.1.0",
5
+ "description": "The core of a guidebook of cited knowledge: sources with their licenses, cited units (a quote, a summary or a fact) and the source gate. The core names no topic.",
6
+ "keywords": [
7
+ "guidebook",
8
+ "citation",
9
+ "license",
10
+ "source",
11
+ "creative-commons",
12
+ "public-domain",
13
+ "typescript"
14
+ ],
15
+ "homepage": "https://github.com/mark1russell7/guidebook#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/mark1russell7/guidebook/issues"
18
+ },
5
19
  "repository": {
6
20
  "type": "git",
7
- "url": "git+https://github.com/mark1russell7/guidebook.git"
21
+ "url": "git+https://github.com/mark1russell7/guidebook.git",
22
+ "directory": "packages/guidebook"
8
23
  },
9
24
  "license": "MIT",
25
+ "author": "Mark Russell (https://github.com/mark1russell7)",
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./build/index.js",
29
+ "types": "./build/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./build/index.d.ts",
33
+ "default": "./build/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "build",
39
+ "src",
40
+ "!src/**/*.test.ts"
41
+ ],
42
+ "engines": {
43
+ "node": ">=22.12"
44
+ },
10
45
  "publishConfig": {
11
46
  "access": "public"
47
+ },
48
+ "devDependencies": {
49
+ "@mark1russell7/cue": "^2.0.0",
50
+ "@types/node": "^26.6.5",
51
+ "@vitest/coverage-v8": "^5.0.3",
52
+ "typescript": "^7.0.2",
53
+ "vitest": "^5.0.3",
54
+ "tsdown": "^0.23.7"
55
+ },
56
+ "scripts": {
57
+ "build": "tsdown",
58
+ "test": "vitest run",
59
+ "test:watch": "vitest",
60
+ "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
12
61
  }
13
- }
62
+ }
package/src/gate.ts ADDED
@@ -0,0 +1,159 @@
1
+ // The source gate: the checks of the sources and the cited units of a guidebook.
2
+ // The gate gives each finding as data. A caller prints the findings, or stops with `assertGate`.
3
+
4
+ import { LICENSE_RULES, LICENSES, permits } from "./license.ts";
5
+ import type { Layer, License, UseContext } from "./license.ts";
6
+ import type { CitedUnit, IsoDate, Source, SourceId } from "./source.ts";
7
+
8
+ /** The codes of the findings of the gate. */
9
+ export const FINDING_CODES = [
10
+ "duplicate-id",
11
+ "empty-title",
12
+ "empty-text",
13
+ "bad-url",
14
+ "bad-date",
15
+ "unknown-license",
16
+ "checked-in-future",
17
+ "stale",
18
+ "missing-source",
19
+ "use-refused",
20
+ ] as const;
21
+
22
+ /** A code of a finding. */
23
+ export type FindingCode = (typeof FINDING_CODES)[number];
24
+
25
+ /**
26
+ * A finding of a gate. A finding with the level `error` stops the gate. A `warning` tells of a review to do.
27
+ * An add-on gives its own codes `C` and its own kinds of record `R`.
28
+ */
29
+ export interface Finding<C extends string = FindingCode, R extends string = "source" | "unit"> {
30
+ readonly level: "error" | "warning";
31
+ readonly code: C;
32
+ /** The kind of the record with the finding. */
33
+ readonly record: R;
34
+ /** The `id` of the record with the finding. */
35
+ readonly id: string;
36
+ readonly message: string;
37
+ }
38
+
39
+ /** The options of the gate. */
40
+ export interface GateOptions extends UseContext {
41
+ /** The date of the gate. Without it, the gate does not examine the age of the checks. */
42
+ readonly at?: IsoDate;
43
+ /** A source whose check is older than this number of days gets the warning `stale`. The default is 365. */
44
+ readonly maxAgeDays?: number;
45
+ }
46
+
47
+ /** The result of a gate. */
48
+ export interface GateResult<F extends Finding<string, string> = Finding> {
49
+ /** This field is true when the result has no finding with the level `error`. */
50
+ readonly ok: boolean;
51
+ readonly findings: readonly F[];
52
+ }
53
+
54
+ /** This function gives the result of a list of findings. */
55
+ export function resultOf<F extends Finding<string, string>>(findings: readonly F[]): GateResult<F> {
56
+ return { ok: findings.every((finding) => finding.level !== "error"), findings };
57
+ }
58
+
59
+ /** This function throws an error with each finding of the level `error`. The name of the gate starts the message. */
60
+ export function throwOnErrors(name: string, findings: readonly Finding<string, string>[]): void {
61
+ const lines = findings.filter((finding) => finding.level === "error").map((finding) => `- ${finding.code}: ${finding.message}`);
62
+ if (lines.length > 0) throw new Error(`The ${name} found ${lines.length} errors:\n${lines.join("\n")}`);
63
+ }
64
+
65
+ /** The number of milliseconds in one day. */
66
+ export const DAY_MS = 86_400_000;
67
+ const ISO_DATE = /^\d{4}-\d{2}-\d{2}$/;
68
+
69
+ /** This function gives the time of a date `YYYY-MM-DD` in milliseconds, or `undefined` when the date is not a real date. */
70
+ export function timeOf(date: string): number | undefined {
71
+ if (!ISO_DATE.test(date)) return undefined;
72
+ const time = Date.parse(`${date}T00:00:00Z`);
73
+ if (Number.isNaN(time)) return undefined;
74
+ return new Date(time).toISOString().slice(0, 10) === date ? time : undefined;
75
+ }
76
+
77
+ function isLicense(value: unknown): value is License {
78
+ return typeof value === "string" && (LICENSES as readonly string[]).includes(value);
79
+ }
80
+
81
+ /** An address with `https:` or `http:`, a host, and no space. The core uses no platform type, so it does not use `URL`. */
82
+ const WEB_ADDRESS = /^https?:\/\/[a-z0-9.-]+(?::\d+)?(?:[/?#]\S*)?$/iu;
83
+
84
+ /** This function tells if a text is a web address that the gate accepts. */
85
+ export function isWebAddress(url: string): boolean {
86
+ return WEB_ADDRESS.test(url);
87
+ }
88
+
89
+ /** This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks. */
90
+ export function checkSources(sources: readonly Source[], options: GateOptions = {}): readonly Finding[] {
91
+ const out: Finding[] = [];
92
+ const error = (code: FindingCode, id: string, message: string): void => {
93
+ out.push({ level: "error", code, record: "source", id, message });
94
+ };
95
+ const at = options.at === undefined ? undefined : timeOf(options.at);
96
+ if (options.at !== undefined && at === undefined) throw new RangeError(`checkSources: the date "${options.at}" of the gate is not a date YYYY-MM-DD.`);
97
+ const maxAge = options.maxAgeDays ?? 365;
98
+ const seen = new Set<SourceId>();
99
+ for (const source of sources) {
100
+ if (seen.has(source.id)) error("duplicate-id", source.id, `Two sources have the id "${source.id}".`);
101
+ seen.add(source.id);
102
+ if (source.title.trim() === "") error("empty-title", source.id, `The source "${source.id}" has no title.`);
103
+ if (!isWebAddress(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". It needs an address with https: or http:.`);
104
+ if (!isLicense(source.license)) error("unknown-license", source.id, `The source "${source.id}" has the unknown license "${String(source.license)}".`);
105
+ const checked = timeOf(source.checked);
106
+ if (checked === undefined) {
107
+ error("bad-date", source.id, `The source "${source.id}" has the check date "${source.checked}". It needs a date YYYY-MM-DD.`);
108
+ } else if (at !== undefined) {
109
+ if (checked > at) error("checked-in-future", source.id, `The source "${source.id}" has a check date after the date of the gate.`);
110
+ const days = (at - checked) / DAY_MS;
111
+ if (days > maxAge) {
112
+ out.push({ level: "warning", code: "stale", record: "source", id: source.id, message: `The last check of the source "${source.id}" is ${days} days old. Read the source again.` });
113
+ }
114
+ }
115
+ }
116
+ return out;
117
+ }
118
+
119
+ /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
120
+ export function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options: UseContext = {}): readonly Finding[] {
121
+ const out: Finding[] = [];
122
+ const error = (code: FindingCode, id: string, message: string): void => {
123
+ out.push({ level: "error", code, record: "unit", id, message });
124
+ };
125
+ const byId = new Map(sources.map((source) => [source.id, source] as const));
126
+ const seen = new Set<string>();
127
+ for (const unit of units) {
128
+ if (seen.has(unit.id)) error("duplicate-id", unit.id, `Two units have the id "${unit.id}".`);
129
+ seen.add(unit.id);
130
+ if (unit.text.trim() === "") error("empty-text", unit.id, `The unit "${unit.id}" has no text.`);
131
+ const source = byId.get(unit.source);
132
+ if (source === undefined) {
133
+ error("missing-source", unit.id, `The unit "${unit.id}" cites the source "${unit.source}", but the list does not have it.`);
134
+ continue;
135
+ }
136
+ if (isLicense(source.license) && !permits(source.license, unit.use, options)) {
137
+ error("use-refused", unit.id, `The license "${source.license}" of the source "${source.id}" refuses the use "${unit.use}" of the unit "${unit.id}".`);
138
+ }
139
+ }
140
+ return out;
141
+ }
142
+
143
+ /** This function applies the gate to the sources and the units of a guidebook. */
144
+ export function gate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): GateResult {
145
+ return resultOf([...checkSources(sources, options), ...checkUnits(units, sources, options)]);
146
+ }
147
+
148
+ /** This function applies the gate, and throws an error with each finding of the level `error`. */
149
+ export function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options: GateOptions = {}): void {
150
+ const result = gate(units, sources, options);
151
+ if (result.ok) return;
152
+ const lines = result.findings.filter((finding) => finding.level === "error").map((finding) => `- ${finding.code}: ${finding.message}`);
153
+ throw new Error(`The source gate found ${lines.length} errors:\n${lines.join("\n")}`);
154
+ }
155
+
156
+ /** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */
157
+ export function layerOf(unit: CitedUnit, source: Source): Layer {
158
+ return unit.use === "quote" ? LICENSE_RULES[source.license].layer : "main";
159
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ // The core of the guidebook: sources with their licenses, cited units of text, and the source gate.
2
+ // The core names no topic. Each topic is an add-on package that gives its own records.
3
+
4
+ export { LICENSES, LICENSE_RULES, USES, permits } from "./license.ts";
5
+ export type { Layer, License, LicenseRule, Use, UseContext } from "./license.ts";
6
+ export { creditOf } from "./source.ts";
7
+ export type { CitedUnit, IsoDate, Source, SourceId, UnitUse } from "./source.ts";
8
+ export { DAY_MS, FINDING_CODES, assertGate, checkSources, checkUnits, gate, isWebAddress, layerOf, resultOf, throwOnErrors, timeOf } from "./gate.ts";
9
+ export type { Finding, FindingCode, GateOptions, GateResult } from "./gate.ts";
package/src/license.ts ADDED
@@ -0,0 +1,146 @@
1
+ // The licenses of the sources, and the rule of each license.
2
+ // The rules come from the text of each license. A rule tells what a guidebook can do with the text of a source.
3
+
4
+ /** The licenses that the gate knows. The license `invented` is for test fixtures only. */
5
+ export const LICENSES = [
6
+ "us-public-domain",
7
+ "public-domain",
8
+ "cc0",
9
+ "cc-by-4.0",
10
+ "cc-by-sa-4.0",
11
+ "cc-by-sa-3.0",
12
+ "cc-by-nc-sa-3.0",
13
+ "all-rights-reserved",
14
+ "invented",
15
+ ] as const;
16
+
17
+ /** A license of a source. The source gate refuses a use that the license does not permit. */
18
+ export type License = (typeof LICENSES)[number];
19
+
20
+ /** The ways to use a source. A unit of text has one use, and an offline copy of the full source is a bundle. */
21
+ export const USES = ["quote", "summary", "fact", "bundle"] as const;
22
+
23
+ /**
24
+ * A use of a source.
25
+ * `quote` is the text word for word. `summary` is the text in the words of the guidebook. `fact` is a value with a citation.
26
+ * `bundle` is an offline copy of the full source.
27
+ */
28
+ export type Use = (typeof USES)[number];
29
+
30
+ /** The layer of a printed quote. A share-alike quote prints in its own layer, with its credit and its license. */
31
+ export type Layer = "main" | "share-alike";
32
+
33
+ /** The rule of one license. */
34
+ export interface LicenseRule {
35
+ /** The name of the license that a credit line prints. */
36
+ readonly title: string;
37
+ /** The address of the text of the license, or an empty string when the license has no text. */
38
+ readonly url: string;
39
+ /** This field is true when a guidebook can print the text of the source word for word. */
40
+ readonly quote: boolean;
41
+ /** This field is true when a guidebook can keep an offline copy of the full source. */
42
+ readonly bundle: boolean;
43
+ /** This field is true when the license permits only a use that is not commercial. */
44
+ readonly nonCommercial: boolean;
45
+ /** The layer of a quote. A share-alike license needs its own layer, so the rest of the guidebook keeps its own license. */
46
+ readonly layer: Layer;
47
+ /** This field is true when a quote or a bundle must print the credit line of the source. */
48
+ readonly credit: boolean;
49
+ /** This field is false for the license `invented`. A real guidebook cites only real sources. */
50
+ readonly real: boolean;
51
+ }
52
+
53
+ /**
54
+ * The rule of each license.
55
+ *
56
+ * - A US federal work (17 USC 105), a work in the public domain and a CC0 work permit each use.
57
+ * - A CC BY work permits each use with a credit line. A CC BY-SA quote also needs its own layer.
58
+ * - A CC BY-NC-SA work permits a bundle only while the guidebook is not commercial. The gate refuses a quote of it.
59
+ * - A work with all rights reserved permits only a summary and a fact, with a citation.
60
+ */
61
+ export const LICENSE_RULES: Readonly<Record<License, LicenseRule>> = {
62
+ "us-public-domain": {
63
+ title: "US public domain (17 USC 105)",
64
+ url: "https://www.law.cornell.edu/uscode/text/17/105",
65
+ quote: true,
66
+ bundle: true,
67
+ nonCommercial: false,
68
+ layer: "main",
69
+ credit: false,
70
+ real: true,
71
+ },
72
+ "public-domain": { title: "Public domain", url: "", quote: true, bundle: true, nonCommercial: false, layer: "main", credit: false, real: true },
73
+ cc0: {
74
+ title: "CC0 1.0",
75
+ url: "https://creativecommons.org/publicdomain/zero/1.0/",
76
+ quote: true,
77
+ bundle: true,
78
+ nonCommercial: false,
79
+ layer: "main",
80
+ credit: false,
81
+ real: true,
82
+ },
83
+ "cc-by-4.0": {
84
+ title: "CC BY 4.0",
85
+ url: "https://creativecommons.org/licenses/by/4.0/",
86
+ quote: true,
87
+ bundle: true,
88
+ nonCommercial: false,
89
+ layer: "main",
90
+ credit: true,
91
+ real: true,
92
+ },
93
+ "cc-by-sa-4.0": {
94
+ title: "CC BY-SA 4.0",
95
+ url: "https://creativecommons.org/licenses/by-sa/4.0/",
96
+ quote: true,
97
+ bundle: true,
98
+ nonCommercial: false,
99
+ layer: "share-alike",
100
+ credit: true,
101
+ real: true,
102
+ },
103
+ "cc-by-sa-3.0": {
104
+ title: "CC BY-SA 3.0",
105
+ url: "https://creativecommons.org/licenses/by-sa/3.0/",
106
+ quote: true,
107
+ bundle: true,
108
+ nonCommercial: false,
109
+ layer: "share-alike",
110
+ credit: true,
111
+ real: true,
112
+ },
113
+ "cc-by-nc-sa-3.0": {
114
+ title: "CC BY-NC-SA 3.0",
115
+ url: "https://creativecommons.org/licenses/by-nc-sa/3.0/",
116
+ quote: false,
117
+ bundle: true,
118
+ nonCommercial: true,
119
+ layer: "share-alike",
120
+ credit: true,
121
+ real: true,
122
+ },
123
+ "all-rights-reserved": { title: "All rights reserved", url: "", quote: false, bundle: false, nonCommercial: false, layer: "main", credit: false, real: true },
124
+ invented: { title: "Invented for a test", url: "", quote: false, bundle: false, nonCommercial: false, layer: "main", credit: false, real: false },
125
+ };
126
+
127
+ /** The context of a use. A guidebook that is sold, or that shows paid advertisements, is commercial. */
128
+ export interface UseContext {
129
+ /** This field is true when the guidebook is commercial. The default is false. */
130
+ readonly commercial?: boolean;
131
+ /** This field is true when the guidebook is a test fixture, so the license `invented` passes. The default is false. */
132
+ readonly fixture?: boolean;
133
+ }
134
+
135
+ /**
136
+ * This function tells if a license permits a use.
137
+ * A summary and a fact are the words of the guidebook, so each real license permits them.
138
+ * A non-commercial license refuses each use of its text in a commercial guidebook. A summary and a fact are not its text, so they stay.
139
+ */
140
+ export function permits(license: License, use: Use, context: UseContext = {}): boolean {
141
+ const rule = LICENSE_RULES[license];
142
+ if (!rule.real) return context.fixture === true;
143
+ if (use === "summary" || use === "fact") return true;
144
+ if (rule.nonCommercial && context.commercial === true) return false;
145
+ return use === "quote" ? rule.quote : rule.bundle;
146
+ }
package/src/source.ts ADDED
@@ -0,0 +1,49 @@
1
+ // The records of the core: a source and a cited unit of text.
2
+
3
+ import { LICENSE_RULES } from "./license.ts";
4
+ import type { License, Use } from "./license.ts";
5
+
6
+ /** A date as `YYYY-MM-DD`. */
7
+ export type IsoDate = string;
8
+
9
+ /** The `id` of a source. */
10
+ export type SourceId = string;
11
+
12
+ /** A page or a document, with its license and the date of the last check. The license `invented` is for test fixtures only. */
13
+ export interface Source {
14
+ readonly id: SourceId;
15
+ readonly title: string;
16
+ /** The address of the source, with `https:` or `http:`. */
17
+ readonly url: string;
18
+ readonly license: License;
19
+ /** The date when a person or a job last read the source and compared it with the units that cite it. */
20
+ readonly checked: IsoDate;
21
+ /** The name that a credit line gives, for example the agency or the authors. The default is the title. */
22
+ readonly by?: string;
23
+ }
24
+
25
+ /** The use of a cited unit. A bundle is a copy of a full source, so a unit cannot have the use `bundle`. */
26
+ export type UnitUse = Exclude<Use, "bundle">;
27
+
28
+ /**
29
+ * A cited unit: a quote (the text word for word), a summary (the words of the guidebook) or a fact (a value with a citation).
30
+ */
31
+ export interface CitedUnit {
32
+ readonly id: string;
33
+ readonly use: UnitUse;
34
+ readonly text: string;
35
+ readonly source: SourceId;
36
+ /** The place of the text in the source, for example a section or a page. */
37
+ readonly locator?: string;
38
+ }
39
+
40
+ /**
41
+ * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.
42
+ * A guidebook prints it under each quote and each bundle whose license needs a credit.
43
+ */
44
+ export function creditOf(source: Source): string {
45
+ const rule = LICENSE_RULES[source.license];
46
+ const name = source.by === undefined || source.by === source.title ? source.title : `${source.by}: ${source.title}`;
47
+ const license = rule.url === "" ? rule.title : `${rule.title} (${rule.url})`;
48
+ return `${name}. ${source.url}. ${license}. Checked ${source.checked}.`;
49
+ }