-
Notifications
You must be signed in to change notification settings - Fork 121
/
Vocabularies.ts
121 lines (101 loc) · 3.77 KB
/
Vocabularies.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* eslint-disable @typescript-eslint/naming-convention, function-paren-newline */
import { namedNode } from '@rdfjs/data-model';
import type { NamedNode } from 'rdf-js';
type PrefixResolver<T> = (localName: string) => T;
type RecordOf<TKey extends any[], TValue> = Record<TKey[number], TValue>;
export type Namespace<TKey extends any[], TValue> =
PrefixResolver<TValue> & RecordOf<TKey, TValue>;
/**
* Creates a function that expands local names from the given base URI,
* and exports the given local names as properties on the returned object.
*/
export const createNamespace = <TKey extends string, TValue>(
baseUri: string,
toValue: (expanded: string) => TValue,
...localNames: TKey[]):
Namespace<typeof localNames, TValue> => {
// Create a function that expands local names
const expanded = {} as Record<string, TValue>;
const namespace = ((localName: string): TValue => {
if (!(localName in expanded)) {
expanded[localName] = toValue(`${baseUri}${localName}`);
}
return expanded[localName];
}) as Namespace<typeof localNames, TValue>;
// Expose the listed local names as properties
for (const localName of localNames) {
(namespace as RecordOf<typeof localNames, TValue>)[localName] = namespace(localName);
}
return namespace;
};
/**
* Creates a function that expands local names from the given base URI into strings,
* and exports the given local names as properties on the returned object.
*/
export const createUriNamespace = <T extends string>(baseUri: string, ...localNames: T[]):
Namespace<typeof localNames, string> =>
createNamespace(baseUri, (expanded): string => expanded, ...localNames);
/**
* Creates a function that expands local names from the given base URI into named nodes,
* and exports the given local names as properties on the returned object.
*/
export const createTermNamespace = <T extends string>(baseUri: string, ...localNames: T[]):
Namespace<typeof localNames, NamedNode> =>
createNamespace(baseUri, namedNode, ...localNames);
/**
* Creates a function that expands local names from the given base URI into string,
* and exports the given local names as properties on the returned object.
* Under the `terms` property, it exposes the expanded local names as named nodes.
*/
export const createUriAndTermNamespace = <T extends string>(baseUri: string, ...localNames: T[]):
Namespace<typeof localNames, string> & { terms: Namespace<typeof localNames, NamedNode> } =>
Object.assign(createUriNamespace(baseUri, ...localNames),
{ terms: createTermNamespace(baseUri, ...localNames) });
export const ACL = createUriAndTermNamespace('http://www.w3.org/ns/auth/acl#',
'accessTo',
'agent',
'agentClass',
'default',
'mode',
'Write',
'Read',
'Append',
'Control',
);
export const DC = createUriAndTermNamespace('http://purl.org/dc/terms/',
'modified',
);
export const FOAF = createUriAndTermNamespace('http://xmlns.com/foaf/0.1/',
'Agent',
'AuthenticatedAgent',
);
export const HTTP = createUriAndTermNamespace('urn:solid:http:',
'location',
'slug',
);
export const LDP = createUriAndTermNamespace('http://www.w3.org/ns/ldp#',
'contains',
'BasicContainer',
'Container',
'Resource',
);
export const MA = createUriAndTermNamespace('http://www.w3.org/ns/ma-ont#',
'format',
);
export const PIM = createUriAndTermNamespace('http://www.w3.org/ns/pim/space#',
'Storage',
);
export const POSIX = createUriAndTermNamespace('http://www.w3.org/ns/posix/stat#',
'mtime',
'size',
);
export const RDF = createUriAndTermNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'type',
);
export const XSD = createUriAndTermNamespace('http://www.w3.org/2001/XMLSchema#',
'dateTime',
'integer',
);
// Alias for most commonly used URI
export const CONTENT_TYPE = MA.format;
export const CONTENT_TYPE_TERM = MA.terms.format;