-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty.ts
More file actions
61 lines (54 loc) · 2.25 KB
/
pretty.ts
File metadata and controls
61 lines (54 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { CatalogEntity, CatalogRecord, SearchCatalogResult, SearchEntityResult } from './types';
import { prettyResult } from './result/pretty';
function sourceLabel(record: CatalogRecord): string {
const { source: inner, graph } = record.source;
const graphPart = graph ? ` (graph: ${graph})` : '';
const base = typeof inner === 'function'
? 'custom queryable'
: inner.type === 'sparql'
? `sparql @ ${inner.endpoint}`
: `rdf @ ${inner.url}`;
return `${base}${graphPart}`;
}
function prettyCatalogEntity(entity: CatalogEntity): string[] {
const lines = [` · ${entity.entityType.padEnd(20)} ${entity.entity}`];
if (entity.shapes.length > 0)
lines.push(` shapes: ${entity.shapes.map(s => s.name).join(', ')}`);
return lines;
}
function prettyCatalogRecord(record: CatalogRecord): string[] {
const lines = ['', `[${record.id}] ${sourceLabel(record)}`];
if (record.entities.length === 0) {
lines.push(' (no entities)');
} else {
lines.push(` ${record.entities.length} entit${record.entities.length !== 1 ? 'ies' : 'y'}:`);
for (const entity of record.entities)
lines.push(...prettyCatalogEntity(entity));
}
return lines;
}
export function prettyCatalogStore(store: CatalogRecord[]): string {
const lines = [`=== Catalog Store (${store.length} source${store.length !== 1 ? 's' : ''}) ===`];
for (const record of store)
lines.push(...prettyCatalogRecord(record));
return lines.join('\n');
}
function prettySearchEntityResult(result: SearchEntityResult): string[] {
return [` [${result.entityType}] ${result.entity}`, ...prettyResult(result.result)];
}
function prettySearchCatalogResult(result: SearchCatalogResult): string[] {
const lines = ['', ` catalog: ${result.catalog}`];
for (const entity of result.entities)
lines.push(...prettySearchEntityResult(entity));
return lines;
}
export function prettySearchResults(starPatternNames: string[], results: SearchCatalogResult[]): string {
const lines = [`=== /search ===`, `Query star patterns: ${starPatternNames.join(', ') || '(none)'}`];
if (results.length === 0) {
lines.push(' (no matches)');
} else {
for (const result of results)
lines.push(...prettySearchCatalogResult(result));
}
return lines.join('\n');
}