@mark1russell7/guidebook 0.5.0 → 0.5.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/README.md CHANGED
@@ -13,7 +13,7 @@ The core names no topic. Each topic is an add-on package with its own records. E
13
13
  | Record | What it holds |
14
14
  | --- | --- |
15
15
  | `Source` | A page or a document: its `id`, `title`, `url`, `license`, the date `checked` of the last read, and the optional name `by`. The optional `terms` is the page of the reuse terms, `updated` is the date of the last change that the source shows, and `sha256` is the hash of the file at the check. |
16
- | `CitedUnit` | A unit of text: its `id`, its `use`, its `text`, the `id` of its `source`, and an optional `locator` in the source |
16
+ | `CitedUnit` | A unit of text: its `id`, its `use`, its `text`, the `id` of its `source`, and an optional `locator` in the source. A quote can have the date `checked` of the last quote check that found it. |
17
17
 
18
18
  A unit has one of three uses:
19
19
 
@@ -66,7 +66,11 @@ The gate gives each finding as data, with a level, a code, the record and a mess
66
66
  | `missing-field` | `error` | A source has no text in a field of the option `require`. |
67
67
  | `stale` | `warning` | The check is older than `maxAgeDays` (365 by default) before `at`. |
68
68
  | `missing-source` | `error` | A unit cites a source that is not in the list. |
69
- | `use-refused` | `error` | The license of the source refuses the use of the unit. |
69
+ | `use-refused` | `error` | The license of the source refuses the use of the unit, or the use is not in `USES`. |
70
+
71
+ A unit with a date `checked` that is not a real date `YYYY-MM-DD` gets `bad-date` too. Since 0.5.2.
72
+
73
+ Since 0.5.1, the gate does not throw for a source or a unit from JSON with a bad shape. A field of a wrong type gives the finding of that field. For example, a title that is not a text gives `empty-title`. An item that is not a record has no fields, so it gives the finding of each necessary field.
70
74
 
71
75
  A dataset adds its own rules with two options of `checkSources` and `gate`:
72
76
 
@@ -79,8 +83,29 @@ checkSources(sources, { require: ["by", "terms"], https: true });
79
83
 
80
84
  `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.
81
85
 
86
+ ## The quote check
87
+
88
+ Since 0.5.2, `checkQuotes(units, texts)` compares each quote with the text of its source. The check is pure: a job reads the texts.
89
+
90
+ - The comparison uses `findPassage` of `@mark1russell7/text/passage`. The case, the quote marks, the dashes and the white space are not significant.
91
+ - A quote that the text does not hold gets the error `quote-not-found`.
92
+ - A source with no text gets the warning `quote-no-read` for each of its quotes. A job that cannot read a source thus does not fail.
93
+ - Each check gives its status: `found` with its `span` in the text, `not-found` or `no-read`.
94
+ - `markChecked(units, checks, at)` sets the date `checked` on each quote that the check found.
95
+
96
+ ```ts
97
+ import { checkQuotes, markChecked } from "@mark1russell7/guidebook";
98
+
99
+ const units = [{ id: "cool", use: "quote", text: "Store the water in a cool, dark place.", source: "store" }] as const;
100
+ const texts = new Map([["store", "Store the water\nin a cool, dark place."]]);
101
+
102
+ const result = checkQuotes(units, texts);
103
+ result.ok; // true
104
+ markChecked(units, result.checks, "2026-11-24")[0]?.checked; // "2026-11-24"
105
+ ```
106
+
82
107
  ## Limits
83
108
 
84
- - 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`.
109
+ - The quote check finds the words of a quote, not their meaning. A quote out of its context passes.
85
110
  - The table holds the licenses of the first sources. A new license goes into `LICENSES` and `LICENSE_RULES` in a pull request.
86
111
  - The rules are a reading of the license texts. They are not legal advice.
package/build/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { PassageSpan } from "@mark1russell7/text/passage";
1
2
  /** The licenses that the gate knows. The license `invented` is for test fixtures only. */
2
3
  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", "ogl-uk-3.0", "gutenberg", "all-rights-reserved", "link-only", "invented"];
3
4
  /** A license of a source. The source gate refuses a use that the license does not permit. */
@@ -94,6 +95,8 @@ interface CitedUnit {
94
95
  readonly source: SourceId;
95
96
  /** The place of the text in the source, for example a section or a page. */
96
97
  readonly locator?: string;
98
+ /** The date of the last quote check that found the text in the source. Only a quote has it. `markChecked` sets it. */
99
+ readonly checked?: IsoDate;
97
100
  }
98
101
  /**
99
102
  * This function gives the credit line of a source: its name, its title, its address, its license and the date of the check.
@@ -158,9 +161,13 @@ export declare function isSha256(text: string): boolean;
158
161
  * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.
159
162
  * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.
160
163
  * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.
164
+ * A source from JSON with a bad shape gives findings and not an error. A bad shape is a field of a wrong type, or an item that is not a record.
161
165
  */
162
166
  export declare function checkSources(sources: readonly Source[], options?: GateOptions): readonly Finding[];
163
- /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
167
+ /**
168
+ * This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses.
169
+ * A unit or a source from JSON with a bad shape gives findings and not an error.
170
+ */
164
171
  export declare function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options?: UseContext): readonly Finding[];
165
172
  /** This function applies the gate to the sources and the units of a guidebook. */
166
173
  export declare function gate(units: readonly CitedUnit[], sources: readonly Source[], options?: GateOptions): GateResult;
