-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
namespace.ts
39 lines (35 loc) · 1008 Bytes
/
namespace.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
type NamespacePrototype = {
iri: string;
prefix: string;
terms: readonly string[];
};
type NamespacePrefix<Namespace extends NamespacePrototype> =
Namespace["prefix"];
type NamespaceIri<Namespace extends NamespacePrototype> = Namespace["iri"];
type NamespaceObject<Namespace extends NamespacePrototype> = {
[Term in Namespace["terms"][number]]: `${NamespacePrefix<Namespace>}${Term}`;
};
export const createNamespace = <
N extends NamespacePrototype,
I = NamespaceIri<N>,
P = NamespacePrefix<N>,
O = NamespaceObject<N>
>(
namespaceSpec: N
) =>
Object.assign(
//<X extends I>(f: [X]) =>
// `${namespaceSpec.prefix}:${f}` as `${string & P}${string & X}`,
namespaceSpec.terms.reduce((acc, term) => {
//acc[term] = `${namespaceSpec.prefix}${term}`
acc[term] = `${namespaceSpec.iri}${term}`;
return acc;
}, {} as any),
{
$prefix: namespaceSpec["prefix"],
$iri: namespaceSpec["iri"],
}
) as O & {
$prefix: P;
$iri: I;
};