-
Notifications
You must be signed in to change notification settings - Fork 4
/
memoizedNamespace.ts
45 lines (38 loc) · 1.48 KB
/
memoizedNamespace.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
import rdfFactory, { Namespace } from "@ontologies/core";
import { NamedNode, Term } from "../rdf";
import { NamespaceMap } from "../types";
const CI_MATCH_PREFIX = 0;
const CI_MATCH_SUFFIX = 1;
/**
* Expands a property if it's in short-form while preserving long-form.
* Note: The vocabulary needs to be present in the store prefix mapping
* @param prop The short- or long-form property
* @param namespaces Object of namespaces by their abbreviation.
* @returns The (expanded) property
*/
export function expandProperty(prop: NamedNode | Term | string | undefined,
namespaces: NamespaceMap = {}): NamedNode | undefined {
if (!prop) {
return prop as undefined;
}
if (typeof prop !== "string"
&& Object.prototype.hasOwnProperty.call(prop, "termType")
&& (prop as Term).termType === "NamedNode") {
return rdfFactory.namedNode(prop.value);
}
if (typeof prop === "object") {
if (prop.termType === "NamedNode") {
return rdfFactory.namedNode(prop.value);
}
return undefined;
}
if (prop.indexOf("/") >= 1) {
if (prop.startsWith("<") && prop.endsWith(">")) {
return rdfFactory.namedNode(prop.slice(1, -1));
}
return rdfFactory.namedNode(prop);
}
const matches = prop.split(":");
const constructor: Namespace | undefined = namespaces[matches[CI_MATCH_PREFIX]]?.ns;
return constructor && constructor(matches[CI_MATCH_SUFFIX]);
}