@rangefoundry/catalog 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ import type { Bundle } from './records.ts';
2
+ type CaliberBundle = Pick<Bundle, 'caliberClasses' | 'cartridges' | 'disciplines'>;
3
+ /** Every member a cartridge record in one class only, with G1 of its sheet within 0.15 mm of the class's nominal diameter; every class a discipline names is a record. */
4
+ export declare function checkCaliberClasses(bundle: CaliberBundle): string[];
5
+ export {};
@@ -0,0 +1,44 @@
1
+ /** Largest gap in mm between a class's nominal diameter and G1 of a member cartridge. */
2
+ const diameterTolerance = 0.15;
3
+ /** Maximum projectile diameter G1 of the cartridge side of a sheet, mm. */
4
+ const projectileDiameter = (values) => {
5
+ const value = values.find((v) => v.symbol === 'G1' && v.side === 'cartridge')?.value;
6
+ return typeof value === 'number' ? value : undefined;
7
+ };
8
+ /** Checks one member of a class: its record, its single class and G1 against the class diameter. */
9
+ function checkMember(c, id, { cartridges, owner }) {
10
+ const where = `caliber-classes/${c.id}.json`;
11
+ const cartridge = cartridges.get(id);
12
+ if (!cartridge)
13
+ return [`${where}: unknown cartridge "${id}"`];
14
+ const errors = [];
15
+ const other = owner.get(cartridge.id);
16
+ if (other)
17
+ errors.push(`${where}: cartridge "${id}" is also in ${other}`);
18
+ else
19
+ owner.set(cartridge.id, c.id);
20
+ const g1 = projectileDiameter(cartridge.values);
21
+ if (g1 === undefined)
22
+ errors.push(`${where}: cartridge "${id}" has no G1`);
23
+ else if (Math.abs(g1 - c.diameter.value) > diameterTolerance)
24
+ errors.push(`${where}: diameter ${c.diameter.value} mm is more than ${diameterTolerance} mm from G1 ${g1} mm of "${id}"`);
25
+ return errors;
26
+ }
27
+ /** Every member a cartridge record in one class only, with G1 of its sheet within 0.15 mm of the class's nominal diameter; every class a discipline names is a record. */
28
+ export function checkCaliberClasses(bundle) {
29
+ const errors = [];
30
+ const members = {
31
+ cartridges: new Map(bundle.cartridges.map((c) => [c.id, c])),
32
+ owner: new Map(),
33
+ };
34
+ for (const c of bundle.caliberClasses)
35
+ for (const id of c.cartridges)
36
+ errors.push(...checkMember(c, id, members));
37
+ const classes = new Set(bundle.caliberClasses.map((c) => c.id));
38
+ for (const d of bundle.disciplines)
39
+ for (const id of d.caliberClasses?.ids ?? [])
40
+ if (!classes.has(id))
41
+ errors.push(`disciplines/${d.id}.json: unknown caliber class "${id}"`);
42
+ return errors;
43
+ }
44
+ //# sourceMappingURL=caliber-classes.js.map
@@ -0,0 +1,237 @@
1
+ import type { Bullet, CaliberClass, Cartridge, CaseMaterial, CaseType, CatalogueCase, CatalogueCaseCapacity, Discipline, EventType, Federation, Manufacturer, Powder, Primer, PrimerCompound, PrimerSize, TargetFace, TargetMapping } from '@rangefoundry/schema';
2
+ /** The entities of the catalogue, the names `Catalogue.get` takes. */
3
+ export type CatalogueKind = 'cartridge' | 'manufacturer' | 'powder' | 'primer' | 'bullet' | 'case' | 'caseCapacity' | 'targetFace' | 'federation' | 'discipline' | 'targetMapping' | 'caliberClass' | 'eventType';
4
+ /** The record type of each {@link CatalogueKind}. */
5
+ export interface RecordOfKind {
6
+ cartridge: Cartridge;
7
+ manufacturer: Manufacturer;
8
+ powder: Powder;
9
+ primer: Primer;
10
+ bullet: Bullet;
11
+ /** The record file leaves out `origin`, which every catalogue record has. */
12
+ case: Omit<CatalogueCase, 'origin'>;
13
+ /** The record file leaves out `origin`, which every catalogue record has. */
14
+ caseCapacity: Omit<CatalogueCaseCapacity, 'origin'>;
15
+ targetFace: TargetFace;
16
+ federation: Federation;
17
+ discipline: Discipline;
18
+ targetMapping: TargetMapping;
19
+ caliberClass: CaliberClass;
20
+ eventType: EventType;
21
+ }
22
+ /**
23
+ * A record as `Catalogue.get` answers it: the stored record with its `kind`, `origin` (always
24
+ * `catalogue`) and `revision` (a hash of the stored record, changes with any field). The
25
+ * generated record file holds none of the three.
26
+ */
27
+ export type Catalogued<K extends CatalogueKind> = RecordOfKind[K] & {
28
+ kind: K;
29
+ origin: 'catalogue';
30
+ revision: string;
31
+ };
32
+ /** One page of a list: `total` counts the items before paging. */
33
+ export interface CataloguePage<T> {
34
+ total: number;
35
+ items: T[];
36
+ }
37
+ /** Paging of a list: `limit` items from `offset`, every item without them. */
38
+ export interface Paging {
39
+ limit?: number | undefined;
40
+ offset?: number | undefined;
41
+ }
42
+ /** Query of `Catalogue.cartridges`: `q` searches names and aliases, `table` is the C.I.P. table. */
43
+ export interface CartridgeQuery extends Paging {
44
+ q?: string | undefined;
45
+ table?: string | undefined;
46
+ }
47
+ /** Query of `Catalogue.powders`: `q` searches the manufacturer's name or alias with the product name, `manufacturer` is a manufacturer id. */
48
+ export interface PowderQuery extends Paging {
49
+ q?: string | undefined;
50
+ manufacturer?: string | undefined;
51
+ }
52
+ /** Query of `Catalogue.bullets`: as {@link PowderQuery}, `cartridge` keeps the bullets that fit that cartridge (`bulletWindow` of `@rangefoundry/ballistics`). */
53
+ export interface BulletQuery extends PowderQuery {
54
+ cartridge?: string | undefined;
55
+ }
56
+ /** Query of `Catalogue.primers`: as {@link PowderQuery}, `size` and `compound` keep the primers of that size and priming compound. */
57
+ export interface PrimerQuery extends PowderQuery {
58
+ size?: PrimerSize | undefined;
59
+ compound?: PrimerCompound | undefined;
60
+ }
61
+ /** Query of `Catalogue.cases`: as {@link PowderQuery}, `cartridge` keeps the cases of that cartridge (an id). */
62
+ export interface CaseQuery extends PowderQuery {
63
+ cartridge?: string | undefined;
64
+ }
65
+ /** A cartridge of the list (`GET /cartridges`). Lengths and diameters in mm, `pmax` in bar. */
66
+ export interface CartridgeItem {
67
+ id: string;
68
+ name: string;
69
+ aliases: string[];
70
+ table: string;
71
+ maxBulletDiameter?: number;
72
+ boreDiameter?: number;
73
+ grooveDiameter?: number;
74
+ caseLength?: number;
75
+ cartridgeLength?: number;
76
+ pmax?: number;
77
+ caseType?: CaseType;
78
+ country?: string;
79
+ }
80
+ /** A manufacturer of the list. */
81
+ export interface ManufacturerItem {
82
+ id: string;
83
+ name: string;
84
+ aliases?: string[];
85
+ website?: string;
86
+ }
87
+ /** The part of a powder's M3 parameters the burn-rate curve reads, with the fit's basis. */
88
+ export interface PowderInteriorItem {
89
+ vivacity?: unknown;
90
+ pressureExponent?: number;
91
+ referencePressure?: unknown;
92
+ burnForm?: unknown;
93
+ trainingRows?: number;
94
+ trainingCartridges?: number;
95
+ /** ISO date the latest fitted source was read. */
96
+ retrieved?: string;
97
+ }
98
+ /** A powder of the list. */
99
+ export interface PowderItem {
100
+ id: string;
101
+ manufacturer: string;
102
+ name: string;
103
+ grainForm?: string;
104
+ propellantBase?: string;
105
+ country?: string;
106
+ /** The uses the maker states: `rifle`, `handgun`, `shotgun`. */
107
+ scope?: string[];
108
+ interior?: PowderInteriorItem;
109
+ }
110
+ /** A primer of the list. `manufacturer` is a record id. */
111
+ export interface PrimerItem {
112
+ id: string;
113
+ manufacturer: string;
114
+ name: string;
115
+ productNumber?: string;
116
+ size: PrimerSize;
117
+ magnum?: boolean;
118
+ compound?: PrimerCompound;
119
+ }
120
+ /** A bullet of the list: values of the record (mm, g), null where it has none. */
121
+ export interface BulletItem {
122
+ id: string;
123
+ manufacturer: string;
124
+ name: string;
125
+ diameter: number | null;
126
+ mass: number;
127
+ length: number | null;
128
+ ballisticCoefficientG1: number | null;
129
+ ballisticCoefficientG7: number | null;
130
+ country?: string;
131
+ tipType?: string;
132
+ baseType?: string;
133
+ construction?: string;
134
+ leadFree?: boolean;
135
+ nameOnly?: true;
136
+ }
137
+ /** A case of the list. `manufacturer` and `cartridge` are record ids; `waterCapacity` (the stated one) and `mass` in g. */
138
+ export interface CaseItem {
139
+ id: string;
140
+ manufacturer: string;
141
+ cartridge: string;
142
+ name: string;
143
+ productNumber: string;
144
+ material: CaseMaterial;
145
+ waterCapacity?: number;
146
+ mass?: number;
147
+ }
148
+ /** A target face of the list, with its caliber classes and categories. `organization` is the abbreviation of the face's federation. */
149
+ export interface TargetFaceItem {
150
+ id: string;
151
+ federation?: string | undefined;
152
+ organization?: string;
153
+ name: string;
154
+ caliberClasses: string[];
155
+ categories: string[];
156
+ }
157
+ /** A federation of the list. */
158
+ export interface FederationItem {
159
+ id: string;
160
+ name: string;
161
+ abbreviation: string;
162
+ country?: string;
163
+ internationalFederation?: string;
164
+ website?: string;
165
+ }
166
+ /** A discipline of the list. */
167
+ export interface DisciplineItem {
168
+ id: string;
169
+ federation: string;
170
+ name: string;
171
+ number?: string;
172
+ distanceMm: number;
173
+ category?: string;
174
+ }
175
+ /** A target mapping of the list. */
176
+ export interface TargetMappingItem {
177
+ id: string;
178
+ discipline: string;
179
+ face: string;
180
+ event?: string;
181
+ stage?: string;
182
+ gaugeMm?: number;
183
+ }
184
+ /** A caliber class of the list. Diameter in mm. */
185
+ export interface CaliberClassItem {
186
+ id: string;
187
+ name: string;
188
+ diameterMm: number;
189
+ }
190
+ /** An event type of the list: its federation and scoring method. */
191
+ export interface EventTypeItem {
192
+ id: string;
193
+ name: string;
194
+ federation: string;
195
+ method: 'hit-factor' | 'rings';
196
+ }
197
+ /** The read-only catalogue: lists in default order, records by id. The same in the API server, the Pages build, the browser and tests. */
198
+ export interface Catalogue {
199
+ /** Cartridges, ranked by `q` where set. */
200
+ cartridges(query?: CartridgeQuery): CataloguePage<CartridgeItem>;
201
+ /** Manufacturers by name. */
202
+ manufacturers(): CataloguePage<ManufacturerItem>;
203
+ /** Powders by manufacturer name and name, ranked by `q` where set. */
204
+ powders(query?: PowderQuery): CataloguePage<PowderItem>;
205
+ /** Primers by manufacturer name and name, ranked by `q` where set. */
206
+ primers(query?: PrimerQuery): CataloguePage<PrimerItem>;
207
+ /** Bullets by manufacturer name and name, ranked by `q` where set; none for a `cartridge` that is unknown or lacks its C.I.P. G1, F or Z. */
208
+ bullets(query?: BulletQuery): CataloguePage<BulletItem>;
209
+ /** Cases by manufacturer name and name, ranked by `q` where set. */
210
+ cases(query?: CaseQuery): CataloguePage<CaseItem>;
211
+ /** Every target face. */
212
+ targetFaces(): CataloguePage<TargetFaceItem>;
213
+ /** Every federation. */
214
+ federations(): CataloguePage<FederationItem>;
215
+ /** Disciplines, of one federation where `federation` is set. */
216
+ disciplines(query?: {
217
+ federation?: string | undefined;
218
+ }): CataloguePage<DisciplineItem>;
219
+ /** Target mappings, of one discipline and one face where set. */
220
+ targetMappings(query?: {
221
+ discipline?: string | undefined;
222
+ face?: string | undefined;
223
+ }): CataloguePage<TargetMappingItem>;
224
+ /** Caliber classes by nominal diameter. */
225
+ caliberClasses(): CataloguePage<CaliberClassItem>;
226
+ /** Event types by name. */
227
+ eventTypes(): CataloguePage<EventTypeItem>;
228
+ /** The record `id` of `kind`; undefined if unknown. */
229
+ get<K extends CatalogueKind>(kind: K, id: string): Catalogued<K> | undefined;
230
+ /** The case capacity records of a cartridge, ordered by id. */
231
+ capacities(cartridge: string): Catalogued<'caseCapacity'>[];
232
+ /**
233
+ * The SHA-256 of the whole data set (every record) as 64 hex digits: the same
234
+ * records give the same value, any changed field changes it. Reads the record file on the first call.
235
+ */
236
+ dataHash(): Promise<string>;
237
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=catalogue.js.map
@@ -0,0 +1,12 @@
1
+ import { type Provenance } from '@rangefoundry/schema';
2
+ /** The kinds whose fields the schema sorts into basic and detailed; every field of a manufacturer is basic. */
3
+ export type FieldClassKind = 'bullet' | 'powder' | 'manufacturer';
4
+ /**
5
+ * Checks that every field that needs a source has one, and that every `fields` entry names a field
6
+ * of the record. Of a bullet or powder (`kind`) only the detailed fields need a source and of a
7
+ * manufacturer none; of the other kinds every field outside the identity set does. A record without
8
+ * provenance passes when none of its fields needs a source.
9
+ */
10
+ export declare function checkCoverage(record: {
11
+ provenance?: Provenance | undefined;
12
+ }, kind?: FieldClassKind): string[];
@@ -0,0 +1,86 @@
1
+ import { detailedFields } from '@rangefoundry/schema';
2
+ import { isObject } from './json-object.js';
3
+ /** Of the kinds without field classes, the fields that name a record rather than carry data, and need no source of their own. */
4
+ const identity = new Set([
5
+ 'id',
6
+ 'origin',
7
+ 'manufacturer',
8
+ 'productNumber',
9
+ 'headstamp',
10
+ 'name',
11
+ 'productLine',
12
+ 'caliber',
13
+ 'cartridge',
14
+ 'note',
15
+ 'nameOnly',
16
+ ]);
17
+ /** Quantity objects (`{ value, raw }`) and arrays count as one field. */
18
+ function leaves(value, path, out) {
19
+ const isQuantity = typeof value === 'object' && value !== null && !Array.isArray(value) && 'value' in value;
20
+ if (typeof value !== 'object' || value === null || Array.isArray(value) || isQuantity) {
21
+ out.push(path);
22
+ return;
23
+ }
24
+ for (const [key, child] of Object.entries(value))
25
+ leaves(child, path ? `${path}.${key}` : key, out);
26
+ }
27
+ const missing = Symbol('missing');
28
+ /** The value at the dot path `path` of `record`, or `missing` where a key on the way is absent. */
29
+ function valueAt(record, path) {
30
+ let node = record;
31
+ for (const key of path.split('.')) {
32
+ if (!isObject(node) || !(key in node))
33
+ return missing;
34
+ node = node[key];
35
+ }
36
+ return node;
37
+ }
38
+ /** The fields of `record` that need a covering source: the detailed ones where its kind has field classes, else each one outside the identity set. */
39
+ function sourcedFields(record, kind) {
40
+ const paths = [];
41
+ if (kind === 'manufacturer')
42
+ return paths;
43
+ if (kind) {
44
+ for (const path of detailedFields(kind, record))
45
+ leaves(valueAt(record, path), path, paths);
46
+ return paths;
47
+ }
48
+ for (const [key, value] of Object.entries(record)) {
49
+ if (!identity.has(key) && key !== 'provenance')
50
+ leaves(value, key, paths);
51
+ }
52
+ return paths;
53
+ }
54
+ const noSource = (path) => `field ${path} has no source`;
55
+ /** Each `fields` entry of a source names a field of the record. */
56
+ function missingFields(record) {
57
+ return record.provenance.sources.flatMap((source) => (source.fields ?? [])
58
+ .filter((field) => valueAt(record, field) === missing)
59
+ .map((field) => `source "${source.title}" names missing field ${field}`));
60
+ }
61
+ /** Each field of `paths` is supported by a source; a source without `fields` supports them all. */
62
+ function uncoveredFields(paths, sources) {
63
+ if (sources.some((s) => !s.fields))
64
+ return [];
65
+ const covered = sources.flatMap((s) => s.fields ?? []);
66
+ return paths
67
+ .filter((path) => !covered.some((f) => path === f || path.startsWith(`${f}.`)))
68
+ .map(noSource);
69
+ }
70
+ /**
71
+ * Checks that every field that needs a source has one, and that every `fields` entry names a field
72
+ * of the record. Of a bullet or powder (`kind`) only the detailed fields need a source and of a
73
+ * manufacturer none; of the other kinds every field outside the identity set does. A record without
74
+ * provenance passes when none of its fields needs a source.
75
+ */
76
+ export function checkCoverage(record, kind) {
77
+ const paths = sourcedFields(record, kind);
78
+ const { provenance } = record;
79
+ if (!provenance)
80
+ return paths.map(noSource);
81
+ return [
82
+ ...missingFields({ ...record, provenance }),
83
+ ...uncoveredFields(paths, provenance.sources),
84
+ ];
85
+ }
86
+ //# sourceMappingURL=coverage.js.map
@@ -0,0 +1,12 @@
1
+ import type { DataSet, SourceRecord } from '@rangefoundry/schema';
2
+ import type { SourceIndex } from './sources.ts';
3
+ /**
4
+ * The publication rule of a data set's records, which the catalog check runs on them in place of
5
+ * the maker-source rule (`makerRows`): the records are another project's data, published under its
6
+ * license with its attribution. The manifest gives the attribution, license, source URL and removal
7
+ * contact (its schema requires each); the rule adds that the attribution names the removal contact,
8
+ * the source URL names the converted commit, the set's source record carries the same license and
9
+ * URL and is not a listed non-maker source, and every record file in `files` cites that source record
10
+ * only. `citations` gives the cited source ids of each record file, undefined for an embedded source.
11
+ */
12
+ export declare function dataSetRule(set: DataSet, source: SourceRecord | undefined, files: readonly string[], citations: SourceIndex['citations']): string[];
@@ -0,0 +1,37 @@
1
+ import { nonMakerSources } from './maker-rows.js';
2
+ /** Where the set's source record `source` differs from the manifest `set` in license or URL, or is missing. */
3
+ function sourceErrors(set, source) {
4
+ const where = `sources/${set.source}.json`;
5
+ if (!source)
6
+ return [`${where}: missing`];
7
+ const errors = [];
8
+ if (source.license !== set.license)
9
+ errors.push(`${where}: license is ${source.license ?? 'missing'}, set.json says ${set.license}`);
10
+ if (source.url !== set.url)
11
+ errors.push(`${where}: url is ${source.url ?? 'missing'}, set.json says ${set.url}`);
12
+ return errors;
13
+ }
14
+ /**
15
+ * The publication rule of a data set's records, which the catalog check runs on them in place of
16
+ * the maker-source rule (`makerRows`): the records are another project's data, published under its
17
+ * license with its attribution. The manifest gives the attribution, license, source URL and removal
18
+ * contact (its schema requires each); the rule adds that the attribution names the removal contact,
19
+ * the source URL names the converted commit, the set's source record carries the same license and
20
+ * URL and is not a listed non-maker source, and every record file in `files` cites that source record
21
+ * only. `citations` gives the cited source ids of each record file, undefined for an embedded source.
22
+ */
23
+ export function dataSetRule(set, source, files, citations) {
24
+ const errors = [];
25
+ if (Object.hasOwn(nonMakerSources, set.source))
26
+ errors.push(`set.json: source "${set.source}" is a listed non-maker source`);
27
+ if (!set.attribution.includes(set.removalContact))
28
+ errors.push(`set.json: attribution does not name the removal contact ${set.removalContact}`);
29
+ if (!set.url.includes(set.commit))
30
+ errors.push(`set.json: url does not name the commit ${set.commit}`);
31
+ errors.push(...sourceErrors(set, source));
32
+ for (const file of files)
33
+ if (citations.get(file)?.some((id) => id !== set.source))
34
+ errors.push(`${file}: cites a source other than the set's source "${set.source}"`);
35
+ return errors;
36
+ }
37
+ //# sourceMappingURL=data-set-rule.js.map
@@ -0,0 +1,12 @@
1
+ import type { DataSet } from '@rangefoundry/schema';
2
+ import type { Bundle } from './records.ts';
3
+ import type { SourceIndex } from './sources.ts';
4
+ /** The record kinds a data set holds beside its source records, each in the folder of its name. */
5
+ export declare const dataSetKinds: readonly ['manufacturers', 'bullets', 'powders'];
6
+ /**
7
+ * Errors of the records of the data set `set` (its own records only, not the built-in catalog it is
8
+ * read against): a bullet or powder id without the set's id prefix, a record that does not cite the
9
+ * set's source record, and counts in the manifest that differ from the records not retired. `citations`
10
+ * gives the cited source ids of each record file.
11
+ */
12
+ export declare function checkDataSet(set: DataSet, records: Pick<Bundle, (typeof dataSetKinds)[number]>, citations: SourceIndex['citations']): string[];
@@ -0,0 +1,26 @@
1
+ /** The record kinds a data set holds beside its source records, each in the folder of its name. */
2
+ export const dataSetKinds = ['manufacturers', 'bullets', 'powders'];
3
+ /**
4
+ * Errors of the records of the data set `set` (its own records only, not the built-in catalog it is
5
+ * read against): a bullet or powder id without the set's id prefix, a record that does not cite the
6
+ * set's source record, and counts in the manifest that differ from the records not retired. `citations`
7
+ * gives the cited source ids of each record file.
8
+ */
9
+ export function checkDataSet(set, records, citations) {
10
+ const errors = [];
11
+ for (const kind of dataSetKinds) {
12
+ const own = records[kind];
13
+ for (const { id } of own) {
14
+ const where = `${kind}/${id}.json`;
15
+ if (kind !== 'manufacturers' && !id.startsWith(`${set.idPrefix}-`))
16
+ errors.push(`${where}: id does not start with the set's prefix "${set.idPrefix}-"`);
17
+ if (!citations.get(where)?.includes(set.source))
18
+ errors.push(`${where}: does not cite the set's source "${set.source}"`);
19
+ }
20
+ const current = own.filter((r) => r.retired === undefined).length;
21
+ if (current !== set.counts[kind])
22
+ errors.push(`set.json: counts.${kind} is ${set.counts[kind]}, the set holds ${current}`);
23
+ }
24
+ return errors;
25
+ }
26
+ //# sourceMappingURL=data-set.js.map
@@ -0,0 +1,3 @@
1
+ import type { Bundle } from './records.ts';
2
+ /** The federation is a record and lists the rule book, a ring scoring's discipline is one of that federation, no division is listed twice and every division of a magazine limit has a power factor. */
3
+ export declare function checkEventTypes(bundle: Pick<Bundle, 'eventTypes' | 'federations' | 'disciplines'>): string[];
@@ -0,0 +1,49 @@
1
+ import { lookupOf } from './federations.js';
2
+ /** A division listed twice among the power factors, a magazine limit of a division without a power factor or for a major rating the division lacks; ring scoring has no divisions, so every magazine limit of a ring event type is an error. */
3
+ function checkDivisions(t, where, errors) {
4
+ const floors = t.scoring.method === 'hit-factor'
5
+ ? new Map(t.scoring.powerFactors.map((p) => [p.division, p]))
6
+ : new Map();
7
+ if (t.scoring.method === 'hit-factor' && floors.size !== t.scoring.powerFactors.length)
8
+ errors.push(`${where}: a division is listed twice in scoring.powerFactors`);
9
+ for (const m of t.magazine ?? []) {
10
+ const floor = floors.get(m.division);
11
+ if (!floor)
12
+ errors.push(`${where}: magazine names division "${m.division}" without a power factor`);
13
+ else if (m.powerFactor === 'major' && floor.major === undefined)
14
+ errors.push(`${where}: magazine limits major in division "${m.division}", which has none`);
15
+ }
16
+ }
17
+ /** The federation is a record and lists the rule book; returns the federation. */
18
+ function checkFederation(t, where, lookup, errors) {
19
+ const federation = lookup.federations.get(t.federation);
20
+ if (!federation)
21
+ errors.push(`${where}: unknown federation "${t.federation}"`);
22
+ else if (!federation.ruleBooks.some((b) => b.sha256 === t.ruleBook.sha256))
23
+ errors.push(`${where}: rule book is not one of federation "${federation.id}"`);
24
+ return federation;
25
+ }
26
+ /** A ring scoring's discipline is a record of the event type's federation. */
27
+ function checkDiscipline(t, where, federation, lookup) {
28
+ if (t.scoring.method !== 'rings' || !t.scoring.discipline)
29
+ return [];
30
+ const d = lookup.disciplines.get(t.scoring.discipline);
31
+ if (!d)
32
+ return [`${where}: unknown discipline "${t.scoring.discipline}"`];
33
+ if (federation && d.federation !== federation.id)
34
+ return [`${where}: discipline "${d.id}" is not one of federation "${federation.id}"`];
35
+ return [];
36
+ }
37
+ /** The federation is a record and lists the rule book, a ring scoring's discipline is one of that federation, no division is listed twice and every division of a magazine limit has a power factor. */
38
+ export function checkEventTypes(bundle) {
39
+ const errors = [];
40
+ const lookup = lookupOf(bundle);
41
+ for (const t of bundle.eventTypes ?? []) {
42
+ const where = `event-types/${t.id}.json`;
43
+ const federation = checkFederation(t, where, lookup, errors);
44
+ errors.push(...checkDiscipline(t, where, federation, lookup));
45
+ checkDivisions(t, where, errors);
46
+ }
47
+ return errors;
48
+ }
49
+ //# sourceMappingURL=event-types.js.map
@@ -0,0 +1,13 @@
1
+ import type { Discipline, Federation, TargetFace } from '@rangefoundry/schema';
2
+ import type { Bundle } from './records.ts';
3
+ /** The federations, disciplines and faces by id. */
4
+ export type Lookup = {
5
+ federations: Map<string, Federation>;
6
+ disciplines: Map<string, Discipline>;
7
+ faces: Map<string, TargetFace>;
8
+ worldBody: (id: string) => boolean;
9
+ };
10
+ /** The lookup of a bundle, shared by the federation and event type checks. */
11
+ export declare function lookupOf(bundle: Pick<Bundle, 'federations' | 'disciplines'> & Partial<Pick<Bundle, 'targetFaces'>>): Lookup;
12
+ /** Every id names a record; a discipline's international equivalent belongs to an international federation; a mapping names an event of its face that the discipline's federation lists, with that event's gauge; each face event has at most one mapping, each discipline at least one. */
13
+ export declare function checkFederations(bundle: Pick<Bundle, 'federations' | 'disciplines' | 'targetMappings' | 'targetFaces'>): string[];
@@ -0,0 +1,96 @@
1
+ /** The lookup of a bundle, shared by the federation and event type checks. */
2
+ export function lookupOf(bundle) {
3
+ const federations = new Map(bundle.federations.map((f) => [f.id, f]));
4
+ return {
5
+ federations,
6
+ disciplines: new Map(bundle.disciplines.map((d) => [d.id, d])),
7
+ faces: new Map((bundle.targetFaces ?? []).map((f) => [f.id, f])),
8
+ worldBody: (id) => federations.get(id)?.country === 'international',
9
+ };
10
+ }
11
+ /** A federation's international federation is a world body. */
12
+ function checkFederationReferences(federations, lookup, errors) {
13
+ for (const f of federations) {
14
+ const where = `federations/${f.id}.json`;
15
+ if (f.internationalFederation && !lookup.worldBody(f.internationalFederation))
16
+ errors.push(`${where}: internationalFederation "${f.internationalFederation}" is no international federation`);
17
+ }
18
+ }
19
+ /** A discipline's federation exists, and its international equivalent belongs to a world body. */
20
+ function checkDisciplineReferences(disciplines, lookup, errors) {
21
+ for (const d of disciplines) {
22
+ const where = `disciplines/${d.id}.json`;
23
+ if (!lookup.federations.has(d.federation))
24
+ errors.push(`${where}: unknown federation "${d.federation}"`);
25
+ if (d.internationalDiscipline) {
26
+ const other = lookup.disciplines.get(d.internationalDiscipline);
27
+ if (!other)
28
+ errors.push(`${where}: unknown international discipline "${d.internationalDiscipline}"`);
29
+ else if (!lookup.worldBody(other.federation))
30
+ errors.push(`${where}: internationalDiscipline "${d.internationalDiscipline}" is no international discipline`);
31
+ }
32
+ }
33
+ }
34
+ /** A face and each of its events that names a federation name a federation record. */
35
+ function checkFaceFederations(faces, lookup, errors) {
36
+ for (const f of faces) {
37
+ const where = `target-faces/${f.id}.json`;
38
+ if (!f.federation)
39
+ errors.push(`${where}: needs a federation`);
40
+ else if (!lookup.federations.has(f.federation))
41
+ errors.push(`${where}: unknown federation "${f.federation}"`);
42
+ for (const e of f.events)
43
+ if (e.federation && !lookup.federations.has(e.federation))
44
+ errors.push(`${where}: event "${e.id}": unknown federation "${e.federation}"`);
45
+ }
46
+ }
47
+ /** At most one mapping (`mapped`: face event to mapping id), listed by the discipline's federation, with the mapping's gauge. */
48
+ function checkMappedEvent(m, { face, event, d }, mapped) {
49
+ const where = `target-mappings/${m.id}.json`;
50
+ const errors = [];
51
+ const key = `${face.id} ${m.event}`;
52
+ if (mapped.has(key))
53
+ errors.push(`${where}: event "${m.event}" is also in ${mapped.get(key)}`);
54
+ else
55
+ mapped.set(key, m.id);
56
+ const listedBy = event.federation ?? face.federation;
57
+ if (d && listedBy && listedBy !== d.federation)
58
+ errors.push(`${where}: event "${m.event}" is not one of federation "${d.federation}"`);
59
+ if (m.gauge && event.gauge && m.gauge.value !== event.gauge.value)
60
+ errors.push(`${where}: gauge ${m.gauge.value} mm differs from the event's ${event.gauge.value} mm`);
61
+ return errors;
62
+ }
63
+ /** A mapping names a known discipline and an event of a known face (see {@link checkMappedEvent}). */
64
+ function checkMapping(m, lookup, mapped) {
65
+ const where = `target-mappings/${m.id}.json`;
66
+ const d = lookup.disciplines.get(m.discipline);
67
+ const face = lookup.faces.get(m.face);
68
+ const errors = d ? [] : [`${where}: unknown discipline "${m.discipline}"`];
69
+ if (!face)
70
+ return [...errors, `${where}: unknown face "${m.face}"`];
71
+ const event = face.events.find((e) => e.id === m.event);
72
+ if (!event)
73
+ return [...errors, `${where}: face "${m.face}" has no event "${m.event}"`];
74
+ return [...errors, ...checkMappedEvent(m, { face, event, d }, mapped)];
75
+ }
76
+ /** Every mapping is checked, and every face event has at most one mapping. */
77
+ function checkMappings(mappings, lookup, errors) {
78
+ const mapped = new Map();
79
+ for (const m of mappings)
80
+ errors.push(...checkMapping(m, lookup, mapped));
81
+ }
82
+ /** Every id names a record; a discipline's international equivalent belongs to an international federation; a mapping names an event of its face that the discipline's federation lists, with that event's gauge; each face event has at most one mapping, each discipline at least one. */
83
+ export function checkFederations(bundle) {
84
+ const errors = [];
85
+ const lookup = lookupOf(bundle);
86
+ checkFederationReferences(bundle.federations, lookup, errors);
87
+ checkFaceFederations(bundle.targetFaces, lookup, errors);
88
+ checkDisciplineReferences(bundle.disciplines, lookup, errors);
89
+ checkMappings(bundle.targetMappings, lookup, errors);
90
+ const used = new Set(bundle.targetMappings.map((m) => m.discipline));
91
+ for (const d of bundle.disciplines)
92
+ if (!used.has(d.id))
93
+ errors.push(`disciplines/${d.id}.json: no target mapping names it`);
94
+ return errors;
95
+ }
96
+ //# sourceMappingURL=federations.js.map
@@ -0,0 +1,10 @@
1
+ export * from './catalogue.ts';
2
+ export * from './coverage.ts';
3
+ export * from './data-set.ts';
4
+ export * from './data-set-rule.ts';
5
+ export * from './load-tables.ts';
6
+ export * from './maker-rows.ts';
7
+ export * from './plausibility.ts';
8
+ export * from './published-loads.ts';
9
+ export * from './records.ts';
10
+ export * from './sources.ts';