@ldo/jsonld-dataset-proxy 0.0.1-1.0.0-alpha.1.0
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/LICENSE.txt +21 -0
- package/README.md +830 -0
- package/dist/ContextUtil.d.ts +17 -0
- package/dist/ContextUtil.js +108 -0
- package/dist/ContextUtil.js.map +1 -0
- package/dist/JsonldDatasetProxyBuilder.d.ts +16 -0
- package/dist/JsonldDatasetProxyBuilder.js +41 -0
- package/dist/JsonldDatasetProxyBuilder.js.map +1 -0
- package/dist/LdoJsonldContext.d.ts +8 -0
- package/dist/LdoJsonldContext.js +3 -0
- package/dist/LdoJsonldContext.js.map +1 -0
- package/dist/ProxyContext.d.ts +31 -0
- package/dist/ProxyContext.js +63 -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 +4 -0
- package/dist/graphOf.js +25 -0
- package/dist/graphOf.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonldDatasetProxy.d.ts +5 -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 +13 -0
- package/dist/language/languagesOf.js +17 -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/setProxy/ObjectSetProxy.d.ts +17 -0
- package/dist/setProxy/ObjectSetProxy.js +31 -0
- package/dist/setProxy/ObjectSetProxy.js.map +1 -0
- package/dist/setProxy/SetProxy.d.ts +29 -0
- package/dist/setProxy/SetProxy.js +72 -0
- package/dist/setProxy/SetProxy.js.map +1 -0
- package/dist/setProxy/SubjectSetProxy.d.ts +15 -0
- package/dist/setProxy/SubjectSetProxy.js +38 -0
- package/dist/setProxy/SubjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardObjectSetProxy.d.ts +20 -0
- package/dist/setProxy/WildcardObjectSetProxy.js +49 -0
- package/dist/setProxy/WildcardObjectSetProxy.js.map +1 -0
- package/dist/setProxy/WildcardSubjectSetProxy.d.ts +19 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js +31 -0
- package/dist/setProxy/WildcardSubjectSetProxy.js.map +1 -0
- package/dist/setProxy/createNewSetProxy.d.ts +5 -0
- package/dist/setProxy/createNewSetProxy.js +27 -0
- package/dist/setProxy/createNewSetProxy.js.map +1 -0
- package/dist/setProxy/isSetProxy.d.ts +2 -0
- package/dist/setProxy/isSetProxy.js +13 -0
- package/dist/setProxy/isSetProxy.js.map +1 -0
- package/dist/setProxy/ldSet/BasicLdSet.d.ts +36 -0
- package/dist/setProxy/ldSet/BasicLdSet.js +193 -0
- package/dist/setProxy/ldSet/BasicLdSet.js.map +1 -0
- package/dist/setProxy/ldSet/LdSet.d.ts +30 -0
- package/dist/setProxy/ldSet/LdSet.js +3 -0
- package/dist/setProxy/ldSet/LdSet.js.map +1 -0
- package/dist/setProxy/set.d.ts +2 -0
- package/dist/setProxy/set.js +9 -0
- package/dist/setProxy/set.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 +75 -0
- package/dist/subjectProxy/createSubjectHandler.js.map +1 -0
- package/dist/subjectProxy/deleteFromDataset.d.ts +3 -0
- package/dist/subjectProxy/deleteFromDataset.js +25 -0
- package/dist/subjectProxy/deleteFromDataset.js.map +1 -0
- package/dist/subjectProxy/getValueForKey.d.ts +5 -0
- package/dist/subjectProxy/getValueForKey.js +41 -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 +11 -0
- package/dist/types.js.map +1 -0
- package/dist/util/NodeSet.d.ts +9 -0
- package/dist/util/NodeSet.js +35 -0
- package/dist/util/NodeSet.js.map +1 -0
- package/dist/util/RawObject.d.ts +9 -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 +47 -0
- package/dist/util/getNodeFromRaw.js.map +1 -0
- package/dist/util/isProxy.d.ts +6 -0
- package/dist/util/isProxy.js +17 -0
- package/dist/util/isProxy.js.map +1 -0
- package/dist/util/nodeToJsonldRepresentation.d.ts +7 -0
- package/dist/util/nodeToJsonldRepresentation.js +75 -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 +173 -0
- package/src/JsonldDatasetProxyBuilder.ts +107 -0
- package/src/LdoJsonldContext.ts +14 -0
- package/src/ProxyContext.ts +118 -0
- package/src/graphOf.ts +52 -0
- package/src/index.ts +39 -0
- package/src/jsonldDatasetProxy.ts +28 -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 +65 -0
- package/src/setLanguagePreferences.ts +14 -0
- package/src/setProxy/ObjectSetProxy.ts +66 -0
- package/src/setProxy/SetProxy.ts +141 -0
- package/src/setProxy/SubjectSetProxy.ts +85 -0
- package/src/setProxy/WildcardObjectSetProxy.ts +89 -0
- package/src/setProxy/WildcardSubjectSetProxy.ts +58 -0
- package/src/setProxy/createNewSetProxy.ts +47 -0
- package/src/setProxy/isSetProxy.ts +7 -0
- package/src/setProxy/ldSet/BasicLdSet.ts +266 -0
- package/src/setProxy/ldSet/LdSet.ts +189 -0
- package/src/setProxy/set.ts +14 -0
- package/src/subjectProxy/SubjectProxy.ts +20 -0
- package/src/subjectProxy/createSubjectHandler.ts +115 -0
- package/src/subjectProxy/deleteFromDataset.ts +33 -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 +35 -0
- package/src/util/RawObject.ts +21 -0
- package/src/util/addObjectToDataset.ts +147 -0
- package/src/util/createInteractOptions.ts +55 -0
- package/src/util/getNodeFromRaw.ts +54 -0
- package/src/util/isProxy.ts +21 -0
- package/src/util/nodeToJsonldRepresentation.ts +78 -0
- package/src/write.ts +12 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { QuadMatch } from "@ldo/rdf-utils";
|
|
2
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
3
|
+
import type { RawObject, RawValue } from "../util/RawObject";
|
|
4
|
+
import type { ObjectSetProxyQuadMatch } from "./ObjectSetProxy";
|
|
5
|
+
import { ObjectSetProxy } from "./ObjectSetProxy";
|
|
6
|
+
import type { SubjectSetProxyQuadMatch } from "./SubjectSetProxy";
|
|
7
|
+
import { SubjectSetProxy } from "./SubjectSetProxy";
|
|
8
|
+
import type { WildcardObjectSetProxyQuadMatch } from "./WildcardObjectSetProxy";
|
|
9
|
+
import { WildcardObjectSetProxy } from "./WildcardObjectSetProxy";
|
|
10
|
+
import type { WildcardSubjectSetProxyQuadMatch } from "./WildcardSubjectSetProxy";
|
|
11
|
+
import { WildcardSubjectSetProxy } from "./WildcardSubjectSetProxy";
|
|
12
|
+
import type { SetProxy } from "./SetProxy";
|
|
13
|
+
|
|
14
|
+
export function createNewSetProxy<T extends NonNullable<RawValue>>(
|
|
15
|
+
quadMatch: QuadMatch,
|
|
16
|
+
isSubjectOriented: boolean,
|
|
17
|
+
proxyContext: ProxyContext,
|
|
18
|
+
isLangSet?: boolean,
|
|
19
|
+
): SetProxy<T> {
|
|
20
|
+
if (!isSubjectOriented) {
|
|
21
|
+
if (quadMatch[0] && quadMatch[1]) {
|
|
22
|
+
return new ObjectSetProxy<T>(
|
|
23
|
+
proxyContext,
|
|
24
|
+
quadMatch as ObjectSetProxyQuadMatch,
|
|
25
|
+
isLangSet,
|
|
26
|
+
);
|
|
27
|
+
} else {
|
|
28
|
+
return new WildcardObjectSetProxy<T>(
|
|
29
|
+
proxyContext,
|
|
30
|
+
quadMatch as WildcardObjectSetProxyQuadMatch,
|
|
31
|
+
isLangSet,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
if (quadMatch[1] && quadMatch[2]) {
|
|
36
|
+
return new SubjectSetProxy<T & RawObject>(
|
|
37
|
+
proxyContext,
|
|
38
|
+
quadMatch as SubjectSetProxyQuadMatch,
|
|
39
|
+
);
|
|
40
|
+
} else {
|
|
41
|
+
return new WildcardSubjectSetProxy<T & RawObject>(
|
|
42
|
+
proxyContext,
|
|
43
|
+
quadMatch as WildcardSubjectSetProxyQuadMatch,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
2
|
+
import { _getUnderlyingNode } from "../../types";
|
|
3
|
+
import type { RawValue } from "../../util/RawObject";
|
|
4
|
+
import type { LdSet } from "./LdSet";
|
|
5
|
+
import { blankNode } from "@rdfjs/data-model";
|
|
6
|
+
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
+
export class BasicLdSet<T extends NonNullable<RawValue> = NonNullable<RawValue>>
|
|
9
|
+
implements LdSet<T>
|
|
10
|
+
{
|
|
11
|
+
private hashMap: Map<string, T>;
|
|
12
|
+
|
|
13
|
+
constructor(values?: Iterable<T> | null) {
|
|
14
|
+
this.hashMap = new Map();
|
|
15
|
+
if (values) {
|
|
16
|
+
for (const value of values) {
|
|
17
|
+
this.add(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
private hashFn(value: T): string {
|
|
23
|
+
if (typeof value !== "object") return value.toString();
|
|
24
|
+
if (value[_getUnderlyingNode]) {
|
|
25
|
+
return (value[_getUnderlyingNode] as NamedNode | BlankNode).value;
|
|
26
|
+
} else if (!value["@id"]) {
|
|
27
|
+
return blankNode().value;
|
|
28
|
+
} else if (typeof value["@id"] === "string") {
|
|
29
|
+
return value["@id"];
|
|
30
|
+
} else {
|
|
31
|
+
return value["@id"].value;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* ===========================================================================
|
|
37
|
+
* Base Set Functions
|
|
38
|
+
* ===========================================================================
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
add(value: T): this {
|
|
42
|
+
const key = this.hashFn(value);
|
|
43
|
+
if (!this.hashMap.has(key)) {
|
|
44
|
+
this.hashMap.set(key, value);
|
|
45
|
+
}
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
clear(): void {
|
|
50
|
+
this.hashMap.clear();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
delete(value: T): boolean {
|
|
54
|
+
const key = this.hashFn(value);
|
|
55
|
+
return this.hashMap.delete(key);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
has(value: T): boolean {
|
|
59
|
+
const key = this.hashFn(value);
|
|
60
|
+
return this.hashMap.has(key);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get size(): number {
|
|
64
|
+
return this.hashMap.size;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
*entries(): IterableIterator<[T, T]> {
|
|
68
|
+
for (const [, value] of this.hashMap.entries()) {
|
|
69
|
+
yield [value, value];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
keys(): IterableIterator<T> {
|
|
74
|
+
return this.hashMap.values();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
values(): IterableIterator<T> {
|
|
78
|
+
return this.hashMap.values();
|
|
79
|
+
}
|
|
80
|
+
[Symbol.iterator](): IterableIterator<T> {
|
|
81
|
+
return this.hashMap.values();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get [Symbol.toStringTag]() {
|
|
85
|
+
// TODO: Change this to be human readable.
|
|
86
|
+
return "BasicLdSet";
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* ===========================================================================
|
|
91
|
+
* Array Functions
|
|
92
|
+
* ===========================================================================
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
every<S extends T>(
|
|
96
|
+
predicate: (value: T, set: LdSet<T>) => value is S,
|
|
97
|
+
thisArg?: any,
|
|
98
|
+
): this is LdSet<S>;
|
|
99
|
+
every(
|
|
100
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
101
|
+
thisArg?: any,
|
|
102
|
+
): boolean;
|
|
103
|
+
every(predicate: (value: T, set: LdSet<T>) => any, thisArg?: any): boolean {
|
|
104
|
+
for (const value of this) {
|
|
105
|
+
if (!predicate.call(thisArg, value, this)) return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
some(
|
|
111
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
112
|
+
thisArg?: any,
|
|
113
|
+
): boolean {
|
|
114
|
+
for (const value of this) {
|
|
115
|
+
if (predicate.call(thisArg, value, this)) return true;
|
|
116
|
+
}
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
forEach(
|
|
121
|
+
callbackfn: (value: T, value2: T, set: LdSet<T>) => void,
|
|
122
|
+
thisArg?: any,
|
|
123
|
+
): void {
|
|
124
|
+
for (const value of this) {
|
|
125
|
+
callbackfn.call(thisArg, value, value, this);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
map<U>(callbackfn: (value: T, set: LdSet<T>) => U, thisArg?: any): U[] {
|
|
130
|
+
const returnValues: U[] = [];
|
|
131
|
+
for (const value of this) {
|
|
132
|
+
returnValues.push(callbackfn.call(thisArg, value, this));
|
|
133
|
+
}
|
|
134
|
+
return returnValues;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
filter<S extends T>(
|
|
138
|
+
predicate: (value: T, set: LdSet<T>) => value is S,
|
|
139
|
+
thisArg?: any,
|
|
140
|
+
): LdSet<S>;
|
|
141
|
+
filter(
|
|
142
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
143
|
+
thisArg?: any,
|
|
144
|
+
): LdSet<T>;
|
|
145
|
+
filter(
|
|
146
|
+
predicate: (value: T, set: LdSet<T>) => any,
|
|
147
|
+
thisArg?: unknown,
|
|
148
|
+
): LdSet<T> {
|
|
149
|
+
const newSet = new BasicLdSet<T>();
|
|
150
|
+
for (const value of this) {
|
|
151
|
+
if (predicate.call(thisArg, value, this)) newSet.add(value);
|
|
152
|
+
}
|
|
153
|
+
return newSet;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
reduce(
|
|
157
|
+
callbackfn: (previousValue: T, currentValue: T, set: LdSet<T>) => T,
|
|
158
|
+
): T;
|
|
159
|
+
reduce(
|
|
160
|
+
callbackfn: (previousValue: T, currentValue: T, set: LdSet<T>) => T,
|
|
161
|
+
initialValue: T,
|
|
162
|
+
): T;
|
|
163
|
+
reduce<U>(
|
|
164
|
+
callbackfn: (previousValue: U, currentValue: T, set: LdSet<T>) => U,
|
|
165
|
+
initialValue: U,
|
|
166
|
+
): U;
|
|
167
|
+
reduce(callbackfn: any, initialValue?: any): any {
|
|
168
|
+
const iterator = this[Symbol.iterator]();
|
|
169
|
+
let accumulator;
|
|
170
|
+
|
|
171
|
+
if (initialValue === undefined) {
|
|
172
|
+
const first = iterator.next();
|
|
173
|
+
if (first.done) {
|
|
174
|
+
throw new TypeError("Reduce of empty collection with no initial value");
|
|
175
|
+
}
|
|
176
|
+
accumulator = first.value;
|
|
177
|
+
} else {
|
|
178
|
+
accumulator = initialValue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
let result = iterator.next();
|
|
182
|
+
while (!result.done) {
|
|
183
|
+
accumulator = callbackfn(accumulator, result.value, this);
|
|
184
|
+
result = iterator.next();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return accumulator;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
toArray(): T[] {
|
|
191
|
+
const arr: T[] = [];
|
|
192
|
+
this.forEach((value) => arr.push(value));
|
|
193
|
+
return arr;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
toJSON(): T[] {
|
|
197
|
+
return this.toArray();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* ===========================================================================
|
|
202
|
+
* Set Methods
|
|
203
|
+
* ===========================================================================
|
|
204
|
+
*/
|
|
205
|
+
|
|
206
|
+
difference(other: Set<T>): LdSet<T> {
|
|
207
|
+
return this.filter((value) => !other.has(value));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
intersection(other: Set<T>): LdSet<T> {
|
|
211
|
+
const newSet = new BasicLdSet<T>();
|
|
212
|
+
const iteratingSet = this.size < other.size ? this : other;
|
|
213
|
+
const comparingSet = this.size < other.size ? other : this;
|
|
214
|
+
for (const value of iteratingSet) {
|
|
215
|
+
if (comparingSet.has(value)) {
|
|
216
|
+
newSet.add(value);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return newSet;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
isDisjointFrom(other: Set<T>): boolean {
|
|
223
|
+
const iteratingSet = this.size < other.size ? this : other;
|
|
224
|
+
const comparingSet = this.size < other.size ? other : this;
|
|
225
|
+
for (const value of iteratingSet) {
|
|
226
|
+
if (comparingSet.has(value)) return false;
|
|
227
|
+
}
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
isSubsetOf(other: Set<T>): boolean {
|
|
232
|
+
if (this.size > other.size) return false;
|
|
233
|
+
for (const value of this) {
|
|
234
|
+
if (!other.has(value)) return false;
|
|
235
|
+
}
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
isSupersetOf(other: Set<T>): boolean {
|
|
240
|
+
if (this.size < other.size) return false;
|
|
241
|
+
for (const value of other) {
|
|
242
|
+
if (!this.has(value)) return false;
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
symmetricDifference(other: Set<T>): LdSet<T> {
|
|
248
|
+
const newSet = new BasicLdSet<T>();
|
|
249
|
+
this.forEach((value) => newSet.add(value));
|
|
250
|
+
other.forEach((value) => {
|
|
251
|
+
if (newSet.has(value)) {
|
|
252
|
+
newSet.delete(value);
|
|
253
|
+
} else {
|
|
254
|
+
newSet.add(value);
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
return newSet;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
union(other: Set<T>): LdSet<T> {
|
|
261
|
+
const newSet = new BasicLdSet<T>();
|
|
262
|
+
this.forEach((value) => newSet.add(value));
|
|
263
|
+
other.forEach((value) => newSet.add(value));
|
|
264
|
+
return newSet;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An abract representation for a set of Linked Data Objects
|
|
5
|
+
*/
|
|
6
|
+
export interface LdSet<T> extends Set<T> {
|
|
7
|
+
/**
|
|
8
|
+
* ===========================================================================
|
|
9
|
+
* BASE METHODS
|
|
10
|
+
* ===========================================================================
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Appends a new element with a specified value to the end of the Set.
|
|
14
|
+
*/
|
|
15
|
+
add(value: T): this;
|
|
16
|
+
/**
|
|
17
|
+
* Clears this set of all values, but keeps the values in the datastore
|
|
18
|
+
*/
|
|
19
|
+
clear(): void;
|
|
20
|
+
/**
|
|
21
|
+
* Removes a specified value from the Set.
|
|
22
|
+
* @returns Returns true if an element in the Set existed and has been removed, or false if the element does not exist.
|
|
23
|
+
*/
|
|
24
|
+
delete(value: T): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* @returns a boolean indicating whether an element with the specified value exists in the Set or not.
|
|
27
|
+
*/
|
|
28
|
+
has(value: T): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* @returns the number of (unique) elements in Set.
|
|
31
|
+
*/
|
|
32
|
+
readonly size: number;
|
|
33
|
+
/** Iterates over values in the set. */
|
|
34
|
+
[Symbol.iterator](): IterableIterator<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Returns an iterable of [v,v] pairs for every value `v` in the set.
|
|
37
|
+
*/
|
|
38
|
+
entries(): IterableIterator<[T, T]>;
|
|
39
|
+
/**
|
|
40
|
+
* Despite its name, returns an iterable of the values in the set.
|
|
41
|
+
*/
|
|
42
|
+
keys(): IterableIterator<T>;
|
|
43
|
+
/**
|
|
44
|
+
* Returns an iterable of values in the set.
|
|
45
|
+
*/
|
|
46
|
+
values(): IterableIterator<T>;
|
|
47
|
+
/**
|
|
48
|
+
* ===========================================================================
|
|
49
|
+
* ITERATOR METHODS
|
|
50
|
+
* These methods mimic array methods
|
|
51
|
+
* ===========================================================================
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Determines whether all the members of an set satisfy the specified test.
|
|
55
|
+
* @param predicate A function that accepts up to two arguments. The every method calls
|
|
56
|
+
* the predicate function for each element in the set until the predicate returns a value
|
|
57
|
+
* which is coercible to the Boolean value false, or until the end of the set.
|
|
58
|
+
* @param thisArg An object to which the this keyword can refer in the predicate function.
|
|
59
|
+
* If thisArg is omitted, undefined is used as the this value.
|
|
60
|
+
*/
|
|
61
|
+
every<S extends T>(
|
|
62
|
+
predicate: (value: T, set: LdSet<T>) => value is S,
|
|
63
|
+
thisArg?: any,
|
|
64
|
+
): this is LdSet<S>;
|
|
65
|
+
/**
|
|
66
|
+
* Determines whether all the members of an set satisfy the specified test.
|
|
67
|
+
* @param predicate A function that accepts up to two arguments. The every method calls
|
|
68
|
+
* the predicate function for each element in the set until the predicate returns a value
|
|
69
|
+
* which is coercible to the Boolean value false, or until the end of the set.
|
|
70
|
+
* @param thisArg An object to which the this keyword can refer in the predicate function.
|
|
71
|
+
* If thisArg is omitted, undefined is used as the this value.
|
|
72
|
+
*/
|
|
73
|
+
every(
|
|
74
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
75
|
+
thisArg?: any,
|
|
76
|
+
): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Determines whether the specified callback function returns true for any element of a set.
|
|
79
|
+
* @param predicate A function that accepts up to two arguments. The some method calls
|
|
80
|
+
* the predicate function for each element in the set until the predicate returns a value
|
|
81
|
+
* which is coercible to the Boolean value true, or until the end of the set.
|
|
82
|
+
* @param thisArg An object to which the this keyword can refer in the predicate function.
|
|
83
|
+
* If thisArg is omitted, undefined is used as the this value.
|
|
84
|
+
*/
|
|
85
|
+
some(predicate: (value: T, set: LdSet<T>) => unknown, thisArg?: any): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Performs the specified action for each element in an set.
|
|
88
|
+
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the set. A "value2" is provided for parity with the base set.
|
|
89
|
+
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
|
|
90
|
+
*/
|
|
91
|
+
forEach(
|
|
92
|
+
callbackfn: (value: T, value2: T, set: LdSet<T>) => void,
|
|
93
|
+
thisArg?: any,
|
|
94
|
+
): void;
|
|
95
|
+
/**
|
|
96
|
+
* Calls a defined callback function on each element of an set, and returns a set that contains the results.
|
|
97
|
+
* @param callbackfn A function that accepts up to two arguments. The map method calls the callbackfn function one time for each element in the set.
|
|
98
|
+
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
|
|
99
|
+
*/
|
|
100
|
+
map<U>(callbackfn: (value: T, set: LdSet<T>) => U, thisArg?: any): U[];
|
|
101
|
+
/**
|
|
102
|
+
* Returns the elements of a set that meet the condition specified in a callback function.
|
|
103
|
+
* @param predicate A function that accepts up to two arguments. The filter method calls the predicate function one time for each element in the set.
|
|
104
|
+
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
|
|
105
|
+
*/
|
|
106
|
+
filter<S extends T>(
|
|
107
|
+
predicate: (value: T, set: LdSet<T>) => value is S,
|
|
108
|
+
thisArg?: any,
|
|
109
|
+
): LdSet<S>;
|
|
110
|
+
/**
|
|
111
|
+
* Returns the elements of a set that meet the condition specified in a callback function.
|
|
112
|
+
* @param predicate A function that accepts up to two arguments. The filter method calls the predicate function one time for each element in the set.
|
|
113
|
+
* @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
|
|
114
|
+
*/
|
|
115
|
+
filter(
|
|
116
|
+
predicate: (value: T, set: LdSet<T>) => unknown,
|
|
117
|
+
thisArg?: any,
|
|
118
|
+
): LdSet<T>;
|
|
119
|
+
/**
|
|
120
|
+
* Calls the specified callback function for all the elements in a set. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
|
|
121
|
+
* @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the set.
|
|
122
|
+
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
|
|
123
|
+
*/
|
|
124
|
+
reduce(
|
|
125
|
+
callbackfn: (previousValue: T, currentValue: T, set: LdSet<T>) => T,
|
|
126
|
+
): T;
|
|
127
|
+
/**
|
|
128
|
+
* Calls the specified callback function for all the elements in a set. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
|
|
129
|
+
* @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the set.
|
|
130
|
+
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
|
|
131
|
+
*/
|
|
132
|
+
reduce(
|
|
133
|
+
callbackfn: (previousValue: T, currentValue: T, set: LdSet<T>) => T,
|
|
134
|
+
initialValue: T,
|
|
135
|
+
): T;
|
|
136
|
+
/**
|
|
137
|
+
* Calls the specified callback function for all the elements in a set. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
|
|
138
|
+
* @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the set.
|
|
139
|
+
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
|
|
140
|
+
*/
|
|
141
|
+
reduce<U>(
|
|
142
|
+
callbackfn: (previousValue: U, currentValue: T, array: LdSet<T>) => U,
|
|
143
|
+
initialValue: U,
|
|
144
|
+
): U;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Converts the current LdSet to an array.
|
|
148
|
+
*/
|
|
149
|
+
toArray(): T[];
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Converts to JSON
|
|
153
|
+
*/
|
|
154
|
+
toJSON(): T[];
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* ===========================================================================
|
|
158
|
+
* EXTENDED SET METHODS
|
|
159
|
+
* ===========================================================================
|
|
160
|
+
*/
|
|
161
|
+
/**
|
|
162
|
+
* Returns a new set containing elements in this set but not in the given set.
|
|
163
|
+
*/
|
|
164
|
+
difference(other: Set<T>): LdSet<T>;
|
|
165
|
+
/**
|
|
166
|
+
* returns a new set containing elements in both this set and the given set.
|
|
167
|
+
*/
|
|
168
|
+
intersection(other: Set<T>): LdSet<T>;
|
|
169
|
+
/**
|
|
170
|
+
* Returns a boolean indicating if this set has no elements in common with the given set.
|
|
171
|
+
*/
|
|
172
|
+
isDisjointFrom(other: Set<T>): boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Returns a boolean indicating if all elements of this set are in the given set.
|
|
175
|
+
*/
|
|
176
|
+
isSubsetOf(other: Set<T>): boolean;
|
|
177
|
+
/**
|
|
178
|
+
* Returns a boolean indicating if all elements of the given set are in this set.
|
|
179
|
+
*/
|
|
180
|
+
isSupersetOf(other: Set<T>): boolean;
|
|
181
|
+
/**
|
|
182
|
+
* Returns a new set containing elements which are in either this set or the given set, but not in both.
|
|
183
|
+
*/
|
|
184
|
+
symmetricDifference(other: Set<T>): LdSet<T>;
|
|
185
|
+
/**
|
|
186
|
+
* Returns a new set containing elements which are in either or both of this set and the given set.
|
|
187
|
+
*/
|
|
188
|
+
union(other: Set<T>): LdSet<T>;
|
|
189
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { RawValue } from "../util/RawObject";
|
|
2
|
+
import { BasicLdSet } from "./ldSet/BasicLdSet";
|
|
3
|
+
import type { LdSet } from "./ldSet/LdSet";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates an LdSet used by LDO as a list of items.
|
|
7
|
+
* @param values The list of items in the set
|
|
8
|
+
* @returns An LdSet
|
|
9
|
+
*/
|
|
10
|
+
export function set<T>(...values: T[]): LdSet<T> {
|
|
11
|
+
return new BasicLdSet(
|
|
12
|
+
values as Iterable<NonNullable<RawValue>>,
|
|
13
|
+
) as unknown as LdSet<T>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GraphNode } from "@ldo/rdf-utils";
|
|
2
|
+
import type { BlankNode, Dataset, NamedNode } from "@rdfjs/types";
|
|
3
|
+
import type { ContextDefinition } from "jsonld";
|
|
4
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
5
|
+
import type {
|
|
6
|
+
_getUnderlyingDataset,
|
|
7
|
+
_getUnderlyingNode,
|
|
8
|
+
_proxyContext,
|
|
9
|
+
_writeGraphs,
|
|
10
|
+
} from "../types";
|
|
11
|
+
|
|
12
|
+
export type SubjectProxy = {
|
|
13
|
+
"@id"?: string;
|
|
14
|
+
"@context": ContextDefinition;
|
|
15
|
+
[key: string | number | symbol]: unknown;
|
|
16
|
+
readonly [_getUnderlyingDataset]: Dataset;
|
|
17
|
+
readonly [_getUnderlyingNode]: NamedNode | BlankNode;
|
|
18
|
+
[_proxyContext]: ProxyContext;
|
|
19
|
+
readonly [_writeGraphs]: GraphNode[];
|
|
20
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { blankNode, namedNode, quad } from "@rdfjs/data-model";
|
|
2
|
+
import type { BlankNode, NamedNode } from "@rdfjs/types";
|
|
3
|
+
import { addObjectToDataset } from "../util/addObjectToDataset";
|
|
4
|
+
import { deleteValueFromDataset } from "./deleteFromDataset";
|
|
5
|
+
import {
|
|
6
|
+
_getUnderlyingDataset,
|
|
7
|
+
_getUnderlyingNode,
|
|
8
|
+
_proxyContext,
|
|
9
|
+
_writeGraphs,
|
|
10
|
+
} from "../types";
|
|
11
|
+
import { getValueForKey } from "./getValueForKey";
|
|
12
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
13
|
+
|
|
14
|
+
export interface SubjectProxyTarget {
|
|
15
|
+
"@id": NamedNode | BlankNode;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function createSubjectHandler(
|
|
19
|
+
initialProxyContext: ProxyContext,
|
|
20
|
+
): ProxyHandler<SubjectProxyTarget> {
|
|
21
|
+
let proxyContext = initialProxyContext;
|
|
22
|
+
return {
|
|
23
|
+
get(target: SubjectProxyTarget, key: string | symbol) {
|
|
24
|
+
switch (key) {
|
|
25
|
+
case _getUnderlyingDataset:
|
|
26
|
+
return proxyContext.dataset;
|
|
27
|
+
case _getUnderlyingNode:
|
|
28
|
+
return target["@id"];
|
|
29
|
+
case _proxyContext:
|
|
30
|
+
return proxyContext;
|
|
31
|
+
case _writeGraphs:
|
|
32
|
+
return proxyContext.writeGraphs;
|
|
33
|
+
case "@context":
|
|
34
|
+
return proxyContext.contextUtil.context;
|
|
35
|
+
}
|
|
36
|
+
return getValueForKey(target, key, proxyContext);
|
|
37
|
+
},
|
|
38
|
+
getOwnPropertyDescriptor(target: SubjectProxyTarget, key: string) {
|
|
39
|
+
return {
|
|
40
|
+
value: getValueForKey(target, key, proxyContext),
|
|
41
|
+
writable: true,
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
ownKeys(target) {
|
|
47
|
+
const subject = target["@id"];
|
|
48
|
+
const tripleDataset = proxyContext.dataset.match(subject);
|
|
49
|
+
const keys: Set<string> = new Set(["@id"]);
|
|
50
|
+
tripleDataset.toArray().forEach((quad) => {
|
|
51
|
+
keys.add(
|
|
52
|
+
proxyContext.contextUtil.iriToKey(
|
|
53
|
+
quad.predicate.value,
|
|
54
|
+
proxyContext.getRdfType(subject),
|
|
55
|
+
),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
return Array.from(keys);
|
|
59
|
+
},
|
|
60
|
+
set: (target: SubjectProxyTarget, key, value) => {
|
|
61
|
+
if (key === _proxyContext) {
|
|
62
|
+
proxyContext = value;
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
key === "@id" &&
|
|
67
|
+
(typeof value === "string" || typeof value == "undefined")
|
|
68
|
+
) {
|
|
69
|
+
const newSubjectNode = value ? namedNode(value) : blankNode();
|
|
70
|
+
// Replace Subject Quads
|
|
71
|
+
const currentSubjectQuads = proxyContext.dataset
|
|
72
|
+
.match(target["@id"])
|
|
73
|
+
.toArray();
|
|
74
|
+
const newSubjectQuads = currentSubjectQuads.map((curQuad) =>
|
|
75
|
+
quad(
|
|
76
|
+
newSubjectNode,
|
|
77
|
+
curQuad.predicate,
|
|
78
|
+
curQuad.object,
|
|
79
|
+
curQuad.graph,
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
currentSubjectQuads.forEach((curQuad) =>
|
|
83
|
+
proxyContext.dataset.delete(curQuad),
|
|
84
|
+
);
|
|
85
|
+
proxyContext.dataset.addAll(newSubjectQuads);
|
|
86
|
+
// Replace Object Quads
|
|
87
|
+
const currentObjectQuads = proxyContext.dataset
|
|
88
|
+
.match(undefined, undefined, target["@id"])
|
|
89
|
+
.toArray();
|
|
90
|
+
const newObjectQuads = currentObjectQuads.map((curQuad) =>
|
|
91
|
+
quad(
|
|
92
|
+
curQuad.subject,
|
|
93
|
+
curQuad.predicate,
|
|
94
|
+
newSubjectNode,
|
|
95
|
+
curQuad.graph,
|
|
96
|
+
),
|
|
97
|
+
);
|
|
98
|
+
currentObjectQuads.forEach((curQuad) =>
|
|
99
|
+
proxyContext.dataset.delete(curQuad),
|
|
100
|
+
);
|
|
101
|
+
proxyContext.dataset.addAll(newObjectQuads);
|
|
102
|
+
target["@id"] = newSubjectNode;
|
|
103
|
+
}
|
|
104
|
+
addObjectToDataset(
|
|
105
|
+
{ "@id": target["@id"], [key]: value },
|
|
106
|
+
true,
|
|
107
|
+
proxyContext,
|
|
108
|
+
);
|
|
109
|
+
return true;
|
|
110
|
+
},
|
|
111
|
+
deleteProperty(target, key) {
|
|
112
|
+
return deleteValueFromDataset(target, key, proxyContext);
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SubjectProxyTarget } from "./createSubjectHandler";
|
|
2
|
+
import type { ProxyContext } from "../ProxyContext";
|
|
3
|
+
import { addObjectToDataset } from "../util/addObjectToDataset";
|
|
4
|
+
|
|
5
|
+
export function deleteValueFromDataset(
|
|
6
|
+
target: SubjectProxyTarget,
|
|
7
|
+
key: string | symbol,
|
|
8
|
+
proxyContext: ProxyContext,
|
|
9
|
+
) {
|
|
10
|
+
if (key === "@context") {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if (key === "toString" || key === Symbol.toStringTag) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
if (typeof key === "symbol") {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
// Remove this node completely if delete on ID
|
|
20
|
+
if (key === "@id") {
|
|
21
|
+
const thisNode = target["@id"];
|
|
22
|
+
proxyContext.dataset.deleteMatches(thisNode, undefined, undefined);
|
|
23
|
+
proxyContext.dataset.deleteMatches(undefined, undefined, thisNode);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
// Otherwise, this is essentially treated like setting a key to undefined.
|
|
27
|
+
addObjectToDataset(
|
|
28
|
+
{ "@id": target["@id"], [key]: undefined },
|
|
29
|
+
true,
|
|
30
|
+
proxyContext,
|
|
31
|
+
);
|
|
32
|
+
return true;
|
|
33
|
+
}
|