@ldo/jsonld-dataset-proxy 0.0.1-alpha.14
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.
- package/README.md +809 -0
- package/dist/ContextUtil.d.ts +12 -0
- package/dist/ContextUtil.js +70 -0
- package/dist/ContextUtil.js.map +1 -0
- package/dist/JsonldDatasetProxyBuilder.d.ts +15 -0
- package/dist/JsonldDatasetProxyBuilder.js +41 -0
- package/dist/JsonldDatasetProxyBuilder.js.map +1 -0
- package/dist/ProxyContext.d.ts +31 -0
- package/dist/ProxyContext.js +65 -0
- package/dist/ProxyContext.js.map +1 -0
- package/dist/arrayProxy/ArrayProxy.d.ts +12 -0
- package/dist/arrayProxy/ArrayProxy.js +3 -0
- package/dist/arrayProxy/ArrayProxy.js.map +1 -0
- package/dist/arrayProxy/arrayMethods.d.ts +17 -0
- package/dist/arrayProxy/arrayMethods.js +161 -0
- package/dist/arrayProxy/arrayMethods.js.map +1 -0
- package/dist/arrayProxy/createArrayHandler.d.ts +9 -0
- package/dist/arrayProxy/createArrayHandler.js +140 -0
- package/dist/arrayProxy/createArrayHandler.js.map +1 -0
- package/dist/arrayProxy/isArrayProxy.d.ts +2 -0
- package/dist/arrayProxy/isArrayProxy.js +17 -0
- package/dist/arrayProxy/isArrayProxy.js.map +1 -0
- package/dist/arrayProxy/modifyArray.d.ts +12 -0
- package/dist/arrayProxy/modifyArray.js +73 -0
- package/dist/arrayProxy/modifyArray.js.map +1 -0
- package/dist/graphOf.d.ts +3 -0
- package/dist/graphOf.js +37 -0
- package/dist/graphOf.js.map +1 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonldDatasetProxy.d.ts +4 -0
- package/dist/jsonldDatasetProxy.js +19 -0
- package/dist/jsonldDatasetProxy.js.map +1 -0
- package/dist/language/languageMapProxy.d.ts +4 -0
- package/dist/language/languageMapProxy.js +55 -0
- package/dist/language/languageMapProxy.js.map +1 -0
- package/dist/language/languageSet.d.ts +23 -0
- package/dist/language/languageSet.js +75 -0
- package/dist/language/languageSet.js.map +1 -0
- package/dist/language/languageTypes.d.ts +2 -0
- package/dist/language/languageTypes.js +3 -0
- package/dist/language/languageTypes.js.map +1 -0
- package/dist/language/languageUtils.d.ts +14 -0
- package/dist/language/languageUtils.js +73 -0
- package/dist/language/languageUtils.js.map +1 -0
- package/dist/language/languagesOf.d.ts +12 -0
- package/dist/language/languagesOf.js +16 -0
- package/dist/language/languagesOf.js.map +1 -0
- package/dist/setLanguagePreferences.d.ts +3 -0
- package/dist/setLanguagePreferences.js +9 -0
- package/dist/setLanguagePreferences.js.map +1 -0
- package/dist/subjectProxy/SubjectProxy.d.ts +14 -0
- package/dist/subjectProxy/SubjectProxy.js +3 -0
- package/dist/subjectProxy/SubjectProxy.js.map +1 -0
- package/dist/subjectProxy/createSubjectHandler.d.ts +6 -0
- package/dist/subjectProxy/createSubjectHandler.js +73 -0
- package/dist/subjectProxy/createSubjectHandler.js.map +1 -0
- package/dist/subjectProxy/deleteFromDataset.d.ts +3 -0
- package/dist/subjectProxy/deleteFromDataset.js +44 -0
- package/dist/subjectProxy/deleteFromDataset.js.map +1 -0
- package/dist/subjectProxy/getValueForKey.d.ts +5 -0
- package/dist/subjectProxy/getValueForKey.js +43 -0
- package/dist/subjectProxy/getValueForKey.js.map +1 -0
- package/dist/subjectProxy/isSubjectProxy.d.ts +4 -0
- package/dist/subjectProxy/isSubjectProxy.js +25 -0
- package/dist/subjectProxy/isSubjectProxy.js.map +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/util/NodeSet.d.ts +11 -0
- package/dist/util/NodeSet.js +45 -0
- package/dist/util/NodeSet.js.map +1 -0
- package/dist/util/RawObject.d.ts +8 -0
- package/dist/util/RawObject.js +3 -0
- package/dist/util/RawObject.js.map +1 -0
- package/dist/util/addObjectToDataset.d.ts +8 -0
- package/dist/util/addObjectToDataset.js +83 -0
- package/dist/util/addObjectToDataset.js.map +1 -0
- package/dist/util/createInteractOptions.d.ts +6 -0
- package/dist/util/createInteractOptions.js +37 -0
- package/dist/util/createInteractOptions.js.map +1 -0
- package/dist/util/getNodeFromRaw.d.ts +6 -0
- package/dist/util/getNodeFromRaw.js +41 -0
- package/dist/util/getNodeFromRaw.js.map +1 -0
- package/dist/util/isProxy.d.ts +5 -0
- package/dist/util/isProxy.js +17 -0
- package/dist/util/isProxy.js.map +1 -0
- package/dist/util/nodeToJsonldRepresentation.d.ts +6 -0
- package/dist/util/nodeToJsonldRepresentation.js +74 -0
- package/dist/util/nodeToJsonldRepresentation.js.map +1 -0
- package/dist/write.d.ts +3 -0
- package/dist/write.js +9 -0
- package/dist/write.js.map +1 -0
- package/package.json +53 -0
- package/src/ContextUtil.ts +90 -0
- package/src/JsonldDatasetProxyBuilder.ts +106 -0
- package/src/ProxyContext.ts +110 -0
- package/src/arrayProxy/ArrayProxy.ts +20 -0
- package/src/arrayProxy/arrayMethods.ts +219 -0
- package/src/arrayProxy/createArrayHandler.ts +177 -0
- package/src/arrayProxy/isArrayProxy.ts +23 -0
- package/src/arrayProxy/modifyArray.ts +129 -0
- package/src/graphOf.ts +63 -0
- package/src/index.ts +37 -0
- package/src/jsonldDatasetProxy.ts +27 -0
- package/src/language/languageMapProxy.ts +75 -0
- package/src/language/languageSet.ts +129 -0
- package/src/language/languageTypes.ts +3 -0
- package/src/language/languageUtils.ts +159 -0
- package/src/language/languagesOf.ts +61 -0
- package/src/setLanguagePreferences.ts +14 -0
- package/src/subjectProxy/SubjectProxy.ts +20 -0
- package/src/subjectProxy/createSubjectHandler.ts +106 -0
- package/src/subjectProxy/deleteFromDataset.ts +44 -0
- package/src/subjectProxy/getValueForKey.ts +62 -0
- package/src/subjectProxy/isSubjectProxy.ts +30 -0
- package/src/types.ts +11 -0
- package/src/util/NodeSet.ts +47 -0
- package/src/util/RawObject.ts +13 -0
- package/src/util/addObjectToDataset.ts +141 -0
- package/src/util/createInteractOptions.ts +55 -0
- package/src/util/getNodeFromRaw.ts +45 -0
- package/src/util/isProxy.ts +20 -0
- package/src/util/nodeToJsonldRepresentation.ts +76 -0
- package/src/write.ts +12 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ObjectLike } from "../types";
|
|
2
|
+
import {
|
|
3
|
+
_getUnderlyingDataset,
|
|
4
|
+
_getUnderlyingNode,
|
|
5
|
+
_proxyContext,
|
|
6
|
+
_writeGraphs,
|
|
7
|
+
} from "../types";
|
|
8
|
+
import type { SubjectProxy } from "./SubjectProxy";
|
|
9
|
+
|
|
10
|
+
export function isSubjectProxy(
|
|
11
|
+
someObject?: unknown,
|
|
12
|
+
): someObject is SubjectProxy {
|
|
13
|
+
if (!someObject) return false;
|
|
14
|
+
if (typeof someObject !== "object") return false;
|
|
15
|
+
const potentialSubjectProxy = someObject as SubjectProxy;
|
|
16
|
+
return !(
|
|
17
|
+
typeof potentialSubjectProxy[_writeGraphs] !== "object" ||
|
|
18
|
+
typeof potentialSubjectProxy[_getUnderlyingDataset] !== "object" ||
|
|
19
|
+
typeof potentialSubjectProxy[_getUnderlyingNode] !== "object" ||
|
|
20
|
+
typeof potentialSubjectProxy[_proxyContext] !== "object"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getSubjectProxyFromObject(object: ObjectLike): SubjectProxy {
|
|
25
|
+
const potentialSubjectProxy = object as SubjectProxy;
|
|
26
|
+
if (!isSubjectProxy(potentialSubjectProxy)) {
|
|
27
|
+
throw new Error(`${object} is not a Jsonld Dataset Proxy Subject`);
|
|
28
|
+
}
|
|
29
|
+
return potentialSubjectProxy;
|
|
30
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const _getUnderlyingNode = Symbol("_getUnderlyingNode");
|
|
2
|
+
export const _getUnderlyingMatch = Symbol("_getUnderlyingMatch");
|
|
3
|
+
export const _isSubjectOriented = Symbol("_isSubjectOriented");
|
|
4
|
+
export const _getNodeAtIndex = Symbol("_getNodeAtIndex");
|
|
5
|
+
export const _getUnderlyingDataset = Symbol("_getUnderlyingDataset");
|
|
6
|
+
export const _getUnderlyingArrayTarget = Symbol("_getUnderlyingArrayTarget");
|
|
7
|
+
export const _proxyContext = Symbol("_proxyContext");
|
|
8
|
+
export const _writeGraphs = Symbol("_writeGraphs");
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
export type ObjectLike = Record<string | number | symbol, any>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { BlankNode, DefaultGraph, Literal, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import type { ObjectNode } from "@ldo/rdf-utils";
|
|
3
|
+
|
|
4
|
+
export function nodeToString(
|
|
5
|
+
node: NamedNode | BlankNode | DefaultGraph | Literal | null | undefined,
|
|
6
|
+
): string {
|
|
7
|
+
if (node == null) {
|
|
8
|
+
return "null";
|
|
9
|
+
}
|
|
10
|
+
switch (node.termType) {
|
|
11
|
+
case "NamedNode":
|
|
12
|
+
return `namedNode(${node.value})`;
|
|
13
|
+
case "BlankNode":
|
|
14
|
+
return `blankNode(${node.value})`;
|
|
15
|
+
case "Literal":
|
|
16
|
+
return `literal(${node.value},${node.datatype.value})`;
|
|
17
|
+
case "DefaultGraph":
|
|
18
|
+
return "defaultGraph()";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class NodeSet {
|
|
23
|
+
private set: Set<string> = new Set();
|
|
24
|
+
private map: Record<string, ObjectNode> = {};
|
|
25
|
+
|
|
26
|
+
add(node: ObjectNode) {
|
|
27
|
+
const key = nodeToString(node);
|
|
28
|
+
this.set.add(key);
|
|
29
|
+
this.map[key] = node;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
has(node: ObjectNode): boolean {
|
|
33
|
+
return this.set.has(nodeToString(node));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
delete(node: ObjectNode) {
|
|
37
|
+
const key = nodeToString(node);
|
|
38
|
+
delete this.map[key];
|
|
39
|
+
return this.set.delete(nodeToString(node));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
toArray() {
|
|
43
|
+
return Array.from(this.set).map((stringVal) => {
|
|
44
|
+
return this.map[stringVal];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { _getUnderlyingNode } from "../types";
|
|
3
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
4
|
+
|
|
5
|
+
export type RawObject =
|
|
6
|
+
| ({
|
|
7
|
+
"@id"?: string | NamedNode | BlankNode;
|
|
8
|
+
} & {
|
|
9
|
+
[key: string | symbol | number]: RawValue | RawValue[];
|
|
10
|
+
})
|
|
11
|
+
| SubjectProxy;
|
|
12
|
+
|
|
13
|
+
export type RawValue = string | boolean | number | RawObject | undefined;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { literal, namedNode, quad } from "@rdfjs/data-model";
|
|
3
|
+
import { _getUnderlyingNode } from "../types";
|
|
4
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
5
|
+
import { getNodeFromRawObject, getNodeFromRawValue } from "./getNodeFromRaw";
|
|
6
|
+
import type { RawObject, RawValue } from "./RawObject";
|
|
7
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
8
|
+
import { isSubjectProxy } from "../subjectProxy/isSubjectProxy";
|
|
9
|
+
import { NodeSet } from "./NodeSet";
|
|
10
|
+
import {
|
|
11
|
+
getLanguageKeyForWriteOperation,
|
|
12
|
+
languageDeleteMatch,
|
|
13
|
+
languageKeyToLiteralLanguage,
|
|
14
|
+
} from "../language/languageUtils";
|
|
15
|
+
|
|
16
|
+
export function addRawValueToDatasetRecursive(
|
|
17
|
+
subject: NamedNode | BlankNode,
|
|
18
|
+
key: string,
|
|
19
|
+
value: RawValue,
|
|
20
|
+
visitedObjects: NodeSet,
|
|
21
|
+
shouldDeleteOldTriples: boolean,
|
|
22
|
+
proxyContext: ProxyContext,
|
|
23
|
+
): void {
|
|
24
|
+
const { dataset, contextUtil } = proxyContext;
|
|
25
|
+
const predicate = namedNode(contextUtil.keyToIri(key));
|
|
26
|
+
// Get the Object Node
|
|
27
|
+
const object = getNodeFromRawValue(key, value, proxyContext);
|
|
28
|
+
if (object == undefined) {
|
|
29
|
+
dataset.deleteMatches(subject, predicate);
|
|
30
|
+
} else if (object.termType === "Literal") {
|
|
31
|
+
let languageAppliedObject = object;
|
|
32
|
+
// Handle language use case
|
|
33
|
+
if (contextUtil.isLangString(key)) {
|
|
34
|
+
const languageKey = getLanguageKeyForWriteOperation(
|
|
35
|
+
proxyContext.languageOrdering,
|
|
36
|
+
);
|
|
37
|
+
if (!languageKey) return;
|
|
38
|
+
languageAppliedObject = literal(
|
|
39
|
+
object.value,
|
|
40
|
+
languageKeyToLiteralLanguage(languageKey),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
proxyContext.writeGraphs.forEach((graph) => {
|
|
44
|
+
proxyContext.dataset.add(
|
|
45
|
+
quad(subject, predicate, languageAppliedObject, graph),
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
} else {
|
|
49
|
+
// Delete any triples if the id is the same
|
|
50
|
+
// if (!visitedObjects.has(object) && !isSubjectProxy(value)) {
|
|
51
|
+
// console.log("deleting 2", object.value);
|
|
52
|
+
// dataset.deleteMatches(object, undefined, undefined);
|
|
53
|
+
// }
|
|
54
|
+
proxyContext.writeGraphs.forEach((graph) => {
|
|
55
|
+
dataset.add(quad(subject, predicate, object, graph));
|
|
56
|
+
});
|
|
57
|
+
if (!isSubjectProxy(value)) {
|
|
58
|
+
const updateData: RawObject = (
|
|
59
|
+
typeof value === "object"
|
|
60
|
+
? { ...value, "@id": object }
|
|
61
|
+
: { "@id": object }
|
|
62
|
+
) as RawObject;
|
|
63
|
+
addRawObjectToDatasetRecursive(
|
|
64
|
+
updateData,
|
|
65
|
+
visitedObjects,
|
|
66
|
+
shouldDeleteOldTriples,
|
|
67
|
+
proxyContext,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function addRawObjectToDatasetRecursive(
|
|
74
|
+
item: RawObject,
|
|
75
|
+
visitedObjects: NodeSet,
|
|
76
|
+
shouldDeleteOldTriples: boolean,
|
|
77
|
+
proxyContext: ProxyContext,
|
|
78
|
+
): SubjectProxy {
|
|
79
|
+
if (isSubjectProxy(item)) {
|
|
80
|
+
return item as SubjectProxy;
|
|
81
|
+
}
|
|
82
|
+
const { dataset } = proxyContext;
|
|
83
|
+
const subject = getNodeFromRawObject(item, proxyContext.contextUtil);
|
|
84
|
+
if (visitedObjects.has(subject)) {
|
|
85
|
+
return proxyContext.createSubjectProxy(subject);
|
|
86
|
+
}
|
|
87
|
+
visitedObjects.add(subject);
|
|
88
|
+
Object.entries(item).forEach(([key, value]) => {
|
|
89
|
+
if (key === "@id") {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const predicate = namedNode(proxyContext.contextUtil.keyToIri(key));
|
|
93
|
+
if (shouldDeleteOldTriples) {
|
|
94
|
+
if (proxyContext.contextUtil.isLangString(key)) {
|
|
95
|
+
const languageKey = getLanguageKeyForWriteOperation(
|
|
96
|
+
proxyContext.languageOrdering,
|
|
97
|
+
);
|
|
98
|
+
if (languageKey) {
|
|
99
|
+
languageDeleteMatch(dataset, subject, predicate, languageKey);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
dataset.deleteMatches(subject, predicate);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (Array.isArray(value)) {
|
|
106
|
+
value.forEach((valueItem) => {
|
|
107
|
+
addRawValueToDatasetRecursive(
|
|
108
|
+
subject,
|
|
109
|
+
key,
|
|
110
|
+
valueItem,
|
|
111
|
+
visitedObjects,
|
|
112
|
+
true,
|
|
113
|
+
proxyContext,
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
} else {
|
|
117
|
+
addRawValueToDatasetRecursive(
|
|
118
|
+
subject,
|
|
119
|
+
key,
|
|
120
|
+
value as RawValue,
|
|
121
|
+
visitedObjects,
|
|
122
|
+
true,
|
|
123
|
+
proxyContext,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return proxyContext.createSubjectProxy(subject);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function addObjectToDataset(
|
|
131
|
+
item: RawObject,
|
|
132
|
+
shouldDeleteOldTriples: boolean,
|
|
133
|
+
proxyContext: ProxyContext,
|
|
134
|
+
): SubjectProxy {
|
|
135
|
+
return addRawObjectToDatasetRecursive(
|
|
136
|
+
item,
|
|
137
|
+
new NodeSet(),
|
|
138
|
+
shouldDeleteOldTriples,
|
|
139
|
+
proxyContext,
|
|
140
|
+
);
|
|
141
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getSubjectProxyFromObject } from "../subjectProxy/isSubjectProxy";
|
|
2
|
+
import type { ObjectLike } from "../types";
|
|
3
|
+
import { _getUnderlyingNode, _proxyContext } from "../types";
|
|
4
|
+
import { getProxyFromObject } from "./isProxy";
|
|
5
|
+
|
|
6
|
+
export interface InteractOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Given a dataset proxy, this will set the action on that dataset proxy
|
|
9
|
+
* @param objects Any number of dataset proxies
|
|
10
|
+
* @returns An end function. Call this if to reset the interaction
|
|
11
|
+
*/
|
|
12
|
+
using(...objects: ObjectLike[]): () => void;
|
|
13
|
+
/**
|
|
14
|
+
* Given a dataset proxy this will copy the dataset proxy and set the action
|
|
15
|
+
* on the copy
|
|
16
|
+
* @param objects Any number of dataset proxies
|
|
17
|
+
* @returns cloned dataset proxies
|
|
18
|
+
*/
|
|
19
|
+
usingCopy<T extends ObjectLike>(...objects: T[]): T[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createInteractOptions(
|
|
23
|
+
paramKey: string,
|
|
24
|
+
parameter: unknown,
|
|
25
|
+
): InteractOptions {
|
|
26
|
+
return {
|
|
27
|
+
using(...objects: ObjectLike[]): () => void {
|
|
28
|
+
const onEndFunctions: (() => void)[] = [];
|
|
29
|
+
objects.forEach((object) => {
|
|
30
|
+
const proxy = getProxyFromObject(object);
|
|
31
|
+
const oldProxyContext = proxy[_proxyContext];
|
|
32
|
+
proxy[_proxyContext] = proxy[_proxyContext].duplicate({
|
|
33
|
+
[paramKey]: parameter,
|
|
34
|
+
});
|
|
35
|
+
onEndFunctions.push(() => {
|
|
36
|
+
proxy[_proxyContext] = oldProxyContext;
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
return function endWrite() {
|
|
40
|
+
onEndFunctions.forEach((func) => func());
|
|
41
|
+
};
|
|
42
|
+
},
|
|
43
|
+
usingCopy<T extends ObjectLike>(...objects: T[]): T[] {
|
|
44
|
+
return objects.map((object) => {
|
|
45
|
+
const proxy = getSubjectProxyFromObject(object);
|
|
46
|
+
const newProxyContext = proxy[_proxyContext].duplicate({
|
|
47
|
+
[paramKey]: parameter,
|
|
48
|
+
});
|
|
49
|
+
return newProxyContext.createSubjectProxy(
|
|
50
|
+
proxy[_getUnderlyingNode],
|
|
51
|
+
) as unknown as T;
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { BlankNode, Literal, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { namedNode, literal, blankNode } from "@rdfjs/data-model";
|
|
3
|
+
import type { ContextUtil } from "../ContextUtil";
|
|
4
|
+
import { _getUnderlyingNode } from "../types";
|
|
5
|
+
import type { RawObject, RawValue } from "./RawObject";
|
|
6
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
7
|
+
|
|
8
|
+
export function getNodeFromRawObject(
|
|
9
|
+
item: RawObject,
|
|
10
|
+
contextUtil: ContextUtil,
|
|
11
|
+
): NamedNode | BlankNode {
|
|
12
|
+
if (item[_getUnderlyingNode]) {
|
|
13
|
+
return item[_getUnderlyingNode] as NamedNode | BlankNode;
|
|
14
|
+
} else if (!item["@id"]) {
|
|
15
|
+
return blankNode();
|
|
16
|
+
} else if (typeof item["@id"] === "string") {
|
|
17
|
+
return namedNode(contextUtil.keyToIri(item["@id"]));
|
|
18
|
+
} else {
|
|
19
|
+
return item["@id"];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getNodeFromRawValue(
|
|
24
|
+
key: string,
|
|
25
|
+
value: RawValue,
|
|
26
|
+
proxyContext: ProxyContext,
|
|
27
|
+
): BlankNode | NamedNode | Literal | undefined {
|
|
28
|
+
// Get the Object Node
|
|
29
|
+
if (value == undefined) {
|
|
30
|
+
return undefined;
|
|
31
|
+
} else if (
|
|
32
|
+
typeof value === "string" ||
|
|
33
|
+
typeof value === "boolean" ||
|
|
34
|
+
typeof value === "number"
|
|
35
|
+
) {
|
|
36
|
+
const datatype = proxyContext.contextUtil.getType(key);
|
|
37
|
+
if (datatype === "@id") {
|
|
38
|
+
return namedNode(value.toString());
|
|
39
|
+
} else {
|
|
40
|
+
return literal(value.toString(), datatype);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
return getNodeFromRawObject(value, proxyContext.contextUtil);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ArrayProxy } from "../arrayProxy/ArrayProxy";
|
|
2
|
+
import { isArrayProxy } from "../arrayProxy/isArrayProxy";
|
|
3
|
+
import { isSubjectProxy } from "../subjectProxy/isSubjectProxy";
|
|
4
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
5
|
+
import type { ObjectLike } from "../types";
|
|
6
|
+
|
|
7
|
+
export function isProxy(
|
|
8
|
+
someObject?: unknown,
|
|
9
|
+
): someObject is ArrayProxy | SubjectProxy {
|
|
10
|
+
return isSubjectProxy(someObject) || isArrayProxy(someObject);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getProxyFromObject(
|
|
14
|
+
object: ObjectLike | ObjectLike[],
|
|
15
|
+
): SubjectProxy | ArrayProxy {
|
|
16
|
+
if (!isProxy(object)) {
|
|
17
|
+
throw new Error(`${object} is not a Jsonld Dataset Proxy`);
|
|
18
|
+
}
|
|
19
|
+
return object;
|
|
20
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { Literal, Quad_Object } from "@rdfjs/types";
|
|
2
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
3
|
+
import type { SubjectProxy } from "../subjectProxy/SubjectProxy";
|
|
4
|
+
|
|
5
|
+
export type ObjectJsonRepresentation = string | number | boolean | SubjectProxy;
|
|
6
|
+
|
|
7
|
+
export function literalToJsonldRepresentation(literal: Literal) {
|
|
8
|
+
switch (literal.datatype.value) {
|
|
9
|
+
case "http://www.w3.org/2001/XMLSchema#string":
|
|
10
|
+
case "http://www.w3.org/2001/XMLSchema#ENTITIES":
|
|
11
|
+
case "http://www.w3.org/2001/XMLSchema#ENTITY":
|
|
12
|
+
case "http://www.w3.org/2001/XMLSchema#ID":
|
|
13
|
+
case "http://www.w3.org/2001/XMLSchema#IDREF":
|
|
14
|
+
case "http://www.w3.org/2001/XMLSchema#IDREFS":
|
|
15
|
+
case "http://www.w3.org/2001/XMLSchema#language":
|
|
16
|
+
case "http://www.w3.org/2001/XMLSchema#Name":
|
|
17
|
+
case "http://www.w3.org/2001/XMLSchema#NCName":
|
|
18
|
+
case "http://www.w3.org/2001/XMLSchema#NMTOKEN":
|
|
19
|
+
case "http://www.w3.org/2001/XMLSchema#NMTOKENS":
|
|
20
|
+
case "http://www.w3.org/2001/XMLSchema#normalizedString":
|
|
21
|
+
case "http://www.w3.org/2001/XMLSchema#QName":
|
|
22
|
+
case "http://www.w3.org/2001/XMLSchema#token":
|
|
23
|
+
return literal.value;
|
|
24
|
+
case "http://www.w3.org/2001/XMLSchema#date":
|
|
25
|
+
case "http://www.w3.org/2001/XMLSchema#dateTime":
|
|
26
|
+
case "http://www.w3.org/2001/XMLSchema#duration":
|
|
27
|
+
case "http://www.w3.org/2001/XMLSchema#gDay":
|
|
28
|
+
case "http://www.w3.org/2001/XMLSchema#gMonth":
|
|
29
|
+
case "http://www.w3.org/2001/XMLSchema#gMonthDay":
|
|
30
|
+
case "http://www.w3.org/2001/XMLSchema#gYear":
|
|
31
|
+
case "http://www.w3.org/2001/XMLSchema#gYearMonth":
|
|
32
|
+
case "http://www.w3.org/2001/XMLSchema#time":
|
|
33
|
+
return literal.value;
|
|
34
|
+
case "http://www.w3.org/2001/XMLSchema#integer":
|
|
35
|
+
case "http://www.w3.org/2001/XMLSchema#byte":
|
|
36
|
+
case "http://www.w3.org/2001/XMLSchema#decimal":
|
|
37
|
+
case "http://www.w3.org/2001/XMLSchema#int":
|
|
38
|
+
case "http://www.w3.org/2001/XMLSchema#long":
|
|
39
|
+
case "http://www.w3.org/2001/XMLSchema#negativeInteger":
|
|
40
|
+
case "http://www.w3.org/2001/XMLSchema#nonNegativeInteger":
|
|
41
|
+
case "http://www.w3.org/2001/XMLSchema#nonPositiveInteger":
|
|
42
|
+
case "http://www.w3.org/2001/XMLSchema#positiveInteger":
|
|
43
|
+
case "http://www.w3.org/2001/XMLSchema#short":
|
|
44
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedLong":
|
|
45
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedInt":
|
|
46
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedShort":
|
|
47
|
+
case "http://www.w3.org/2001/XMLSchema#unsignedByte":
|
|
48
|
+
return parseFloat(literal.value);
|
|
49
|
+
case "http://www.w3.org/2001/XMLSchema#boolean":
|
|
50
|
+
return literal.value === "true";
|
|
51
|
+
case "http://www.w3.org/2001/XMLSchema#hexBinary":
|
|
52
|
+
return literal.value;
|
|
53
|
+
case "http://www.w3.org/2001/XMLSchema#anyURI":
|
|
54
|
+
return literal.value;
|
|
55
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML":
|
|
56
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#PlainLiteral":
|
|
57
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral":
|
|
58
|
+
case "http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON":
|
|
59
|
+
return literal.value;
|
|
60
|
+
default:
|
|
61
|
+
return literal.value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function nodeToJsonldRepresentation(
|
|
66
|
+
node: Quad_Object,
|
|
67
|
+
proxyContext: ProxyContext,
|
|
68
|
+
): string | number | boolean | SubjectProxy {
|
|
69
|
+
if (node.termType === "Literal") {
|
|
70
|
+
return literalToJsonldRepresentation(node);
|
|
71
|
+
} else if (node.termType === "NamedNode" || node.termType === "BlankNode") {
|
|
72
|
+
return proxyContext.createSubjectProxy(node);
|
|
73
|
+
} else {
|
|
74
|
+
throw new Error("Can only convert NamedNodes or Literals or BlankNodes");
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/write.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GraphNode } from "@ldo/rdf-utils";
|
|
2
|
+
import type { InteractOptions } from "./util/createInteractOptions";
|
|
3
|
+
import { createInteractOptions } from "./util/createInteractOptions";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Set the graphs that should be written to
|
|
7
|
+
* @param graphs The graphs that should be written to
|
|
8
|
+
* @returns a write builder
|
|
9
|
+
*/
|
|
10
|
+
export function write(...graphs: GraphNode[]): InteractOptions {
|
|
11
|
+
return createInteractOptions("writeGraphs", graphs);
|
|
12
|
+
}
|