@@ -168,5 +175,53 @@ export declare function gate(units: readonly CitedUnit[], sources: readonly Sour
168
175
  export declare function assertGate(units: readonly CitedUnit[], sources: readonly Source[], options?: GateOptions): void;
169
176
  /** This function gives the layer of a printed unit. Only a quote of a share-alike source goes to the share-alike layer. */
170
177
  export declare function layerOf(unit: CitedUnit, source: Source): Layer;
171
- export type { CitedUnit, Finding, FindingCode, GateOptions, GateResult, IsoDate, Layer, License, LicenseRule, Source, SourceField, SourceId, UnitUse, Use, UseContext };
178
+ /** The codes of the findings of the quote check. */
179
+ export declare const QUOTE_CODES: readonly ["quote-not-found", "quote-no-read"];
180
+ /** A code of a finding of the quote check. */
181
+ type QuoteCode = (typeof QUOTE_CODES)[number];
182
+ /** A finding of the quote check. */
183
+ type QuoteFinding = Finding<QuoteCode, "unit">;
184
+ /**
185
+ * The result of the check of one quote.
186
+ *
187
+ * - `found`: the text of the source holds the quote. `span` is its place in that text.
188
+ * - `not-found`: the text of the source does not hold the quote.
189
+ * - `no-read`: the check has no text of the source, so it cannot tell.
190
+ */
191
+ interface QuoteCheck {
192
+ /** The `id` of the unit. */
193
+ readonly unit: string;
194
+ readonly source: SourceId;
195
+ readonly status: "found" | "not-found" | "no-read";
196
+ /** The place of the quote in the text of the source. Only the status `found` has it. */
197
+ readonly span?: PassageSpan;
198
+ }
199
+ /** The result of the quote check: the findings, and the check of each quote. */
200
+ interface QuoteResult extends GateResult<QuoteFinding> {
201
+ readonly checks: readonly QuoteCheck[];
202
+ }
203
+ /**
204
+ * This function compares each unit of the use `quote` with the text of its source. It has no I/O.
205
+ *
206
+ * - The comparison is `findPassage` of `@mark1russell7/text`. The case, the quote marks, the dashes and the white space are not significant.
207
+ * - A quote that the text does not hold gets the error `quote-not-found`.
208
+ * - A source with no text, or with only white space, gives the warning `quote-no-read` for each of its quotes.
209
+ * - A unit of another use, or a unit from JSON with a bad shape, gets no check. The source gate examines its shape.
210
+ *
211
+ * @param units - The units of a guidebook. The function examines only the quotes.
212
+ * @param texts - The plain text of each source that a job read, by the `id` of the source.
213
+ * @returns The findings and the check of each quote, in the order of the units.
214
+ */
215
+ export declare function checkQuotes(units: readonly CitedUnit[], texts: ReadonlyMap<SourceId, string>): QuoteResult;
216
+ /**
217
+ * This function sets the date `checked` on each unit whose quote the check found. The other units do not change.
218
+ * It throws a `RangeError` when the date is not a real date `YYYY-MM-DD`.
219
+ *
220
+ * @param units - The units of a guidebook.
221
+ * @param checks - The checks of `checkQuotes`.
222
+ * @param at - The date of the check.
223
+ * @returns The units, in the same order.
224
+ */
225
+ export declare function markChecked<U extends CitedUnit>(units: readonly U[], checks: readonly QuoteCheck[], at: IsoDate): readonly U[];
226
+ export type { CitedUnit, Finding, FindingCode, GateOptions, GateResult, IsoDate, Layer, License, LicenseRule, QuoteCheck, QuoteCode, QuoteFinding, QuoteResult, Source, SourceField, SourceId, UnitUse, Use, UseContext };
172
227
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts"],"mappings":";qBAIa;;KAgBD,kBAAkB;;qBAGjB;;;;;;KAOD,cAAc;;KAGd;;UAGK;;WAEN;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA,OAAO;;WAEP;;WAEA;;;;;;;;;;;;;qBAcE,eAAe,SAAS,OAAO,SAAS;;UAgGpC;;WAEN;;WAEA;;;;;;;wBAQK,QAAQ,SAAS,SAAS,KAAK,KAAK,UAAS;;KC1KjD;;KAGA;;UAGK;WACN,IAAI;WACJ;;WAEA;WACA,SAAS;;WAET,SAAS;;WAET;;WAEA;;WAEA;;WAEA,UAAU;;WAEV;;;KAIC,UAAU,QAAQ;;;;UAKb;WACN;WACA,KAAK;WACL;WACA,QAAQ;;WAER;;;;;;wBAOK,SAAS,QAAQ;;qBC3CpB;;KAiBD,sBAAsB;;;;;UAMjB,QAAQ,mBAAmB,aAAa;WAC9C;WACA,MAAM;;WAEN,QAAQ;;WAER;WACA;;;qBAIE;;KAGD,sBAAsB;;UAGjB,oBAAoB;;WAE1B,KAAK;;WAEL;;;;;WAKA,mBAAmB;;WAEnB;;;UAIM,WAAW,UAAU,0BAA0B;;WAErD;WACA,mBAAmB;;;wBAId,SAAS,UAAU,yBAAyB,mBAAmB,MAAM,WAAW;;wBAKhF,cAAc,cAAc,mBAAmB;;qBAMlD;;wBAIG,OAAO;;wBAeP,aAAa;;wBAab,SAAS;;;;;;;wBAUT,aAAa,kBAAkB,UAAU,UAAS,uBAA4B;;wBA8C9E,WAAW,gBAAgB,aAAa,kBAAkB,UAAU,UAAS,sBAA2B;;wBAwBxG,KAAK,gBAAgB,aAAa,kBAAkB,UAAU,UAAS,cAAmB;;wBAK1F,WAAW,gBAAgB,aAAa,kBAAkB,UAAU,UAAS;;wBAQ7E,QAA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AQ,SAAS;;qBC5N7C;;KAGD,oBAAoB;;KAGpB,eAAe,QAAQ;;;;;;;;UASlB;;WAEN;WACA,QAAQ;WACR;;WAEA,OAAO;;;UAID,oBAAoB,WAAW;WACrC,iBAAiB;;;;;;;;;;;;;;wBAeZ,YAAY,gBAAgB,aAAa,OAAO,YAAY,oBAAoB;;;;;;;;;;wBAkChF,YAAY,UAAU,WAAW,gBAAgB,KAAK,iBAAiB,cAAc,IAAI,mBAAmB"}
package/build/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { findPassage } from "@mark1russell7/text/passage";
1
2
  // src/license.ts
2
3
  /** The licenses that the gate knows. The license `invented` is for test fixtures only. */
3
4
  const LICENSES = [
@@ -236,6 +237,16 @@ function timeOf(date) {
236
237
  if (Number.isNaN(time)) return void 0;
237
238
  return new Date(time).toISOString().slice(0, 10) === date ? time : void 0;
238
239
  }
240
+ /**
241
+ * This function gives the fields of an item from JSON. Each field can have any type or be absent.
242
+ * `Object` boxes a primitive and gives an empty object for null, so a read of a field does not throw.
243
+ */
244
+ function fields(raw) {
245
+ return Object(raw);
246
+ }
247
+ function isUse(value) {
248
+ return typeof value === "string" && USES.includes(value);
249
+ }
239
250
  function isLicense(value) {
240
251
  return typeof value === "string" && LICENSES.includes(value);
241
252
  }
@@ -260,6 +271,7 @@ function isSha256(text) {
260
271
  * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.
261
272
  * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.
262
273
  * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.
274
+ * A source from JSON with a bad shape gives findings and not an error. A bad shape is a field of a wrong type, or an item that is not a record.
263
275
  */
264
276
  function checkSources(sources, options = {}) {
265
277
  const out = [];
@@ -279,40 +291,45 @@ function checkSources(sources, options = {}) {
279
291
  if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(", ")}.`);
280
292
  const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);
281
293
  const seen = /* @__PURE__ */ new Set();
282
- for (const source of sources) {
283
- if (seen.has(source.id)) error("duplicate-id", source.id, `Two sources have the id "${source.id}".`);
284
- seen.add(source.id);
285
- if (source.title.trim() === "") error("empty-title", source.id, `The source "${source.id}" has no title.`);
286
- 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:.`);
287
- else if (options.https === true && !isSecure(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". The gate accepts only an address with https:.`);
288
- if (!isLicense(source.license)) error("unknown-license", source.id, `The source "${source.id}" has the unknown license "${String(source.license)}".`);
294
+ for (const raw of sources) {
295
+ const source = fields(raw);
296
+ const id = String(source.id);
297
+ if (seen.has(id)) error("duplicate-id", id, `Two sources have the id "${id}".`);
298
+ seen.add(id);
299
+ if (typeof source.title !== "string" || source.title.trim() === "") error("empty-title", id, `The source "${id}" has no title.`);
300
+ if (typeof source.url !== "string" || !isWebAddress(source.url)) error("bad-url", id, `The source "${id}" has the address "${String(source.url)}". It needs an address with https: or http:.`);
301
+ else if (options.https === true && !isSecure(source.url)) error("bad-url", id, `The source "${id}" has the address "${String(source.url)}". The gate accepts only an address with https:.`);
302
+ if (!isLicense(source.license)) error("unknown-license", id, `The source "${id}" has the unknown license "${String(source.license)}".`);
289
303
  for (const field of necessary) {
290
304
  const value = source[field];
291
- if (typeof value !== "string" || value.trim() === "") error("missing-field", source.id, `The source "${source.id}" has no field "${field}". The gate makes it necessary.`);
305
+ if (typeof value !== "string" || value.trim() === "") error("missing-field", id, `The source "${id}" has no field "${field}". The gate makes it necessary.`);
292
306
  }
293
- if (source.terms !== void 0 && !isWebAddress(source.terms)) error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". It needs an address with https: or http:.`);
294
- else if (source.terms !== void 0 && options.https === true && !isSecure(source.terms)) error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". The gate accepts only an address with https:.`);
295
- if (source.sha256 !== void 0 && !isSha256(source.sha256)) error("bad-sha256", source.id, `The hash of the source "${source.id}" is not 64 lowercase hexadecimal digits.`);
296
- const checked = timeOf(source.checked);
297
- const updated = source.updated === void 0 ? void 0 : timeOf(source.updated);
298
- if (source.updated !== void 0 && updated === void 0) error("bad-date", source.id, `The source "${source.id}" has the change date "${source.updated}". It needs a date YYYY-MM-DD.`);
299
- if (updated !== void 0 && checked !== void 0 && updated > checked) error("updated-after-check", source.id, `The source "${source.id}" has a change date after its check date.`);
300
- 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.`);
307
+ if (source.terms !== void 0 && (typeof source.terms !== "string" || !isWebAddress(source.terms))) error("bad-url", id, `The source "${id}" has the terms address "${String(source.terms)}". It needs an address with https: or http:.`);
308
+ else if (source.terms !== void 0 && options.https === true && !isSecure(source.terms)) error("bad-url", id, `The source "${id}" has the terms address "${String(source.terms)}". The gate accepts only an address with https:.`);
309
+ if (source.sha256 !== void 0 && (typeof source.sha256 !== "string" || !isSha256(source.sha256))) error("bad-sha256", id, `The hash of the source "${id}" is not 64 lowercase hexadecimal digits.`);
310
+ const checked = typeof source.checked === "string" ? timeOf(source.checked) : void 0;
311
+ const updated = typeof source.updated === "string" ? timeOf(source.updated) : void 0;
312
+ if (source.updated !== void 0 && updated === void 0) error("bad-date", id, `The source "${id}" has the change date "${String(source.updated)}". It needs a date YYYY-MM-DD.`);
313
+ if (updated !== void 0 && checked !== void 0 && updated > checked) error("updated-after-check", id, `The source "${id}" has a change date after its check date.`);
314
+ if (checked === void 0) error("bad-date", id, `The source "${id}" has the check date "${String(source.checked)}". It needs a date YYYY-MM-DD.`);
301
315
  else if (at !== void 0) {
302
- if (checked > at) error("checked-in-future", source.id, `The source "${source.id}" has a check date after the date of the gate.`);
316
+ if (checked > at) error("checked-in-future", id, `The source "${id}" has a check date after the date of the gate.`);
303
317
  const days = (at - checked) / DAY_MS;
304
318
  if (days > maxAge) out.push({
305
319
  level: "warning",
306
320
  code: "stale",
307
321
  record: "source",
308
- id: source.id,
309
- message: `The last check of the source "${source.id}" is ${days} days old. Read the source again.`
322
+ id,
323
+ message: `The last check of the source "${id}" is ${days} days old. Read the source again.`
310
324
  });
311
325
  }
312
326
  }
313
327
  return out;
314
328
  }
315
- /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
329
+ /**
330
+ * This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses.
331
+ * A unit or a source from JSON with a bad shape gives findings and not an error.
332
+ */
316
333
  function checkUnits(units, sources, options = {}) {
317
334
  const out = [];
318
335
  const error = (code, id, message) => {
@@ -324,18 +341,22 @@ function checkUnits(units, sources, options = {}) {
324
341
  message
325
342
  });
326
343
  };
327
- const byId = new Map(sources.map((source) => [source.id, source]));
344
+ const byId = new Map(sources.map((raw) => fields(raw)).map((source) => [source.id, source]));
328
345
  const seen = /* @__PURE__ */ new Set();
329
- for (const unit of units) {
330
- if (seen.has(unit.id)) error("duplicate-id", unit.id, `Two units have the id "${unit.id}".`);
331
- seen.add(unit.id);
332
- if (unit.text.trim() === "") error("empty-text", unit.id, `The unit "${unit.id}" has no text.`);
346
+ for (const raw of units) {
347
+ const unit = fields(raw);
348
+ const id = String(unit.id);
349
+ if (seen.has(id)) error("duplicate-id", id, `Two units have the id "${id}".`);
350
+ seen.add(id);
351
+ if (typeof unit.text !== "string" || unit.text.trim() === "") error("empty-text", id, `The unit "${id}" has no text.`);
352
+ if (unit.checked !== void 0 && (typeof unit.checked !== "string" || timeOf(unit.checked) === void 0)) error("bad-date", id, `The unit "${id}" has the check date "${String(unit.checked)}". It needs a date YYYY-MM-DD.`);
333
353
  const source = byId.get(unit.source);
334
354
  if (source === void 0) {
335
- error("missing-source", unit.id, `The unit "${unit.id}" cites the source "${unit.source}", but the list does not have it.`);
355
+ error("missing-source", id, `The unit "${id}" cites the source "${String(unit.source)}", but the list does not have it.`);
336
356
  continue;
337
357
  }
338
- 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}".`);
358
+ if (!isUse(unit.use)) error("use-refused", id, `The unit "${id}" has the unknown use "${String(unit.use)}". A use is ${USES.join(", ")}.`);
359
+ else if (isLicense(source.license) && !permits(source.license, unit.use, options)) error("use-refused", id, `The license "${source.license}" of the source "${String(source.id)}" refuses the use "${unit.use}" of the unit "${id}".`);
339
360
  }
340
361
  return out;
341
362
  }
@@ -354,6 +375,90 @@ function assertGate(units, sources, options = {}) {
354
375
  function layerOf(unit, source) {
355
376
  return unit.use === "quote" ? LICENSE_RULES[source.license].layer : "main";
356
377
  }
357
- export { DAY_MS, FINDING_CODES, LICENSES, LICENSE_RULES, SOURCE_FIELDS, USES, assertGate, checkSources, checkUnits, creditOf, gate, isSha256, isWebAddress, layerOf, permits, resultOf, throwOnErrors, timeOf };
378
+ // src/quote.ts
379
+ /** The codes of the findings of the quote check. */
380
+ const QUOTE_CODES = ["quote-not-found", "quote-no-read"];
381
+ /**
382
+ * This function compares each unit of the use `quote` with the text of its source. It has no I/O.
383
+ *
384
+ * - The comparison is `findPassage` of `@mark1russell7/text`. The case, the quote marks, the dashes and the white space are not significant.
385
+ * - A quote that the text does not hold gets the error `quote-not-found`.
386
+ * - A source with no text, or with only white space, gives the warning `quote-no-read` for each of its quotes.
387
+ * - A unit of another use, or a unit from JSON with a bad shape, gets no check. The source gate examines its shape.
388
+ *
389
+ * @param units - The units of a guidebook. The function examines only the quotes.
390
+ * @param texts - The plain text of each source that a job read, by the `id` of the source.
391
+ * @returns The findings and the check of each quote, in the order of the units.
392
+ */
393
+ function checkQuotes(units, texts) {
394
+ const findings = [];
395
+ const checks = [];
396
+ for (const raw of units) {
397
+ const unit = Object(raw);
398
+ if (unit.use !== "quote" || typeof unit.text !== "string" || typeof unit.source !== "string") continue;
399
+ const id = String(unit.id);
400
+ const source = unit.source;
401
+ const text = texts.get(source);
402
+ if (text === void 0 || text.trim() === "") {
403
+ checks.push({
404
+ unit: id,
405
+ source,
406
+ status: "no-read"
407
+ });
408
+ findings.push({
409
+ level: "warning",
410
+ code: "quote-no-read",
411
+ record: "unit",
412
+ id,
413
+ message: `The check has no text of the source "${source}", so it cannot examine the quote "${id}".`
414
+ });
415
+ continue;
416
+ }
417
+ const span = findPassage(text, unit.text);
418
+ if (span === void 0) {
419
+ checks.push({
420
+ unit: id,
421
+ source,
422
+ status: "not-found"
423
+ });
424
+ findings.push({
425
+ level: "error",
426
+ code: "quote-not-found",
427
+ record: "unit",
428
+ id,
429
+ message: `The text of the source "${source}" does not hold the quote "${id}".`
430
+ });
431
+ continue;
432
+ }
433
+ checks.push({
434
+ unit: id,
435
+ source,
436
+ status: "found",
437
+ span
438
+ });
439
+ }
440
+ return {
441
+ ...resultOf(findings),
442
+ checks
443
+ };
444
+ }
445
+ /**
446
+ * This function sets the date `checked` on each unit whose quote the check found. The other units do not change.
447
+ * It throws a `RangeError` when the date is not a real date `YYYY-MM-DD`.
448
+ *
449
+ * @param units - The units of a guidebook.
450
+ * @param checks - The checks of `checkQuotes`.
451
+ * @param at - The date of the check.
452
+ * @returns The units, in the same order.
453
+ */
454
+ function markChecked(units, checks, at) {
455
+ if (timeOf(at) === void 0) throw new RangeError(`markChecked: the date "${at}" is not a date YYYY-MM-DD.`);
456
+ const found = new Set(checks.filter((check) => check.status === "found").map((check) => check.unit));
457
+ return units.map((unit) => unit.use === "quote" && found.has(unit.id) ? {
458
+ ...unit,
459
+ checked: at
460
+ } : unit);
461
+ }
462
+ export { DAY_MS, FINDING_CODES, LICENSES, LICENSE_RULES, QUOTE_CODES, SOURCE_FIELDS, USES, assertGate, checkQuotes, checkSources, checkUnits, creditOf, gate, isSha256, isWebAddress, layerOf, markChecked, permits, resultOf, throwOnErrors, timeOf };
358
463
 
359
464
  //# sourceMappingURL=index.js.map
@@ -1 +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 \"ogl-uk-3.0\",\n \"gutenberg\",\n \"all-rights-reserved\",\n \"link-only\",\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 a guidebook can write a summary or a fact of the source. It is false when a guidebook can only link to the source. */\n readonly units: 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 * - The Project Gutenberg License covers works that are in the US public domain. A copy keeps the full license and the name.\n * A copy for a fee pays the royalty of the trademark license, or it removes each reference to Project Gutenberg.\n * - A work with all rights reserved permits only a summary and a fact, with a citation.\n * - A link-only work permits no use of its text. A guidebook can only link to it, for example when its publisher refuses each AI use.\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 units: 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, units: 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 units: 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 units: 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 units: 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 units: 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 units: true,\n nonCommercial: true,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"ogl-uk-3.0\": {\n title: \"Open Government Licence v3.0\",\n url: \"https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n gutenberg: {\n title: \"Project Gutenberg License\",\n url: \"https://www.gutenberg.org/policy/license.html\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"all-rights-reserved\": { title: \"All rights reserved\", url: \"\", quote: false, bundle: false, units: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n \"link-only\": { title: \"Link only\", url: \"\", quote: false, bundle: false, units: false, nonCommercial: false, layer: \"main\", credit: false, real: true },\n invented: { title: \"Invented for a test\", url: \"\", quote: false, bundle: false, units: true, 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, but a link-only license refuses 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 rule.units;\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 /** A term of the publisher in addition to the license, with its date, for example \"No AI ingestion (publisher, 2026-11-23)\". */\n readonly note?: string;\n /** The page of the publisher that tells the terms of the reuse, with `https:` or `http:`. */\n readonly terms?: string | undefined;\n /** The date that the source shows as its last change. It is not after the date `checked`. */\n readonly updated?: IsoDate | undefined;\n /** The SHA-256 hash of the file of the source at the date `checked`, as 64 lowercase hexadecimal digits. */\n readonly sha256?: string | undefined;\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 \"updated-after-check\",\n \"bad-sha256\",\n \"missing-field\",\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 optional fields of a source, in the order of the findings of the option `require`. */\nexport const SOURCE_FIELDS: readonly [\"by\", \"note\", \"terms\", \"updated\", \"sha256\"] = [\"by\", \"note\", \"terms\", \"updated\", \"sha256\"];\n\n/** An optional field of a source that the option `require` of the gate can make necessary. */\nexport type SourceField = (typeof SOURCE_FIELDS)[number];\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 * The optional fields that each source must have. A field with no text gets the error `missing-field`.\n * A dataset uses it for its own rules, for example `[\"by\", \"terms\"]` for the credit and the evidence of a license.\n */\n readonly require?: readonly SourceField[];\n /** When this option is true, the address and the terms address of each source must start with `https:`. The default is false. */\n readonly https?: boolean;\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/** The form of a SHA-256 hash: 64 lowercase hexadecimal digits. */\nconst SHA256 = /^[0-9a-f]{64}$/u;\n\n/** This function tells if a web address starts with `https:`, in any case of the letters. */\nfunction isSecure(url: string): boolean {\n return url.slice(0, 8).toLowerCase() === \"https://\";\n}\n\n/** This function tells if a text is a SHA-256 hash in the form that the gate accepts: 64 lowercase hexadecimal digits. */\nexport function isSha256(text: string): boolean {\n return SHA256.test(text);\n}\n\n/**\n * This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks.\n * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.\n * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.\n * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.\n */\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 unknown = (options.require ?? []).filter((field) => !(SOURCE_FIELDS as readonly string[]).includes(field));\n if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(\", \")}.`);\n const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);\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 else if (options.https === true && !isSecure(source.url)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the address \"${source.url}\". The gate accepts only an address with https:.`);\n if (!isLicense(source.license)) error(\"unknown-license\", source.id, `The source \"${source.id}\" has the unknown license \"${String(source.license)}\".`);\n for (const field of necessary) {\n const value: unknown = source[field];\n if (typeof value !== \"string\" || value.trim() === \"\") error(\"missing-field\", source.id, `The source \"${source.id}\" has no field \"${field}\". The gate makes it necessary.`);\n }\n if (source.terms !== undefined && !isWebAddress(source.terms)) error(\"bad-url\", source.id, `The source \"${source.id}\" has the terms address \"${source.terms}\". It needs an address with https: or http:.`);\n else if (source.terms !== undefined && options.https === true && !isSecure(source.terms)) {\n error(\"bad-url\", source.id, `The source \"${source.id}\" has the terms address \"${source.terms}\". The gate accepts only an address with https:.`);\n }\n if (source.sha256 !== undefined && !isSha256(source.sha256)) error(\"bad-sha256\", source.id, `The hash of the source \"${source.id}\" is not 64 lowercase hexadecimal digits.`);\n const checked = timeOf(source.checked);\n const updated = source.updated === undefined ? undefined : timeOf(source.updated);\n if (source.updated !== undefined && updated === undefined) error(\"bad-date\", source.id, `The source \"${source.id}\" has the change date \"${source.updated}\". It needs a date YYYY-MM-DD.`);\n if (updated !== undefined && checked !== undefined && updated > checked) error(\"updated-after-check\", source.id, `The source \"${source.id}\" has a change date after its check date.`);\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":";;AAIA,MAAa,WAAW;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAMA,MAAa,OAAO;CAAC;CAAS;CAAW;CAAQ;AAAQ;;;;;;;;;;;;AA6CzD,MAAa,gBAAwD;CACnE,oBAAoB;EAClB,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EACR,OAAO;EACP,eAAe;EACf,OAAO;EACP,QAAQ;EACR,MAAM;CACR;CACA,iBAAiB;EAAE,OAAO;EAAiB,KAAK;EAAI,OAAO;EAAM,QAAQ;EAAM,OAAO;EAAM,eAAe;EAAO,OAAO;EAAQ,QAAQ;EAAO,MAAM;CAAK;CAC3J,KAAK;EACH,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EACR,OAAO;EACP,eAAe;EACf,OAAO;EACP,QAAQ;EACR,MAAM;CACR;CACA,aAAa;EACX,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EAC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,SAAS,KAAK,IAAI,KAAK,MAAM;EACnC,IAAI,WAAW,KAAA,GAAW;GACxB,MAAM,kBAAkB,KAAK,IAAI,aAAa,KAAK,GAAG,sBAAsB,KAAK,OAAO,kCAAkC;GAC1H;EACF;EACA,IAAI,UAAU,OAAO,OAAO,KAAK,CAAC,QAAQ,OAAO,SAAS,KAAK,KAAK,OAAO,GACzE,MAAM,eAAe,KAAK,IAAI,gBAAgB,OAAO,QAAQ,mBAAmB,OAAO,GAAG,qBAAqB,KAAK,IAAI,iBAAiB,KAAK,GAAG,GAAG;CAExJ;CACA,OAAO;AACT;;AAGA,SAAgB,KAAK,OAA6B,SAA4B,UAAuB,CAAC,GAAe;CACnH,OAAO,SAAS,CAAC,GAAG,aAAa,SAAS,OAAO,GAAG,GAAG,WAAW,OAAO,SAAS,OAAO,CAAC,CAAC;AAC7F;;AAGA,SAAgB,WAAW,OAA6B,SAA4B,UAAuB,CAAC,GAAS;CACnH,MAAM,SAAS,KAAK,OAAO,SAAS,OAAO;CAC3C,IAAI,OAAO,IAAI;CACf,MAAM,QAAQ,OAAO,SAAS,QAAQ,YAAY,QAAQ,UAAU,OAAO,CAAC,CAAC,KAAK,YAAY,KAAK,QAAQ,KAAK,IAAI,QAAQ,SAAS;CACrI,MAAM,IAAI,MAAM,yBAAyB,MAAM,OAAO,YAAY,MAAM,KAAK,IAAI,GAAG;AACtF;;AAGA,SAAgB,QAAQ,MAAiB,QAAuB;CAC9D,OAAO,KAAK,QAAQ,UAAU,cAAc,OAAO,QAAQ,CAAC,QAAQ;AACtE"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/license.ts","../src/source.ts","../src/gate.ts","../src/quote.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 \"ogl-uk-3.0\",\n \"gutenberg\",\n \"all-rights-reserved\",\n \"link-only\",\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 a guidebook can write a summary or a fact of the source. It is false when a guidebook can only link to the source. */\n readonly units: 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 * - The Project Gutenberg License covers works that are in the US public domain. A copy keeps the full license and the name.\n * A copy for a fee pays the royalty of the trademark license, or it removes each reference to Project Gutenberg.\n * - A work with all rights reserved permits only a summary and a fact, with a citation.\n * - A link-only work permits no use of its text. A guidebook can only link to it, for example when its publisher refuses each AI use.\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 units: 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, units: 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 units: 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 units: 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 units: 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 units: 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 units: true,\n nonCommercial: true,\n layer: \"share-alike\",\n credit: true,\n real: true,\n },\n \"ogl-uk-3.0\": {\n title: \"Open Government Licence v3.0\",\n url: \"https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n gutenberg: {\n title: \"Project Gutenberg License\",\n url: \"https://www.gutenberg.org/policy/license.html\",\n quote: true,\n bundle: true,\n units: true,\n nonCommercial: false,\n layer: \"main\",\n credit: true,\n real: true,\n },\n \"all-rights-reserved\": { title: \"All rights reserved\", url: \"\", quote: false, bundle: false, units: true, nonCommercial: false, layer: \"main\", credit: false, real: true },\n \"link-only\": { title: \"Link only\", url: \"\", quote: false, bundle: false, units: false, nonCommercial: false, layer: \"main\", credit: false, real: true },\n invented: { title: \"Invented for a test\", url: \"\", quote: false, bundle: false, units: true, 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, but a link-only license refuses 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 rule.units;\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 /** A term of the publisher in addition to the license, with its date, for example \"No AI ingestion (publisher, 2026-11-23)\". */\n readonly note?: string;\n /** The page of the publisher that tells the terms of the reuse, with `https:` or `http:`. */\n readonly terms?: string | undefined;\n /** The date that the source shows as its last change. It is not after the date `checked`. */\n readonly updated?: IsoDate | undefined;\n /** The SHA-256 hash of the file of the source at the date `checked`, as 64 lowercase hexadecimal digits. */\n readonly sha256?: string | undefined;\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 /** The date of the last quote check that found the text in the source. Only a quote has it. `markChecked` sets it. */\n readonly checked?: IsoDate;\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, USES } from \"./license.ts\";\nimport type { Layer, License, Use, UseContext } from \"./license.ts\";\nimport type { CitedUnit, IsoDate, Source } 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 \"updated-after-check\",\n \"bad-sha256\",\n \"missing-field\",\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 optional fields of a source, in the order of the findings of the option `require`. */\nexport const SOURCE_FIELDS: readonly [\"by\", \"note\", \"terms\", \"updated\", \"sha256\"] = [\"by\", \"note\", \"terms\", \"updated\", \"sha256\"];\n\n/** An optional field of a source that the option `require` of the gate can make necessary. */\nexport type SourceField = (typeof SOURCE_FIELDS)[number];\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 * The optional fields that each source must have. A field with no text gets the error `missing-field`.\n * A dataset uses it for its own rules, for example `[\"by\", \"terms\"]` for the credit and the evidence of a license.\n */\n readonly require?: readonly SourceField[];\n /** When this option is true, the address and the terms address of each source must start with `https:`. The default is false. */\n readonly https?: boolean;\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\n/**\n * This function gives the fields of an item from JSON. Each field can have any type or be absent.\n * `Object` boxes a primitive and gives an empty object for null, so a read of a field does not throw.\n */\nfunction fields<T>(raw: unknown): { readonly [K in keyof T]?: unknown } {\n return Object(raw) as { readonly [K in keyof T]?: unknown };\n}\n\nfunction isUse(value: unknown): value is Use {\n return typeof value === \"string\" && (USES as readonly string[]).includes(value);\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/** The form of a SHA-256 hash: 64 lowercase hexadecimal digits. */\nconst SHA256 = /^[0-9a-f]{64}$/u;\n\n/** This function tells if a web address starts with `https:`, in any case of the letters. */\nfunction isSecure(url: string): boolean {\n return url.slice(0, 8).toLowerCase() === \"https://\";\n}\n\n/** This function tells if a text is a SHA-256 hash in the form that the gate accepts: 64 lowercase hexadecimal digits. */\nexport function isSha256(text: string): boolean {\n return SHA256.test(text);\n}\n\n/**\n * This function gives the findings of the sources: duplicate ids, empty titles, bad addresses, bad dates, unknown licenses and old checks.\n * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.\n * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.\n * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.\n * A source from JSON with a bad shape gives findings and not an error. A bad shape is a field of a wrong type, or an item that is not a record.\n */\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 unknown = (options.require ?? []).filter((field) => !(SOURCE_FIELDS as readonly string[]).includes(field));\n if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(\", \")}.`);\n const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);\n const seen = new Set<string>();\n for (const raw of sources) {\n const source = fields<Source>(raw);\n const id = String(source.id);\n if (seen.has(id)) error(\"duplicate-id\", id, `Two sources have the id \"${id}\".`);\n seen.add(id);\n if (typeof source.title !== \"string\" || source.title.trim() === \"\") error(\"empty-title\", id, `The source \"${id}\" has no title.`);\n if (typeof source.url !== \"string\" || !isWebAddress(source.url)) error(\"bad-url\", id, `The source \"${id}\" has the address \"${String(source.url)}\". It needs an address with https: or http:.`);\n else if (options.https === true && !isSecure(source.url)) error(\"bad-url\", id, `The source \"${id}\" has the address \"${String(source.url)}\". The gate accepts only an address with https:.`);\n if (!isLicense(source.license)) error(\"unknown-license\", id, `The source \"${id}\" has the unknown license \"${String(source.license)}\".`);\n for (const field of necessary) {\n const value: unknown = source[field];\n if (typeof value !== \"string\" || value.trim() === \"\") error(\"missing-field\", id, `The source \"${id}\" has no field \"${field}\". The gate makes it necessary.`);\n }\n if (source.terms !== undefined && (typeof source.terms !== \"string\" || !isWebAddress(source.terms))) error(\"bad-url\", id, `The source \"${id}\" has the terms address \"${String(source.terms)}\". It needs an address with https: or http:.`);\n else if (source.terms !== undefined && options.https === true && !isSecure(source.terms)) {\n error(\"bad-url\", id, `The source \"${id}\" has the terms address \"${String(source.terms)}\". The gate accepts only an address with https:.`);\n }\n if (source.sha256 !== undefined && (typeof source.sha256 !== \"string\" || !isSha256(source.sha256))) error(\"bad-sha256\", id, `The hash of the source \"${id}\" is not 64 lowercase hexadecimal digits.`);\n const checked = typeof source.checked === \"string\" ? timeOf(source.checked) : undefined;\n const updated = typeof source.updated === \"string\" ? timeOf(source.updated) : undefined;\n if (source.updated !== undefined && updated === undefined) error(\"bad-date\", id, `The source \"${id}\" has the change date \"${String(source.updated)}\". It needs a date YYYY-MM-DD.`);\n if (updated !== undefined && checked !== undefined && updated > checked) error(\"updated-after-check\", id, `The source \"${id}\" has a change date after its check date.`);\n if (checked === undefined) {\n error(\"bad-date\", id, `The source \"${id}\" has the check date \"${String(source.checked)}\". It needs a date YYYY-MM-DD.`);\n } else if (at !== undefined) {\n if (checked > at) error(\"checked-in-future\", id, `The 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: id, message: `The last check of the source \"${id}\" is ${days} days old. Read the source again.` });\n }\n }\n }\n return out;\n}\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.\n * A unit or a source from JSON with a bad shape gives findings and not an error.\n */\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((raw) => fields<Source>(raw)).map((source) => [source.id, source] as const));\n const seen = new Set<string>();\n for (const raw of units) {\n const unit = fields<CitedUnit>(raw);\n const id = String(unit.id);\n if (seen.has(id)) error(\"duplicate-id\", id, `Two units have the id \"${id}\".`);\n seen.add(id);\n if (typeof unit.text !== \"string\" || unit.text.trim() === \"\") error(\"empty-text\", id, `The unit \"${id}\" has no text.`);\n if (unit.checked !== undefined && (typeof unit.checked !== \"string\" || timeOf(unit.checked) === undefined)) {\n error(\"bad-date\", id, `The unit \"${id}\" has the check date \"${String(unit.checked)}\". It needs a date YYYY-MM-DD.`);\n }\n const source = byId.get(unit.source);\n if (source === undefined) {\n error(\"missing-source\", id, `The unit \"${id}\" cites the source \"${String(unit.source)}\", but the list does not have it.`);\n continue;\n }\n if (!isUse(unit.use)) error(\"use-refused\", id, `The unit \"${id}\" has the unknown use \"${String(unit.use)}\". A use is ${USES.join(\", \")}.`);\n else if (isLicense(source.license) && !permits(source.license, unit.use, options)) {\n error(\"use-refused\", id, `The license \"${source.license}\" of the source \"${String(source.id)}\" refuses the use \"${unit.use}\" of the 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","// The quote check: each unit of the use `quote` against the text of its source.\n// The check is pure. A job reads the text of each source, gives the texts to `checkQuotes`, and sets the dates with `markChecked`.\n\nimport { findPassage } from \"@mark1russell7/text/passage\";\nimport type { PassageSpan } from \"@mark1russell7/text/passage\";\nimport { resultOf, timeOf } from \"./gate.ts\";\nimport type { Finding, GateResult } from \"./gate.ts\";\nimport type { CitedUnit, IsoDate, SourceId } from \"./source.ts\";\n\n/** The codes of the findings of the quote check. */\nexport const QUOTE_CODES = [\"quote-not-found\", \"quote-no-read\"] as const;\n\n/** A code of a finding of the quote check. */\nexport type QuoteCode = (typeof QUOTE_CODES)[number];\n\n/** A finding of the quote check. */\nexport type QuoteFinding = Finding<QuoteCode, \"unit\">;\n\n/**\n * The result of the check of one quote.\n *\n * - `found`: the text of the source holds the quote. `span` is its place in that text.\n * - `not-found`: the text of the source does not hold the quote.\n * - `no-read`: the check has no text of the source, so it cannot tell.\n */\nexport interface QuoteCheck {\n /** The `id` of the unit. */\n readonly unit: string;\n readonly source: SourceId;\n readonly status: \"found\" | \"not-found\" | \"no-read\";\n /** The place of the quote in the text of the source. Only the status `found` has it. */\n readonly span?: PassageSpan;\n}\n\n/** The result of the quote check: the findings, and the check of each quote. */\nexport interface QuoteResult extends GateResult<QuoteFinding> {\n readonly checks: readonly QuoteCheck[];\n}\n\n/**\n * This function compares each unit of the use `quote` with the text of its source. It has no I/O.\n *\n * - The comparison is `findPassage` of `@mark1russell7/text`. The case, the quote marks, the dashes and the white space are not significant.\n * - A quote that the text does not hold gets the error `quote-not-found`.\n * - A source with no text, or with only white space, gives the warning `quote-no-read` for each of its quotes.\n * - A unit of another use, or a unit from JSON with a bad shape, gets no check. The source gate examines its shape.\n *\n * @param units - The units of a guidebook. The function examines only the quotes.\n * @param texts - The plain text of each source that a job read, by the `id` of the source.\n * @returns The findings and the check of each quote, in the order of the units.\n */\nexport function checkQuotes(units: readonly CitedUnit[], texts: ReadonlyMap<SourceId, string>): QuoteResult {\n const findings: QuoteFinding[] = [];\n const checks: QuoteCheck[] = [];\n for (const raw of units) {\n const unit = Object(raw) as { readonly id?: unknown; readonly use?: unknown; readonly text?: unknown; readonly source?: unknown };\n if (unit.use !== \"quote\" || typeof unit.text !== \"string\" || typeof unit.source !== \"string\") continue;\n const id = String(unit.id);\n const source = unit.source;\n const text = texts.get(source);\n if (text === undefined || text.trim() === \"\") {\n checks.push({ unit: id, source, status: \"no-read\" });\n findings.push({ level: \"warning\", code: \"quote-no-read\", record: \"unit\", id, message: `The check has no text of the source \"${source}\", so it cannot examine the quote \"${id}\".` });\n continue;\n }\n const span = findPassage(text, unit.text);\n if (span === undefined) {\n checks.push({ unit: id, source, status: \"not-found\" });\n findings.push({ level: \"error\", code: \"quote-not-found\", record: \"unit\", id, message: `The text of the source \"${source}\" does not hold the quote \"${id}\".` });\n continue;\n }\n checks.push({ unit: id, source, status: \"found\", span });\n }\n return { ...resultOf(findings), checks };\n}\n\n/**\n * This function sets the date `checked` on each unit whose quote the check found. The other units do not change.\n * It throws a `RangeError` when the date is not a real date `YYYY-MM-DD`.\n *\n * @param units - The units of a guidebook.\n * @param checks - The checks of `checkQuotes`.\n * @param at - The date of the check.\n * @returns The units, in the same order.\n */\nexport function markChecked<U extends CitedUnit>(units: readonly U[], checks: readonly QuoteCheck[], at: IsoDate): readonly U[] {\n if (timeOf(at) === undefined) throw new RangeError(`markChecked: the date \"${at}\" is not a date YYYY-MM-DD.`);\n const found = new Set(checks.filter((check) => check.status === \"found\").map((check) => check.unit));\n return units.map((unit) => (unit.use === \"quote\" && found.has(unit.id) ? { ...unit, checked: at } : unit));\n}\n"],"mappings":";;;AAIA,MAAa,WAAW;CACtB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;AAMA,MAAa,OAAO;CAAC;CAAS;CAAW;CAAQ;AAAQ;;;;;;;;;;;;AA6CzD,MAAa,gBAAwD;CACnE,oBAAoB;EAClB,OAAO;EACP,KAAK;EACL,OAAO;EACP,QAAQ;EACR,OAAO;EACP,eAAe;EACf,OAAO;EACP,QAAQ;EACR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;IAAiB,QAAQ;IAAQ;IAAI,SAAS,wCAAwC,OAAO,qCAAqC,GAAG;GAAI,CAAC;GAClL;EACF;EACA,MAAM,OAAO,YAAY,MAAM,KAAK,IAAI;EACxC,IAAI,SAAS,KAAA,GAAW;GACtB,OAAO,KAAK;IAAE,MAAM;IAAI;IAAQ,QAAQ;GAAY,CAAC;GACrD,SAAS,KAAK;IAAE,OAAO;IAAS,MAAM;IAAmB,QAAQ;IAAQ;IAAI,SAAS,2BAA2B,OAAO,6BAA6B,GAAG;GAAI,CAAC;GAC7J;EACF;EACA,OAAO,KAAK;GAAE,MAAM;GAAI;GAAQ,QAAQ;GAAS;EAAK,CAAC;CACzD;CACA,OAAO;EAAE,GAAG,SAAS,QAAQ;EAAG;CAAO;AACzC;;;;;;;;;;AAWA,SAAgB,YAAiC,OAAqB,QAA+B,IAA2B;CAC9H,IAAI,OAAO,EAAE,MAAM,KAAA,GAAW,MAAM,IAAI,WAAW,0BAA0B,GAAG,4BAA4B;CAC5G,MAAM,QAAQ,IAAI,IAAI,OAAO,QAAQ,UAAU,MAAM,WAAW,OAAO,CAAC,CAAC,KAAK,UAAU,MAAM,IAAI,CAAC;CACnG,OAAO,MAAM,KAAK,SAAU,KAAK,QAAQ,WAAW,MAAM,IAAI,KAAK,EAAE,IAAI;EAAE,GAAG;EAAM,SAAS;CAAG,IAAI,IAAK;AAC3G"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/guidebook",
4
- "version": "0.5.0",
4
+ "version": "0.5.2",
5
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
6
  "keywords": [
7
7
  "guidebook",
@@ -49,9 +49,12 @@
49
49
  "@mark1russell7/cue": "^2.0.0",
50
50
  "@types/node": "^26.6.5",
51
51
  "@vitest/coverage-v8": "^5.0.3",
52
+ "tsdown": "^0.23.7",
52
53
  "typescript": "^7.0.2",
53
- "vitest": "^5.0.3",
54
- "tsdown": "^0.23.7"
54
+ "vitest": "^5.0.3"
55
+ },
56
+ "dependencies": {
57
+ "@mark1russell7/text": "^0.0.18"
55
58
  },
56
59
  "scripts": {
57
60
  "build": "tsdown",
package/src/gate.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  // The source gate: the checks of the sources and the cited units of a guidebook.
2
2
  // The gate gives each finding as data. A caller prints the findings, or stops with `assertGate`.
3
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";
4
+ import { LICENSE_RULES, LICENSES, permits, USES } from "./license.ts";
5
+ import type { Layer, License, Use, UseContext } from "./license.ts";
6
+ import type { CitedUnit, IsoDate, Source } from "./source.ts";
7
7
 
8
8
  /** The codes of the findings of the gate. */
9
9
  export const FINDING_CODES = [
@@ -90,6 +90,18 @@ export function timeOf(date: string): number | undefined {
90
90
  return new Date(time).toISOString().slice(0, 10) === date ? time : undefined;
91
91
  }
92
92
 
93
+ /**
94
+ * This function gives the fields of an item from JSON. Each field can have any type or be absent.
95
+ * `Object` boxes a primitive and gives an empty object for null, so a read of a field does not throw.
96
+ */
97
+ function fields<T>(raw: unknown): { readonly [K in keyof T]?: unknown } {
98
+ return Object(raw) as { readonly [K in keyof T]?: unknown };
99
+ }
100
+
101
+ function isUse(value: unknown): value is Use {
102
+ return typeof value === "string" && (USES as readonly string[]).includes(value);
103
+ }
104
+
93
105
  function isLicense(value: unknown): value is License {
94
106
  return typeof value === "string" && (LICENSES as readonly string[]).includes(value);
95
107
  }
@@ -120,6 +132,7 @@ export function isSha256(text: string): boolean {
120
132
  * It also examines the optional fields: the address `terms`, the date `updated` and the form of the hash `sha256`.
121
133
  * The options `require` and `https` add the rules of a dataset: the fields that each source must have, and addresses with `https:` only.
122
134
  * It throws a `RangeError` for a bad date of the gate or an unknown field in `require`.
135
+ * A source from JSON with a bad shape gives findings and not an error. A bad shape is a field of a wrong type, or an item that is not a record.
123
136
  */
124
137
  export function checkSources(sources: readonly Source[], options: GateOptions = {}): readonly Finding[] {
125
138
  const out: Finding[] = [];
@@ -132,59 +145,70 @@ export function checkSources(sources: readonly Source[], options: GateOptions =
132
145
  const unknown = (options.require ?? []).filter((field) => !(SOURCE_FIELDS as readonly string[]).includes(field));
133
146
  if (unknown.length > 0) throw new RangeError(`checkSources: the option require has the unknown fields ${unknown.join(", ")}.`);
134
147
  const necessary = SOURCE_FIELDS.filter((field) => options.require?.includes(field) === true);
135
- const seen = new Set<SourceId>();
136
- for (const source of sources) {
137
- if (seen.has(source.id)) error("duplicate-id", source.id, `Two sources have the id "${source.id}".`);
138
- seen.add(source.id);
139
- if (source.title.trim() === "") error("empty-title", source.id, `The source "${source.id}" has no title.`);
140
- 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:.`);
141
- else if (options.https === true && !isSecure(source.url)) error("bad-url", source.id, `The source "${source.id}" has the address "${source.url}". The gate accepts only an address with https:.`);
142
- if (!isLicense(source.license)) error("unknown-license", source.id, `The source "${source.id}" has the unknown license "${String(source.license)}".`);
148
+ const seen = new Set<string>();
149
+ for (const raw of sources) {
150
+ const source = fields<Source>(raw);
151
+ const id = String(source.id);
152
+ if (seen.has(id)) error("duplicate-id", id, `Two sources have the id "${id}".`);
153
+ seen.add(id);
154
+ if (typeof source.title !== "string" || source.title.trim() === "") error("empty-title", id, `The source "${id}" has no title.`);
155
+ if (typeof source.url !== "string" || !isWebAddress(source.url)) error("bad-url", id, `The source "${id}" has the address "${String(source.url)}". It needs an address with https: or http:.`);
156
+ else if (options.https === true && !isSecure(source.url)) error("bad-url", id, `The source "${id}" has the address "${String(source.url)}". The gate accepts only an address with https:.`);
157
+ if (!isLicense(source.license)) error("unknown-license", id, `The source "${id}" has the unknown license "${String(source.license)}".`);
143
158
  for (const field of necessary) {
144
159
  const value: unknown = source[field];
145
- if (typeof value !== "string" || value.trim() === "") error("missing-field", source.id, `The source "${source.id}" has no field "${field}". The gate makes it necessary.`);
160
+ if (typeof value !== "string" || value.trim() === "") error("missing-field", id, `The source "${id}" has no field "${field}". The gate makes it necessary.`);
146
161
  }
147
- if (source.terms !== undefined && !isWebAddress(source.terms)) error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". It needs an address with https: or http:.`);
162
+ if (source.terms !== undefined && (typeof source.terms !== "string" || !isWebAddress(source.terms))) error("bad-url", id, `The source "${id}" has the terms address "${String(source.terms)}". It needs an address with https: or http:.`);
148
163
  else if (source.terms !== undefined && options.https === true && !isSecure(source.terms)) {
149
- error("bad-url", source.id, `The source "${source.id}" has the terms address "${source.terms}". The gate accepts only an address with https:.`);
164
+ error("bad-url", id, `The source "${id}" has the terms address "${String(source.terms)}". The gate accepts only an address with https:.`);
150
165
  }
151
- if (source.sha256 !== undefined && !isSha256(source.sha256)) error("bad-sha256", source.id, `The hash of the source "${source.id}" is not 64 lowercase hexadecimal digits.`);
152
- const checked = timeOf(source.checked);
153
- const updated = source.updated === undefined ? undefined : timeOf(source.updated);
154
- if (source.updated !== undefined && updated === undefined) error("bad-date", source.id, `The source "${source.id}" has the change date "${source.updated}". It needs a date YYYY-MM-DD.`);
155
- if (updated !== undefined && checked !== undefined && updated > checked) error("updated-after-check", source.id, `The source "${source.id}" has a change date after its check date.`);
166
+ if (source.sha256 !== undefined && (typeof source.sha256 !== "string" || !isSha256(source.sha256))) error("bad-sha256", id, `The hash of the source "${id}" is not 64 lowercase hexadecimal digits.`);
167
+ const checked = typeof source.checked === "string" ? timeOf(source.checked) : undefined;
168
+ const updated = typeof source.updated === "string" ? timeOf(source.updated) : undefined;
169
+ if (source.updated !== undefined && updated === undefined) error("bad-date", id, `The source "${id}" has the change date "${String(source.updated)}". It needs a date YYYY-MM-DD.`);
170
+ if (updated !== undefined && checked !== undefined && updated > checked) error("updated-after-check", id, `The source "${id}" has a change date after its check date.`);
156
171
  if (checked === undefined) {
157
- error("bad-date", source.id, `The source "${source.id}" has the check date "${source.checked}". It needs a date YYYY-MM-DD.`);
172
+ error("bad-date", id, `The source "${id}" has the check date "${String(source.checked)}". It needs a date YYYY-MM-DD.`);
158
173
  } else if (at !== undefined) {
159
- if (checked > at) error("checked-in-future", source.id, `The source "${source.id}" has a check date after the date of the gate.`);
174
+ if (checked > at) error("checked-in-future", id, `The source "${id}" has a check date after the date of the gate.`);
160
175
  const days = (at - checked) / DAY_MS;
161
176
  if (days > maxAge) {
162
- 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.` });
177
+ out.push({ level: "warning", code: "stale", record: "source", id: id, message: `The last check of the source "${id}" is ${days} days old. Read the source again.` });
163
178
  }
164
179
  }
165
180
  }
166
181
  return out;
167
182
  }
168
183
 
169
- /** This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses. */
184
+ /**
185
+ * This function gives the findings of the units: duplicate ids, empty text, a missing source, and a use that the license refuses.
186
+ * A unit or a source from JSON with a bad shape gives findings and not an error.
187
+ */
170
188
  export function checkUnits(units: readonly CitedUnit[], sources: readonly Source[], options: UseContext = {}): readonly Finding[] {
171
189
  const out: Finding[] = [];
172
190
  const error = (code: FindingCode, id: string, message: string): void => {
173
191
  out.push({ level: "error", code, record: "unit", id, message });
174
192
  };
175
- const byId = new Map(sources.map((source) => [source.id, source] as const));
193
+ const byId = new Map(sources.map((raw) => fields<Source>(raw)).map((source) => [source.id, source] as const));
176
194
  const seen = new Set<string>();
177
- for (const unit of units) {
178
- if (seen.has(unit.id)) error("duplicate-id", unit.id, `Two units have the id "${unit.id}".`);
179
- seen.add(unit.id);
180
- if (unit.text.trim() === "") error("empty-text", unit.id, `The unit "${unit.id}" has no text.`);
195
+ for (const raw of units) {
196
+ const unit = fields<CitedUnit>(raw);
197
+ const id = String(unit.id);
198
+ if (seen.has(id)) error("duplicate-id", id, `Two units have the id "${id}".`);
199
+ seen.add(id);
200
+ if (typeof unit.text !== "string" || unit.text.trim() === "") error("empty-text", id, `The unit "${id}" has no text.`);
201
+ if (unit.checked !== undefined && (typeof unit.checked !== "string" || timeOf(unit.checked) === undefined)) {
202
+ error("bad-date", id, `The unit "${id}" has the check date "${String(unit.checked)}". It needs a date YYYY-MM-DD.`);
203
+ }
181
204
  const source = byId.get(unit.source);
182
205
  if (source === undefined) {
183
- error("missing-source", unit.id, `The unit "${unit.id}" cites the source "${unit.source}", but the list does not have it.`);
206
+ error("missing-source", id, `The unit "${id}" cites the source "${String(unit.source)}", but the list does not have it.`);
184
207
  continue;
185
208
  }
186
- if (isLicense(source.license) && !permits(source.license, unit.use, options)) {
187
- error("use-refused", unit.id, `The license "${source.license}" of the source "${source.id}" refuses the use "${unit.use}" of the unit "${unit.id}".`);
209
+ if (!isUse(unit.use)) error("use-refused", id, `The unit "${id}" has the unknown use "${String(unit.use)}". A use is ${USES.join(", ")}.`);
210
+ else if (isLicense(source.license) && !permits(source.license, unit.use, options)) {
211
+ error("use-refused", id, `The license "${source.license}" of the source "${String(source.id)}" refuses the use "${unit.use}" of the unit "${id}".`);
188
212
  }
189
213
  }
190
214
  return out;
package/src/index.ts CHANGED
@@ -7,3 +7,5 @@ export { creditOf } from "./source.ts";
7
7
  export type { CitedUnit, IsoDate, Source, SourceId, UnitUse } from "./source.ts";
8
8
  export { DAY_MS, FINDING_CODES, SOURCE_FIELDS, assertGate, checkSources, checkUnits, gate, isSha256, isWebAddress, layerOf, resultOf, throwOnErrors, timeOf } from "./gate.ts";
9
9
  export type { Finding, FindingCode, GateOptions, GateResult, SourceField } from "./gate.ts";
10
+ export { QUOTE_CODES, checkQuotes, markChecked } from "./quote.ts";
11
+ export type { QuoteCheck, QuoteCode, QuoteFinding, QuoteResult } from "./quote.ts";
package/src/quote.ts ADDED
@@ -0,0 +1,90 @@
1
+ // The quote check: each unit of the use `quote` against the text of its source.
2
+ // The check is pure. A job reads the text of each source, gives the texts to `checkQuotes`, and sets the dates with `markChecked`.
3
+
4
+ import { findPassage } from "@mark1russell7/text/passage";
5
+ import type { PassageSpan } from "@mark1russell7/text/passage";
6
+ import { resultOf, timeOf } from "./gate.ts";
7
+ import type { Finding, GateResult } from "./gate.ts";
8
+ import type { CitedUnit, IsoDate, SourceId } from "./source.ts";
9
+
10
+ /** The codes of the findings of the quote check. */
11
+ export const QUOTE_CODES = ["quote-not-found", "quote-no-read"] as const;
12
+
13
+ /** A code of a finding of the quote check. */
14
+ export type QuoteCode = (typeof QUOTE_CODES)[number];
15
+
16
+ /** A finding of the quote check. */
17
+ export type QuoteFinding = Finding<QuoteCode, "unit">;
18
+
19
+ /**
20
+ * The result of the check of one quote.
21
+ *
22
+ * - `found`: the text of the source holds the quote. `span` is its place in that text.
23
+ * - `not-found`: the text of the source does not hold the quote.
24
+ * - `no-read`: the check has no text of the source, so it cannot tell.
25
+ */
26
+ export interface QuoteCheck {
27
+ /** The `id` of the unit. */
28
+ readonly unit: string;
29
+ readonly source: SourceId;
30
+ readonly status: "found" | "not-found" | "no-read";
31
+ /** The place of the quote in the text of the source. Only the status `found` has it. */
32
+ readonly span?: PassageSpan;
33
+ }
34
+
35
+ /** The result of the quote check: the findings, and the check of each quote. */
36
+ export interface QuoteResult extends GateResult<QuoteFinding> {
37
+ readonly checks: readonly QuoteCheck[];
38
+ }
39
+
40
+ /**
41
+ * This function compares each unit of the use `quote` with the text of its source. It has no I/O.
42
+ *
43
+ * - The comparison is `findPassage` of `@mark1russell7/text`. The case, the quote marks, the dashes and the white space are not significant.
44
+ * - A quote that the text does not hold gets the error `quote-not-found`.
45
+ * - A source with no text, or with only white space, gives the warning `quote-no-read` for each of its quotes.
46
+ * - A unit of another use, or a unit from JSON with a bad shape, gets no check. The source gate examines its shape.
47
+ *
48
+ * @param units - The units of a guidebook. The function examines only the quotes.
49
+ * @param texts - The plain text of each source that a job read, by the `id` of the source.
50
+ * @returns The findings and the check of each quote, in the order of the units.
51
+ */
52
+ export function checkQuotes(units: readonly CitedUnit[], texts: ReadonlyMap<SourceId, string>): QuoteResult {
53
+ const findings: QuoteFinding[] = [];
54
+ const checks: QuoteCheck[] = [];
55
+ for (const raw of units) {
56
+ const unit = Object(raw) as { readonly id?: unknown; readonly use?: unknown; readonly text?: unknown; readonly source?: unknown };
57
+ if (unit.use !== "quote" || typeof unit.text !== "string" || typeof unit.source !== "string") continue;
58
+ const id = String(unit.id);
59
+ const source = unit.source;
60
+ const text = texts.get(source);
61
+ if (text === undefined || text.trim() === "") {
62
+ checks.push({ unit: id, source, status: "no-read" });
63
+ findings.push({ level: "warning", code: "quote-no-read", record: "unit", id, message: `The check has no text of the source "${source}", so it cannot examine the quote "${id}".` });
64
+ continue;
65
+ }
66
+ const span = findPassage(text, unit.text);
67
+ if (span === undefined) {
68
+ checks.push({ unit: id, source, status: "not-found" });
69
+ findings.push({ level: "error", code: "quote-not-found", record: "unit", id, message: `The text of the source "${source}" does not hold the quote "${id}".` });
70
+ continue;
71
+ }
72
+ checks.push({ unit: id, source, status: "found", span });
73
+ }
74
+ return { ...resultOf(findings), checks };
75
+ }
76
+
77
+ /**
78
+ * This function sets the date `checked` on each unit whose quote the check found. The other units do not change.
79
+ * It throws a `RangeError` when the date is not a real date `YYYY-MM-DD`.
80
+ *
81
+ * @param units - The units of a guidebook.
82
+ * @param checks - The checks of `checkQuotes`.
83
+ * @param at - The date of the check.
84
+ * @returns The units, in the same order.
85
+ */
86
+ export function markChecked<U extends CitedUnit>(units: readonly U[], checks: readonly QuoteCheck[], at: IsoDate): readonly U[] {
87
+ if (timeOf(at) === undefined) throw new RangeError(`markChecked: the date "${at}" is not a date YYYY-MM-DD.`);
88
+ const found = new Set(checks.filter((check) => check.status === "found").map((check) => check.unit));
89
+ return units.map((unit) => (unit.use === "quote" && found.has(unit.id) ? { ...unit, checked: at } : unit));
90
+ }
package/src/source.ts CHANGED
@@ -43,6 +43,8 @@ export interface CitedUnit {
43
43
  readonly source: SourceId;
44
44
  /** The place of the text in the source, for example a section or a page. */
45
45
  readonly locator?: string;
46
+ /** The date of the last quote check that found the text in the source. Only a quote has it. `markChecked` sets it. */
47
+ readonly checked?: IsoDate;
46
48
  }
47
49
 
48
50
  /**