@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,219 @@
|
|
|
1
|
+
import type { ArrayProxyTarget } from "./createArrayHandler";
|
|
2
|
+
import type { ObjectJsonRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
3
|
+
import { nodeToJsonldRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
4
|
+
import { modifyArray } from "./modifyArray";
|
|
5
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
6
|
+
|
|
7
|
+
export type methodBuilder<Return> = (
|
|
8
|
+
target: ArrayProxyTarget,
|
|
9
|
+
key: string,
|
|
10
|
+
proxyContext: ProxyContext,
|
|
11
|
+
) => Return;
|
|
12
|
+
|
|
13
|
+
export interface ArrayMethodBuildersType {
|
|
14
|
+
copyWithin: methodBuilder<Array<ObjectJsonRepresentation>["copyWithin"]>;
|
|
15
|
+
fill: methodBuilder<Array<ObjectJsonRepresentation>["fill"]>;
|
|
16
|
+
pop: methodBuilder<Array<ObjectJsonRepresentation>["pop"]>;
|
|
17
|
+
push: methodBuilder<Array<ObjectJsonRepresentation>["push"]>;
|
|
18
|
+
reverse: methodBuilder<Array<ObjectJsonRepresentation>["reverse"]>;
|
|
19
|
+
shift: methodBuilder<Array<ObjectJsonRepresentation>["shift"]>;
|
|
20
|
+
sort: methodBuilder<Array<ObjectJsonRepresentation>["sort"]>;
|
|
21
|
+
splice: methodBuilder<Array<ObjectJsonRepresentation>["splice"]>;
|
|
22
|
+
unshift: methodBuilder<Array<ObjectJsonRepresentation>["unshift"]>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const methodNames: Set<keyof ArrayMethodBuildersType> = new Set([
|
|
26
|
+
"copyWithin",
|
|
27
|
+
"fill",
|
|
28
|
+
"pop",
|
|
29
|
+
"push",
|
|
30
|
+
"reverse",
|
|
31
|
+
"shift",
|
|
32
|
+
"sort",
|
|
33
|
+
"splice",
|
|
34
|
+
"unshift",
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
export const arrayMethodsBuilders: ArrayMethodBuildersType = {
|
|
38
|
+
copyWithin: (target, key, proxyContext) => {
|
|
39
|
+
return (targetIndex, start, end) => {
|
|
40
|
+
return modifyArray(
|
|
41
|
+
{
|
|
42
|
+
target,
|
|
43
|
+
key,
|
|
44
|
+
quadsToDelete: (quads) => {
|
|
45
|
+
const oldQuads = [...quads];
|
|
46
|
+
const newQuadSet = new Set(
|
|
47
|
+
quads.copyWithin(targetIndex, start, end),
|
|
48
|
+
);
|
|
49
|
+
return oldQuads.filter((oldQuad) => !newQuadSet.has(oldQuad));
|
|
50
|
+
},
|
|
51
|
+
modifyCoreArray: (coreArray) => {
|
|
52
|
+
coreArray.copyWithin(targetIndex, start, end);
|
|
53
|
+
return proxyContext.createArrayProxy(
|
|
54
|
+
target[0],
|
|
55
|
+
target[2],
|
|
56
|
+
) as ObjectJsonRepresentation[];
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
proxyContext,
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
fill: (target, key, proxyContext) => {
|
|
64
|
+
return (value, start, end) => {
|
|
65
|
+
return modifyArray(
|
|
66
|
+
{
|
|
67
|
+
target,
|
|
68
|
+
key,
|
|
69
|
+
toAdd: [value],
|
|
70
|
+
quadsToDelete: (quads) => {
|
|
71
|
+
return quads.slice(start, end);
|
|
72
|
+
},
|
|
73
|
+
modifyCoreArray: (coreArray, addedValues) => {
|
|
74
|
+
coreArray.fill(addedValues[0], start, end);
|
|
75
|
+
return proxyContext.createArrayProxy(
|
|
76
|
+
target[0],
|
|
77
|
+
target[2],
|
|
78
|
+
) as ObjectJsonRepresentation[];
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
proxyContext,
|
|
82
|
+
);
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
pop: (target, key, proxyContext) => {
|
|
86
|
+
return () => {
|
|
87
|
+
return modifyArray(
|
|
88
|
+
{
|
|
89
|
+
target,
|
|
90
|
+
key,
|
|
91
|
+
quadsToDelete: (quads) => {
|
|
92
|
+
return quads[quads.length - 1] ? [quads[quads.length - 1]] : [];
|
|
93
|
+
},
|
|
94
|
+
modifyCoreArray: (coreArray) => {
|
|
95
|
+
const popped = coreArray.pop();
|
|
96
|
+
return popped
|
|
97
|
+
? nodeToJsonldRepresentation(popped, proxyContext)
|
|
98
|
+
: undefined;
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
proxyContext,
|
|
102
|
+
);
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
push: (target, key, proxyContext) => {
|
|
106
|
+
return (...args) => {
|
|
107
|
+
return modifyArray(
|
|
108
|
+
{
|
|
109
|
+
target,
|
|
110
|
+
key,
|
|
111
|
+
toAdd: args,
|
|
112
|
+
modifyCoreArray: (coreArray, addedValues) => {
|
|
113
|
+
coreArray.push(...addedValues);
|
|
114
|
+
return proxyContext.createArrayProxy(target[0], target[2]).length;
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
proxyContext,
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
reverse: (target, _key, proxyContext) => {
|
|
122
|
+
return () => {
|
|
123
|
+
target[1].reverse();
|
|
124
|
+
return proxyContext.createArrayProxy(
|
|
125
|
+
target[0],
|
|
126
|
+
target[2],
|
|
127
|
+
) as ObjectJsonRepresentation[];
|
|
128
|
+
};
|
|
129
|
+
},
|
|
130
|
+
shift: (target, key, proxyContext) => {
|
|
131
|
+
return () => {
|
|
132
|
+
return modifyArray(
|
|
133
|
+
{
|
|
134
|
+
target,
|
|
135
|
+
key,
|
|
136
|
+
quadsToDelete: (quads) => {
|
|
137
|
+
return quads[0] ? [quads[0]] : [];
|
|
138
|
+
},
|
|
139
|
+
modifyCoreArray: (coreArray) => {
|
|
140
|
+
const shifted = coreArray.shift();
|
|
141
|
+
return shifted
|
|
142
|
+
? nodeToJsonldRepresentation(shifted, proxyContext)
|
|
143
|
+
: undefined;
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
proxyContext,
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
},
|
|
150
|
+
sort: (target, _key, proxyContext) => {
|
|
151
|
+
return (compareFunction) => {
|
|
152
|
+
if (compareFunction) {
|
|
153
|
+
target[1].sort((a, b) => {
|
|
154
|
+
return compareFunction(
|
|
155
|
+
nodeToJsonldRepresentation(a, proxyContext),
|
|
156
|
+
nodeToJsonldRepresentation(b, proxyContext),
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
} else if (target) {
|
|
160
|
+
target[1].sort((a, b) => {
|
|
161
|
+
const aReal = nodeToJsonldRepresentation(a, proxyContext);
|
|
162
|
+
const bReal = nodeToJsonldRepresentation(b, proxyContext);
|
|
163
|
+
if (aReal > bReal) {
|
|
164
|
+
return 1;
|
|
165
|
+
} else if (bReal > aReal) {
|
|
166
|
+
return -1;
|
|
167
|
+
} else {
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
return proxyContext.createArrayProxy(
|
|
173
|
+
target[0],
|
|
174
|
+
target[2],
|
|
175
|
+
) as ObjectJsonRepresentation[];
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
splice: (target, key, proxyContext) => {
|
|
179
|
+
return (start, deleteCount, ...items: ObjectJsonRepresentation[]) => {
|
|
180
|
+
return modifyArray(
|
|
181
|
+
{
|
|
182
|
+
target,
|
|
183
|
+
key,
|
|
184
|
+
toAdd: items,
|
|
185
|
+
quadsToDelete: (quads) => {
|
|
186
|
+
return quads.splice(start, deleteCount);
|
|
187
|
+
},
|
|
188
|
+
modifyCoreArray: (coreArray, addedValues) => {
|
|
189
|
+
const spliced = coreArray.splice(
|
|
190
|
+
start,
|
|
191
|
+
deleteCount || 0,
|
|
192
|
+
...addedValues,
|
|
193
|
+
);
|
|
194
|
+
return spliced.map((node) => {
|
|
195
|
+
return nodeToJsonldRepresentation(node, proxyContext);
|
|
196
|
+
});
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
proxyContext,
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
},
|
|
203
|
+
unshift: (target, key, proxyContext) => {
|
|
204
|
+
return (...args) => {
|
|
205
|
+
return modifyArray(
|
|
206
|
+
{
|
|
207
|
+
target,
|
|
208
|
+
key,
|
|
209
|
+
toAdd: args,
|
|
210
|
+
modifyCoreArray: (coreArray, addedValues) => {
|
|
211
|
+
coreArray.unshift(...addedValues);
|
|
212
|
+
return proxyContext.createArrayProxy(target[0], target[2]).length;
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
proxyContext,
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { quad } from "@rdfjs/data-model";
|
|
2
|
+
import type { NamedNode } from "@rdfjs/types";
|
|
3
|
+
import type { ObjectNode, QuadMatch, SubjectNode } from "@ldo/rdf-utils";
|
|
4
|
+
import type { ObjectJsonRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
5
|
+
import { nodeToJsonldRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
6
|
+
import type { ArrayMethodBuildersType } from "./arrayMethods";
|
|
7
|
+
import { arrayMethodsBuilders, methodNames } from "./arrayMethods";
|
|
8
|
+
import {
|
|
9
|
+
_getNodeAtIndex,
|
|
10
|
+
_getUnderlyingArrayTarget,
|
|
11
|
+
_getUnderlyingDataset,
|
|
12
|
+
_getUnderlyingMatch,
|
|
13
|
+
_isSubjectOriented,
|
|
14
|
+
_proxyContext,
|
|
15
|
+
} from "../types";
|
|
16
|
+
import { modifyArray } from "./modifyArray";
|
|
17
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
18
|
+
import { NodeSet } from "../util/NodeSet";
|
|
19
|
+
import { filterQuadsByLanguageOrdering } from "../language/languageUtils";
|
|
20
|
+
|
|
21
|
+
export type ArrayProxyTarget = [
|
|
22
|
+
quadMatch: QuadMatch,
|
|
23
|
+
curArray: ObjectNode[],
|
|
24
|
+
isSubjectOriented?: boolean,
|
|
25
|
+
isLangStringArray?: boolean,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
function updateArrayOrder(
|
|
29
|
+
target: ArrayProxyTarget,
|
|
30
|
+
proxyContext: ProxyContext,
|
|
31
|
+
): void {
|
|
32
|
+
let quads = proxyContext.dataset.match(...target[0]);
|
|
33
|
+
if (target[3]) {
|
|
34
|
+
// Is lang string array
|
|
35
|
+
quads = filterQuadsByLanguageOrdering(quads, proxyContext.languageOrdering);
|
|
36
|
+
}
|
|
37
|
+
const datasetObjects = new NodeSet();
|
|
38
|
+
quads.toArray().forEach((quad) => {
|
|
39
|
+
// If this this a subject-oriented document
|
|
40
|
+
if (target[2]) {
|
|
41
|
+
datasetObjects.add(quad.subject as SubjectNode);
|
|
42
|
+
} else {
|
|
43
|
+
datasetObjects.add(quad.object as ObjectNode);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const processedObjects: ObjectNode[] = [];
|
|
47
|
+
target[1].forEach((arrItem) => {
|
|
48
|
+
if (datasetObjects.has(arrItem)) {
|
|
49
|
+
processedObjects.push(arrItem);
|
|
50
|
+
datasetObjects.delete(arrItem);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
datasetObjects.toArray().forEach((datasetObject) => {
|
|
54
|
+
processedObjects.push(datasetObject);
|
|
55
|
+
});
|
|
56
|
+
target[1] = processedObjects;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getProcessedArray(
|
|
60
|
+
target: ArrayProxyTarget,
|
|
61
|
+
proxyContext: ProxyContext,
|
|
62
|
+
): ObjectJsonRepresentation[] {
|
|
63
|
+
return target[1].map((node) => {
|
|
64
|
+
return nodeToJsonldRepresentation(node, proxyContext);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function createArrayHandler(
|
|
69
|
+
proxyContext: ProxyContext,
|
|
70
|
+
): ProxyHandler<ArrayProxyTarget> {
|
|
71
|
+
return {
|
|
72
|
+
get(target, key, ...rest) {
|
|
73
|
+
switch (key) {
|
|
74
|
+
case _getUnderlyingDataset:
|
|
75
|
+
return proxyContext.dataset;
|
|
76
|
+
case _getUnderlyingMatch:
|
|
77
|
+
return target[0];
|
|
78
|
+
case _isSubjectOriented:
|
|
79
|
+
return target[2];
|
|
80
|
+
case _getUnderlyingArrayTarget:
|
|
81
|
+
return target;
|
|
82
|
+
case _proxyContext:
|
|
83
|
+
return proxyContext;
|
|
84
|
+
case _getNodeAtIndex:
|
|
85
|
+
return (index: number): ObjectNode | undefined => {
|
|
86
|
+
updateArrayOrder(target, proxyContext);
|
|
87
|
+
return target[1][index];
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// TODO: Because of this, every get operation is O(n). Consider changing
|
|
92
|
+
// this
|
|
93
|
+
updateArrayOrder(target, proxyContext);
|
|
94
|
+
const processedArray = getProcessedArray(target, proxyContext);
|
|
95
|
+
if (methodNames.has(key as keyof ArrayMethodBuildersType)) {
|
|
96
|
+
return arrayMethodsBuilders[key as keyof ArrayMethodBuildersType](
|
|
97
|
+
target,
|
|
98
|
+
key as string,
|
|
99
|
+
proxyContext,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return Reflect.get(processedArray, key, ...rest);
|
|
103
|
+
},
|
|
104
|
+
getOwnPropertyDescriptor(target, key, ...rest) {
|
|
105
|
+
updateArrayOrder(target, proxyContext);
|
|
106
|
+
const processedArray = getProcessedArray(target, proxyContext);
|
|
107
|
+
return Reflect.getOwnPropertyDescriptor(processedArray, key, ...rest);
|
|
108
|
+
},
|
|
109
|
+
ownKeys(target, ...rest) {
|
|
110
|
+
updateArrayOrder(target, proxyContext);
|
|
111
|
+
const processedArray = getProcessedArray(target, proxyContext);
|
|
112
|
+
return Reflect.ownKeys(processedArray, ...rest);
|
|
113
|
+
},
|
|
114
|
+
getPrototypeOf(target, ...rest) {
|
|
115
|
+
updateArrayOrder(target, proxyContext);
|
|
116
|
+
const processedObjects = getProcessedArray(target, proxyContext);
|
|
117
|
+
return Reflect.getPrototypeOf(processedObjects, ...rest);
|
|
118
|
+
},
|
|
119
|
+
has(target, ...rest) {
|
|
120
|
+
updateArrayOrder(target, proxyContext);
|
|
121
|
+
const processedObjects = getProcessedArray(target, proxyContext);
|
|
122
|
+
return Reflect.has(processedObjects, ...rest);
|
|
123
|
+
},
|
|
124
|
+
set(target, key, value, ...rest) {
|
|
125
|
+
if (key === _proxyContext) {
|
|
126
|
+
proxyContext = value;
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
updateArrayOrder(target, proxyContext);
|
|
130
|
+
if (typeof key !== "symbol" && !isNaN(parseInt(key as string))) {
|
|
131
|
+
const index = parseInt(key);
|
|
132
|
+
return modifyArray(
|
|
133
|
+
{
|
|
134
|
+
target,
|
|
135
|
+
key,
|
|
136
|
+
toAdd: [value],
|
|
137
|
+
quadsToDelete(allQuads) {
|
|
138
|
+
return allQuads[index] ? [allQuads[index]] : [];
|
|
139
|
+
},
|
|
140
|
+
modifyCoreArray(coreArray, addedValues) {
|
|
141
|
+
coreArray[index] = addedValues[0];
|
|
142
|
+
return true;
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
proxyContext,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return Reflect.set(target[1], key, ...rest);
|
|
149
|
+
},
|
|
150
|
+
deleteProperty(target, key) {
|
|
151
|
+
const { dataset } = proxyContext;
|
|
152
|
+
if (typeof key !== "symbol" && !isNaN(parseInt(key as string))) {
|
|
153
|
+
const objectQuad = dataset.match(...target[0]).toArray()[parseInt(key)];
|
|
154
|
+
if (!objectQuad) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
const term = target[2] ? objectQuad.subject : objectQuad.object;
|
|
158
|
+
if (term.termType === "Literal") {
|
|
159
|
+
const subject = target[0][0] as NamedNode;
|
|
160
|
+
const predicate = target[0][1] as NamedNode;
|
|
161
|
+
if (subject && predicate) {
|
|
162
|
+
dataset.delete(quad(subject, predicate, term));
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
} else if (
|
|
166
|
+
term.termType === "NamedNode" ||
|
|
167
|
+
term.termType === "BlankNode"
|
|
168
|
+
) {
|
|
169
|
+
dataset.deleteMatches(term, undefined, undefined);
|
|
170
|
+
dataset.deleteMatches(undefined, undefined, term);
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return true;
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
_getNodeAtIndex,
|
|
3
|
+
_getUnderlyingArrayTarget,
|
|
4
|
+
_getUnderlyingDataset,
|
|
5
|
+
_getUnderlyingMatch,
|
|
6
|
+
_getUnderlyingNode,
|
|
7
|
+
_proxyContext,
|
|
8
|
+
_writeGraphs,
|
|
9
|
+
} from "../types";
|
|
10
|
+
import type { ArrayProxy } from "./ArrayProxy";
|
|
11
|
+
|
|
12
|
+
export function isArrayProxy(someObject?: unknown): someObject is ArrayProxy {
|
|
13
|
+
if (!someObject) return false;
|
|
14
|
+
if (typeof someObject !== "object") return false;
|
|
15
|
+
const potentialArrayProxy = someObject as ArrayProxy;
|
|
16
|
+
|
|
17
|
+
return !(
|
|
18
|
+
typeof potentialArrayProxy[_getUnderlyingDataset] !== "object" ||
|
|
19
|
+
typeof potentialArrayProxy[_getUnderlyingMatch] !== "object" ||
|
|
20
|
+
typeof potentialArrayProxy[_getNodeAtIndex] !== "function" ||
|
|
21
|
+
typeof potentialArrayProxy[_getUnderlyingArrayTarget] !== "object"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { defaultGraph } from "@rdfjs/data-model";
|
|
2
|
+
import type { Quad } from "@rdfjs/types";
|
|
3
|
+
import type { ObjectNode } from "@ldo/rdf-utils";
|
|
4
|
+
import { ProxyTransactionalDataset } from "@ldo/subscribable-dataset";
|
|
5
|
+
import { createDatasetFactory } from "@ldo/dataset";
|
|
6
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
7
|
+
import { addObjectToDataset } from "../util/addObjectToDataset";
|
|
8
|
+
import {
|
|
9
|
+
getNodeFromRawObject,
|
|
10
|
+
getNodeFromRawValue,
|
|
11
|
+
} from "../util/getNodeFromRaw";
|
|
12
|
+
import { nodeToString } from "../util/NodeSet";
|
|
13
|
+
import type { ObjectJsonRepresentation } from "../util/nodeToJsonldRepresentation";
|
|
14
|
+
import type { RawObject, RawValue } from "../util/RawObject";
|
|
15
|
+
import type { ArrayProxyTarget } from "./createArrayHandler";
|
|
16
|
+
|
|
17
|
+
export function checkArrayModification(
|
|
18
|
+
target: ArrayProxyTarget,
|
|
19
|
+
objectsToAdd: RawValue[],
|
|
20
|
+
proxyContext: ProxyContext,
|
|
21
|
+
) {
|
|
22
|
+
if (target[2]) {
|
|
23
|
+
for (const objectToAdd of objectsToAdd) {
|
|
24
|
+
// Undefined is fine no matter what
|
|
25
|
+
if (objectToAdd === undefined) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (typeof objectToAdd !== "object") {
|
|
29
|
+
throw new Error(
|
|
30
|
+
`Cannot add a literal "${objectToAdd}"(${typeof objectToAdd}) to a subject-oriented collection.`,
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
// Create a test dataset to see if the inputted data is valid
|
|
34
|
+
const testDataset = new ProxyTransactionalDataset(
|
|
35
|
+
proxyContext.dataset,
|
|
36
|
+
createDatasetFactory(),
|
|
37
|
+
);
|
|
38
|
+
addObjectToDataset(
|
|
39
|
+
objectToAdd as RawObject,
|
|
40
|
+
false,
|
|
41
|
+
proxyContext.duplicate({
|
|
42
|
+
writeGraphs: [defaultGraph()],
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
const isValidAddition =
|
|
46
|
+
testDataset.match(
|
|
47
|
+
getNodeFromRawObject(objectToAdd, proxyContext.contextUtil),
|
|
48
|
+
target[0][1],
|
|
49
|
+
target[0][2],
|
|
50
|
+
).size !== 0;
|
|
51
|
+
if (!isValidAddition) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`Cannot add value to collection. This must contain a quad that matches (${nodeToString(
|
|
54
|
+
target[0][0],
|
|
55
|
+
)}, ${nodeToString(target[0][1])}, ${nodeToString(
|
|
56
|
+
target[0][2],
|
|
57
|
+
)}, ${nodeToString(target[0][3])})`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
} else if (!target[0][0] || !target[0][1]) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
"A collection that does not specify a match for both a subject or predicate cannot be modified directly.",
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function modifyArray<ReturnType>(
|
|
69
|
+
config: {
|
|
70
|
+
target: ArrayProxyTarget;
|
|
71
|
+
key: string;
|
|
72
|
+
toAdd?: RawValue[];
|
|
73
|
+
quadsToDelete?: (quads: Quad[]) => Quad[];
|
|
74
|
+
modifyCoreArray: (
|
|
75
|
+
coreArray: ArrayProxyTarget[1],
|
|
76
|
+
addedValues: ArrayProxyTarget[1],
|
|
77
|
+
) => ReturnType;
|
|
78
|
+
},
|
|
79
|
+
proxyContext: ProxyContext,
|
|
80
|
+
): ReturnType {
|
|
81
|
+
const { target, toAdd, quadsToDelete, modifyCoreArray, key } = config;
|
|
82
|
+
const { dataset, contextUtil } = proxyContext;
|
|
83
|
+
checkArrayModification(target, toAdd || [], proxyContext);
|
|
84
|
+
|
|
85
|
+
// Remove appropriate Quads
|
|
86
|
+
if (quadsToDelete) {
|
|
87
|
+
const quadArr = dataset.match(...target[0]).toArray();
|
|
88
|
+
const deleteQuadArr = quadsToDelete(quadArr);
|
|
89
|
+
// Filter out overlapping items
|
|
90
|
+
deleteQuadArr.forEach((delQuad) => {
|
|
91
|
+
if (target[2]) {
|
|
92
|
+
dataset.deleteMatches(delQuad.subject, undefined, undefined);
|
|
93
|
+
} else {
|
|
94
|
+
dataset.delete(delQuad);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Add new items to the dataset
|
|
100
|
+
const added = toAdd
|
|
101
|
+
?.map((item) => {
|
|
102
|
+
return typeof item === "object"
|
|
103
|
+
? addObjectToDataset(item, false, proxyContext)
|
|
104
|
+
: item;
|
|
105
|
+
})
|
|
106
|
+
.filter(
|
|
107
|
+
(val) => val != undefined,
|
|
108
|
+
) as NonNullable<ObjectJsonRepresentation>[];
|
|
109
|
+
if (!target[2] && target[0][0] && target[0][1] && added) {
|
|
110
|
+
addObjectToDataset(
|
|
111
|
+
{
|
|
112
|
+
"@id": target[0][0],
|
|
113
|
+
[contextUtil.iriToKey(target[0][1].value)]: added,
|
|
114
|
+
} as RawObject,
|
|
115
|
+
false,
|
|
116
|
+
proxyContext,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
const addedNodes = added
|
|
120
|
+
? (added
|
|
121
|
+
.map((addedValue) => {
|
|
122
|
+
return getNodeFromRawValue(key, addedValue, proxyContext);
|
|
123
|
+
})
|
|
124
|
+
.filter((val) => val != undefined) as ObjectNode[])
|
|
125
|
+
: [];
|
|
126
|
+
|
|
127
|
+
// Allow the base array to be modified
|
|
128
|
+
return modifyCoreArray(target[1], addedNodes);
|
|
129
|
+
}
|
package/src/graphOf.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { ObjectNode, GraphNode } from "@ldo/rdf-utils";
|
|
2
|
+
import { namedNode } from "@rdfjs/data-model";
|
|
3
|
+
import {
|
|
4
|
+
getSubjectProxyFromObject,
|
|
5
|
+
isSubjectProxy,
|
|
6
|
+
} from "./subjectProxy/isSubjectProxy";
|
|
7
|
+
import type { ObjectLike } from "./types";
|
|
8
|
+
import {
|
|
9
|
+
_getNodeAtIndex,
|
|
10
|
+
_getUnderlyingDataset,
|
|
11
|
+
_getUnderlyingMatch,
|
|
12
|
+
_getUnderlyingNode,
|
|
13
|
+
_proxyContext,
|
|
14
|
+
} from "./types";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Returns the graph for which a defined triple is a member
|
|
18
|
+
* @param subject A JsonldDatasetProxy that represents the subject
|
|
19
|
+
* @param predicate The key on the JsonldDatasetProxy
|
|
20
|
+
* @param object The direct object. This can be a JsonldDatasetProxy or the index
|
|
21
|
+
* @returns a list of graphs for which the triples are members
|
|
22
|
+
*/
|
|
23
|
+
export function graphOf<Subject extends ObjectLike, Key extends keyof Subject>(
|
|
24
|
+
subject: Subject,
|
|
25
|
+
predicate: Key,
|
|
26
|
+
object?: NonNullable<Subject[Key]> extends Array<unknown>
|
|
27
|
+
? number | ObjectLike
|
|
28
|
+
: ObjectLike,
|
|
29
|
+
): GraphNode[] {
|
|
30
|
+
const subjectProxy = getSubjectProxyFromObject(subject);
|
|
31
|
+
const proxyContext = subjectProxy[_proxyContext];
|
|
32
|
+
const subjectNode = subjectProxy[_getUnderlyingNode];
|
|
33
|
+
const predicateNode = namedNode(
|
|
34
|
+
proxyContext.contextUtil.keyToIri(predicate as string),
|
|
35
|
+
);
|
|
36
|
+
let objectNode: ObjectNode | null;
|
|
37
|
+
if (object == null) {
|
|
38
|
+
objectNode = null;
|
|
39
|
+
} else if (typeof object === "number") {
|
|
40
|
+
const proxyArray = subject[predicate];
|
|
41
|
+
if (!proxyArray[_getUnderlyingMatch]) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Key "${String(predicate)}" of ${subject} is not an array.`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
if (!proxyArray[object]) {
|
|
47
|
+
throw new Error(`Index ${object} does not exist.`);
|
|
48
|
+
}
|
|
49
|
+
if (isSubjectProxy(proxyArray[object])) {
|
|
50
|
+
objectNode = proxyArray[object][1];
|
|
51
|
+
}
|
|
52
|
+
objectNode = proxyArray[_getNodeAtIndex](object);
|
|
53
|
+
} else {
|
|
54
|
+
const objectProxy = getSubjectProxyFromObject(object);
|
|
55
|
+
objectNode = objectProxy[_getUnderlyingNode];
|
|
56
|
+
}
|
|
57
|
+
const quads = subjectProxy[_getUnderlyingDataset].match(
|
|
58
|
+
subjectNode,
|
|
59
|
+
predicateNode,
|
|
60
|
+
objectNode,
|
|
61
|
+
);
|
|
62
|
+
return quads.toArray().map((quad): GraphNode => quad.graph as GraphNode);
|
|
63
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { jsonldDatasetProxy } from "./jsonldDatasetProxy";
|
|
2
|
+
|
|
3
|
+
export default jsonldDatasetProxy;
|
|
4
|
+
export * from "./types";
|
|
5
|
+
export * from "./ContextUtil";
|
|
6
|
+
export * from "./ProxyContext";
|
|
7
|
+
export * from "./JsonldDatasetProxyBuilder";
|
|
8
|
+
export * from "./jsonldDatasetProxy";
|
|
9
|
+
export * from "./write";
|
|
10
|
+
export * from "./graphOf";
|
|
11
|
+
export * from "./setLanguagePreferences";
|
|
12
|
+
|
|
13
|
+
export * from "./language/languagesOf";
|
|
14
|
+
export * from "./language/languageMapProxy";
|
|
15
|
+
export * from "./language/languageSet";
|
|
16
|
+
export * from "./language/languageTypes";
|
|
17
|
+
export * from "./language/languageUtils";
|
|
18
|
+
|
|
19
|
+
export * from "./arrayProxy/createArrayHandler";
|
|
20
|
+
export * from "./arrayProxy/arrayMethods";
|
|
21
|
+
export * from "./arrayProxy/ArrayProxy";
|
|
22
|
+
export * from "./arrayProxy/modifyArray";
|
|
23
|
+
export * from "./arrayProxy/isArrayProxy";
|
|
24
|
+
|
|
25
|
+
export * from "./subjectProxy/createSubjectHandler";
|
|
26
|
+
export * from "./subjectProxy/SubjectProxy";
|
|
27
|
+
export * from "./subjectProxy/getValueForKey";
|
|
28
|
+
export * from "./subjectProxy/deleteFromDataset";
|
|
29
|
+
export * from "./subjectProxy/isSubjectProxy";
|
|
30
|
+
|
|
31
|
+
export * from "./util/addObjectToDataset";
|
|
32
|
+
export * from "./util/nodeToJsonldRepresentation";
|
|
33
|
+
export * from "./util/RawObject";
|
|
34
|
+
export * from "./util/getNodeFromRaw";
|
|
35
|
+
export * from "./util/NodeSet";
|
|
36
|
+
export * from "./util/isProxy";
|
|
37
|
+
export * from "./util/createInteractOptions";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defaultGraph } from "@rdfjs/data-model";
|
|
2
|
+
import type { Dataset } from "@rdfjs/types";
|
|
3
|
+
import type { ContextDefinition } from "jsonld";
|
|
4
|
+
import { ContextUtil } from "./ContextUtil";
|
|
5
|
+
import { JsonldDatasetProxyBuilder } from "./JsonldDatasetProxyBuilder";
|
|
6
|
+
import { ProxyContext } from "./ProxyContext";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Creates a JSON-LD Dataset Proxy
|
|
10
|
+
*
|
|
11
|
+
* @param inputDataset the source dataset
|
|
12
|
+
* @param context JSON-LD Context
|
|
13
|
+
* @returns a JSON-LD Dataset proxy
|
|
14
|
+
*/
|
|
15
|
+
export function jsonldDatasetProxy(
|
|
16
|
+
inputDataset: Dataset,
|
|
17
|
+
context: ContextDefinition,
|
|
18
|
+
): JsonldDatasetProxyBuilder {
|
|
19
|
+
const contextUtil = new ContextUtil(context);
|
|
20
|
+
const proxyContext = new ProxyContext({
|
|
21
|
+
dataset: inputDataset,
|
|
22
|
+
contextUtil,
|
|
23
|
+
writeGraphs: [defaultGraph()],
|
|
24
|
+
languageOrdering: ["none", "en", "other"],
|
|
25
|
+
});
|
|
26
|
+
return new JsonldDatasetProxyBuilder(proxyContext);
|
|
27
|
+
}
|