@ldo/dataset 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.
Files changed (43) hide show
  1. package/dist/ExtendedDataset.d.ts +31 -0
  2. package/dist/ExtendedDataset.js +191 -0
  3. package/dist/ExtendedDataset.js.map +1 -0
  4. package/dist/ExtendedDatasetFactory.d.ts +7 -0
  5. package/dist/ExtendedDatasetFactory.js +16 -0
  6. package/dist/ExtendedDatasetFactory.js.map +1 -0
  7. package/dist/ProxyTransactionalDataset.d.ts +26 -0
  8. package/dist/ProxyTransactionalDataset.js +163 -0
  9. package/dist/ProxyTransactionalDataset.js.map +1 -0
  10. package/dist/WrapperSubscribableDataset.d.ts +57 -0
  11. package/dist/WrapperSubscribableDataset.js +269 -0
  12. package/dist/WrapperSubscribableDataset.js.map +1 -0
  13. package/dist/WrapperSubscribableDatasetFactory.d.ts +7 -0
  14. package/dist/WrapperSubscribableDatasetFactory.js +16 -0
  15. package/dist/WrapperSubscribableDatasetFactory.js.map +1 -0
  16. package/dist/createDatasetFromSerializedInput.d.ts +3 -0
  17. package/dist/createDatasetFromSerializedInput.js +9 -0
  18. package/dist/createDatasetFromSerializedInput.js.map +1 -0
  19. package/dist/createExtendedDataset.d.ts +5 -0
  20. package/dist/createExtendedDataset.js +23 -0
  21. package/dist/createExtendedDataset.js.map +1 -0
  22. package/dist/createExtendedDatasetFromSerializedInput.d.ts +4 -0
  23. package/dist/createExtendedDatasetFromSerializedInput.js +13 -0
  24. package/dist/createExtendedDatasetFromSerializedInput.js.map +1 -0
  25. package/dist/createWrapperSubscribableDataset.d.ts +5 -0
  26. package/dist/createWrapperSubscribableDataset.js +23 -0
  27. package/dist/createWrapperSubscribableDataset.js.map +1 -0
  28. package/dist/createWrapperSubscribableDatasetFromSerializedInput.d.ts +4 -0
  29. package/dist/createWrapperSubscribableDatasetFromSerializedInput.js +13 -0
  30. package/dist/createWrapperSubscribableDatasetFromSerializedInput.js.map +1 -0
  31. package/dist/index.d.ts +5 -0
  32. package/dist/index.js +18 -0
  33. package/dist/index.js.map +1 -0
  34. package/dist/types.d.ts +33 -0
  35. package/dist/types.js +3 -0
  36. package/dist/types.js.map +1 -0
  37. package/package.json +50 -0
  38. package/src/ExtendedDataset.ts +394 -0
  39. package/src/ExtendedDatasetFactory.ts +34 -0
  40. package/src/createDatasetFromSerializedInput.ts +26 -0
  41. package/src/createExtendedDataset.ts +36 -0
  42. package/src/createExtendedDatasetFromSerializedInput.ts +28 -0
  43. package/src/index.ts +9 -0
@@ -0,0 +1,394 @@
1
+ import {
2
+ type DatasetCore,
3
+ type Dataset,
4
+ type BaseQuad,
5
+ type Stream,
6
+ type Term,
7
+ type DatasetCoreFactory,
8
+ type Quad,
9
+ } from "@rdfjs/types";
10
+ import { datasetToString } from "@ldo/rdf-utils";
11
+ import { Readable } from "readable-stream";
12
+
13
+ /**
14
+ * A full implementation of the RDF JS Dataset interface.
15
+ */
16
+ export default class ExtendedDataset<InAndOutQuad extends BaseQuad = BaseQuad>
17
+ implements Dataset<InAndOutQuad, InAndOutQuad>
18
+ {
19
+ /**
20
+ * The main backing dataset
21
+ */
22
+ protected dataset: DatasetCore<InAndOutQuad, InAndOutQuad>;
23
+
24
+ /**
25
+ * A factory that generates datasets for the methods
26
+ */
27
+ protected datasetCoreFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>;
28
+
29
+ /**
30
+ * Constructor
31
+ */
32
+ constructor(
33
+ dataset: DatasetCore<InAndOutQuad, InAndOutQuad>,
34
+ datasetFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>,
35
+ ) {
36
+ this.dataset = dataset;
37
+ this.datasetCoreFactory = datasetFactory;
38
+ }
39
+
40
+ /**
41
+ * Creates a blank dataset using the dataset factory
42
+ */
43
+ private createBlankDataset(): Dataset<InAndOutQuad, InAndOutQuad> {
44
+ return new ExtendedDataset<InAndOutQuad>(
45
+ this.datasetCoreFactory.dataset(),
46
+ this.datasetCoreFactory,
47
+ );
48
+ }
49
+
50
+ /**
51
+ * Imports the quads into this dataset.
52
+ * This method differs from Dataset.union in that it adds all quads to the current instance, rather than combining quads and the current instance to create a new instance.
53
+ * @param quads
54
+ * @returns the dataset instance it was called on.
55
+ */
56
+ addAll(quads: InAndOutQuad[] | Dataset<InAndOutQuad>): this {
57
+ for (const quad of quads) {
58
+ this.add(quad);
59
+ }
60
+ return this;
61
+ }
62
+
63
+ /**
64
+ * Returns true if the current instance is a superset of the given dataset; differently put: if the given dataset is a subset of, is contained in the current dataset.
65
+ * Blank Nodes will be normalized.
66
+ * @param other
67
+ */
68
+ contains(other: Dataset<InAndOutQuad>): boolean {
69
+ if (other.size > this.size) {
70
+ return false;
71
+ }
72
+ for (const quad of other) {
73
+ if (!this.has(quad)) {
74
+ return false;
75
+ }
76
+ }
77
+ return true;
78
+ }
79
+
80
+ /**
81
+ * This method removes the quads in the current instance that match the given arguments. The logic described in Quad Matching is applied for each quad in this dataset to select the quads which will be deleted.
82
+ * @param subject
83
+ * @param predicate
84
+ * @param object
85
+ * @param graph
86
+ * @returns the dataset instance it was called on.
87
+ */
88
+ deleteMatches(
89
+ subject?: Term,
90
+ predicate?: Term,
91
+ object?: Term,
92
+ graph?: Term,
93
+ ): this {
94
+ const matching = this.match(subject, predicate, object, graph);
95
+ for (const quad of matching) {
96
+ this.dataset.delete(quad);
97
+ }
98
+ return this;
99
+ }
100
+
101
+ /**
102
+ * Returns a new dataset that contains alls quads from the current dataset, not included in the given dataset.
103
+ * @param other
104
+ */
105
+ difference(
106
+ other: DatasetCore<InAndOutQuad>,
107
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
108
+ const dataset = this.createBlankDataset();
109
+ for (const quad of this) {
110
+ if (!other.has(quad)) {
111
+ dataset.add(quad);
112
+ }
113
+ }
114
+ return dataset;
115
+ }
116
+
117
+ /**
118
+ * Returns true if the current instance contains the same graph structure as the given dataset.
119
+ * @param other
120
+ */
121
+ equals(other: Dataset<InAndOutQuad, InAndOutQuad>): boolean {
122
+ if (this.size !== other.size) {
123
+ return false;
124
+ }
125
+ for (const quad of this) {
126
+ if (!other.has(quad)) {
127
+ return false;
128
+ }
129
+ }
130
+ return true;
131
+ }
132
+
133
+ /**
134
+ * Universal quantification method, tests whether every quad in the dataset passes the test implemented by the provided iteratee.
135
+ * This method immediately returns boolean false once a quad that does not pass the test is found.
136
+ * This method always returns boolean true on an empty dataset.
137
+ * Note: This method is aligned with Array.prototype.every() in ECMAScript-262.
138
+ * @param iteratee
139
+ */
140
+ every(iteratee: (quad: InAndOutQuad, dataset: this) => boolean): boolean {
141
+ for (const quad of this) {
142
+ if (!iteratee(quad, this)) {
143
+ return false;
144
+ }
145
+ }
146
+ return true;
147
+ }
148
+
149
+ /**
150
+ * Creates a new dataset with all the quads that pass the test implemented by the provided iteratee.
151
+ * Note: This method is aligned with Array.prototype.filter() in ECMAScript-262.
152
+ * @param iteratee
153
+ */
154
+ filter(
155
+ iteratee: (quad: InAndOutQuad, dataset: this) => boolean,
156
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
157
+ const dataset = this.createBlankDataset();
158
+ for (const quad of this) {
159
+ if (iteratee(quad, this)) {
160
+ dataset.add(quad);
161
+ }
162
+ }
163
+ return dataset;
164
+ }
165
+
166
+ /**
167
+ * Executes the provided iteratee once on each quad in the dataset.
168
+ * Note: This method is aligned with Array.prototype.forEach() in ECMAScript-262.
169
+ * @param iteratee
170
+ */
171
+ forEach(iteratee: (quad: InAndOutQuad, dataset: this) => void): void {
172
+ for (const quad of this) {
173
+ iteratee(quad, this);
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Imports all quads from the given stream into the dataset.
179
+ * The stream events end and error are wrapped in a Promise.
180
+ * @param stream
181
+ */
182
+ import(stream: Stream<InAndOutQuad>): Promise<this> {
183
+ return new Promise((resolve, reject) => {
184
+ stream
185
+ .on("data", (quad) => {
186
+ this.add(quad);
187
+ })
188
+ .on("end", () => {
189
+ resolve(this);
190
+ })
191
+ .on("error", (err) => reject(err));
192
+ });
193
+ }
194
+
195
+ /**
196
+ * Returns a new dataset containing alls quads from the current dataset that are also included in the given dataset.
197
+ * @param other
198
+ */
199
+ intersection(
200
+ other: Dataset<InAndOutQuad, InAndOutQuad>,
201
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
202
+ const dataset = this.createBlankDataset();
203
+ const iteratingDataset = this.size < other.size ? this : other;
204
+ const comparingDataset = this.size < other.size ? other : this;
205
+ for (const quad of iteratingDataset) {
206
+ if (comparingDataset.has(quad)) {
207
+ dataset.add(quad);
208
+ }
209
+ }
210
+ return dataset;
211
+ }
212
+
213
+ /**
214
+ * Returns a new dataset containing all quads returned by applying iteratee to each quad in the current dataset.
215
+ * @param iteratee
216
+ */
217
+ map(
218
+ iteratee: (quad: InAndOutQuad, dataset: this) => InAndOutQuad,
219
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
220
+ const dataset = this.createBlankDataset();
221
+ for (const quad of this) {
222
+ dataset.add(iteratee(quad, this));
223
+ }
224
+ return dataset;
225
+ }
226
+
227
+ /**
228
+ * This method calls the iteratee on each quad of the DatasetCore. The first time the iteratee is called, the accumulator value is the initialValue or, if not given, equals to the first quad of the Dataset. The return value of the iteratee is used as accumulator value for the next calls.
229
+ * This method returns the return value of the last iteratee call.
230
+ * Note: This method is aligned with Array.prototype.reduce() in ECMAScript-262.
231
+ * @param iteratee
232
+ * @param initialValue
233
+ */
234
+ reduce<A = unknown>(
235
+ iteratee: (accumulator: A, quad: InAndOutQuad, dataset: this) => A,
236
+ initialValue?: A,
237
+ ): A {
238
+ if (this.size === 0 && initialValue == undefined) {
239
+ throw new Error(
240
+ "Cannot reduce an empty Dataset without an initial value.",
241
+ );
242
+ }
243
+ const thisIterator: Iterator<InAndOutQuad> = this[Symbol.iterator]();
244
+ let iteratorResult = thisIterator.next();
245
+ let accumulatedValue: A = initialValue as A;
246
+ while (!iteratorResult.done) {
247
+ accumulatedValue = iteratee(accumulatedValue, iteratorResult.value, this);
248
+ iteratorResult = thisIterator.next();
249
+ }
250
+ return accumulatedValue;
251
+ }
252
+
253
+ /**
254
+ * Existential quantification method, tests whether some quads in the dataset pass the test implemented by the provided iteratee.
255
+ * Note: This method is aligned with Array.prototype.some() in ECMAScript-262.
256
+ * @param iteratee
257
+ * @returns boolean true once a quad that passes the test is found.
258
+ */
259
+ some(iteratee: (quad: InAndOutQuad, dataset: this) => boolean): boolean {
260
+ for (const quad of this) {
261
+ if (iteratee(quad, this)) {
262
+ return true;
263
+ }
264
+ }
265
+ return false;
266
+ }
267
+
268
+ /**
269
+ * Returns the set of quads within the dataset as a host language native sequence, for example an Array in ECMAScript-262.
270
+ * Note: Since a DatasetCore is an unordered set, the order of the quads within the returned sequence is arbitrary.
271
+ */
272
+ toArray(): InAndOutQuad[] {
273
+ const array: InAndOutQuad[] = [];
274
+ for (const quad of this) {
275
+ array.push(quad);
276
+ }
277
+ return array;
278
+ }
279
+
280
+ /**
281
+ * Returns an N-Quads string representation of the dataset, preprocessed with RDF Dataset Normalization algorithm.
282
+ */
283
+ toCanonical(): string {
284
+ throw new Error("Method not implemented.");
285
+ }
286
+
287
+ /**
288
+ * Returns a stream that contains all quads of the dataset.
289
+ */
290
+ toStream(): Stream<InAndOutQuad> {
291
+ const iterator = this[Symbol.iterator]();
292
+ let curNext = iterator.next();
293
+ const stream = new Readable({
294
+ objectMode: true,
295
+ read() {
296
+ if (curNext.done || !curNext.value) {
297
+ this.push(null);
298
+ return;
299
+ }
300
+ this.push(curNext.value);
301
+ curNext = iterator.next();
302
+ },
303
+ });
304
+ return stream;
305
+ }
306
+
307
+ /**
308
+ * Returns an N-Quads string representation of the dataset.
309
+ * No prior normalization is required, therefore the results for the same quads may vary depending on the Dataset implementation.
310
+ */
311
+ toString(): string {
312
+ return datasetToString(this as Dataset<Quad>, { format: "N-Triples" });
313
+ }
314
+
315
+ /**
316
+ * Returns a new Dataset that is a concatenation of this dataset and the quads given as an argument.
317
+ * @param other
318
+ */
319
+ union(
320
+ other: Dataset<InAndOutQuad, InAndOutQuad>,
321
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
322
+ const dataset = this.createBlankDataset();
323
+ for (const quad of this) {
324
+ dataset.add(quad);
325
+ }
326
+ for (const quad of other) {
327
+ dataset.add(quad);
328
+ }
329
+ return dataset;
330
+ }
331
+
332
+ /**
333
+ * This method returns a new dataset that is comprised of all quads in the current instance matching the given arguments. The logic described in Quad Matching is applied for each quad in this dataset to check if it should be included in the output dataset.
334
+ * @param subject
335
+ * @param predicate
336
+ * @param object
337
+ * @param graph
338
+ * @returns a Dataset with matching triples
339
+ */
340
+ match(
341
+ subject?: Term | null,
342
+ predicate?: Term | null,
343
+ object?: Term | null,
344
+ graph?: Term | null,
345
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
346
+ return new ExtendedDataset(
347
+ this.dataset.match(subject, predicate, object, graph),
348
+ this.datasetCoreFactory,
349
+ );
350
+ }
351
+
352
+ /**
353
+ * A non-negative integer that specifies the number of quads in the set.
354
+ */
355
+ public get size(): number {
356
+ return this.dataset.size;
357
+ }
358
+
359
+ /**
360
+ * Adds the specified quad to the dataset.
361
+ * Existing quads, as defined in Quad.equals, will be ignored.
362
+ * @param quad
363
+ * @returns the dataset instance it was called on.
364
+ */
365
+ public add(quad: InAndOutQuad): this {
366
+ this.dataset.add(quad);
367
+ return this;
368
+ }
369
+
370
+ /**
371
+ * Removes the specified quad from the dataset.
372
+ * This method returns the dataset instance it was called on.
373
+ * @param quad
374
+ */
375
+ public delete(quad: InAndOutQuad): this {
376
+ this.dataset.delete(quad);
377
+ return this;
378
+ }
379
+
380
+ /**
381
+ * Determines whether a dataset includes a certain quad, returning true or false as appropriate.
382
+ * @param quad
383
+ */
384
+ public has(quad: InAndOutQuad): boolean {
385
+ return Boolean(this.dataset.has(quad));
386
+ }
387
+
388
+ /**
389
+ * Returns an iterator
390
+ */
391
+ public [Symbol.iterator](): Iterator<InAndOutQuad, InAndOutQuad, undefined> {
392
+ return this.dataset[Symbol.iterator]();
393
+ }
394
+ }
@@ -0,0 +1,34 @@
1
+ import type {
2
+ DatasetFactory,
3
+ BaseQuad,
4
+ Dataset,
5
+ DatasetCoreFactory,
6
+ } from "@rdfjs/types";
7
+ import ExtendedDataset from "./ExtendedDataset";
8
+
9
+ /**
10
+ * A DatasetFactory that creates an ExtendedDataset given a DatasetCoreFactory.
11
+ */
12
+ export default class ExtendedDatasetFactory<
13
+ InAndOutQuad extends BaseQuad = BaseQuad,
14
+ > implements DatasetFactory<InAndOutQuad, InAndOutQuad>
15
+ {
16
+ private datasetCoreFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>;
17
+ constructor(
18
+ datasetCoreFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>,
19
+ ) {
20
+ this.datasetCoreFactory = datasetCoreFactory;
21
+ }
22
+
23
+ dataset(
24
+ quads?: Dataset<InAndOutQuad, InAndOutQuad> | InAndOutQuad[],
25
+ ): ExtendedDataset<InAndOutQuad> {
26
+ return new ExtendedDataset(
27
+ // The typings are incorrect on the dataset core factory
28
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
29
+ // @ts-ignore
30
+ this.datasetCoreFactory.dataset(quads),
31
+ this.datasetCoreFactory,
32
+ );
33
+ }
34
+ }
@@ -0,0 +1,26 @@
1
+ import type { Dataset, DatasetFactory, Quad } from "@rdfjs/types";
2
+ import type { ParserOptions } from "@ldo/rdf-utils";
3
+ import { serializedToQuads } from "@ldo/rdf-utils";
4
+
5
+ /**
6
+ * Creates a dataset with a string input that could be JON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
7
+ * @param datasetFactory A datasetFactory that will initialize a returned dataset.\
8
+ * @param data A string representation of RDF Data in JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
9
+ * @param options Parser options: {
10
+ * format?: string;
11
+ * factory?: RDF.DataFactory;
12
+ * baseIRI?: string;
13
+ * blankNodePrefix?: string;
14
+ * }
15
+ * @returns A dataset
16
+ */
17
+ export default async function createDatasetFromSerializedInput<
18
+ ReturnDataset extends Dataset = Dataset,
19
+ >(
20
+ datasetFactory: DatasetFactory<Quad>,
21
+ data: string,
22
+ options?: ParserOptions,
23
+ ): Promise<ReturnDataset> {
24
+ const quads = await serializedToQuads(data, options);
25
+ return datasetFactory.dataset(quads) as unknown as ReturnDataset;
26
+ }
@@ -0,0 +1,36 @@
1
+ import type {
2
+ Dataset,
3
+ DatasetCoreFactory,
4
+ DatasetCore,
5
+ Quad,
6
+ } from "@rdfjs/types";
7
+ import ExtendedDatasetFactory from "./ExtendedDatasetFactory";
8
+ import { dataset as initializeDatasetCore } from "@rdfjs/dataset";
9
+ import type ExtendedDataset from "./ExtendedDataset";
10
+
11
+ /**
12
+ * Creates a dataset factory that generates ExtendedDatasets
13
+ * @returns DatasetFactory
14
+ */
15
+ export function createExtendedDatasetFactory(): ExtendedDatasetFactory<Quad> {
16
+ const datasetFactory: DatasetCoreFactory<Quad> = {
17
+ dataset: (quads?: Dataset<Quad> | Quad[]): DatasetCore<Quad> => {
18
+ return initializeDatasetCore<Quad>(
19
+ Array.isArray(quads) ? quads : quads?.toArray(),
20
+ );
21
+ },
22
+ };
23
+ return new ExtendedDatasetFactory<Quad>(datasetFactory);
24
+ }
25
+
26
+ /**
27
+ * Creates an ExtendedDataset
28
+ * @param quads: A dataset or array of Quads to initialize the dataset.
29
+ * @returns Dataset
30
+ */
31
+ export default function createExtendedDataset(
32
+ quads?: Dataset<Quad> | Quad[],
33
+ ): ExtendedDataset<Quad> {
34
+ const extendedDatasetFactory = createExtendedDatasetFactory();
35
+ return extendedDatasetFactory.dataset(quads);
36
+ }
@@ -0,0 +1,28 @@
1
+ import type { Quad } from "@rdfjs/types";
2
+ import type { ParserOptions } from "@ldo/rdf-utils";
3
+ import createDatasetFromSerializedInput from "./createDatasetFromSerializedInput";
4
+ import { createExtendedDatasetFactory } from "./createExtendedDataset";
5
+ import type ExtendedDataset from "./ExtendedDataset";
6
+
7
+ /**
8
+ * Creates an ExtendedDataset with a string input that could be JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
9
+ * @param data A string representation of RDF Data in JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
10
+ * @param options Parser options: {
11
+ * format?: string;
12
+ * factory?: RDF.DataFactory;
13
+ * baseIRI?: string;
14
+ * blankNodePrefix?: string;
15
+ * }
16
+ * @returns A dataset
17
+ */
18
+ export default async function createExtendedDatasetFromSerializedInput(
19
+ data: string,
20
+ options?: ParserOptions,
21
+ ): Promise<ExtendedDataset<Quad>> {
22
+ const datasetFactory = createExtendedDatasetFactory();
23
+ return createDatasetFromSerializedInput<ExtendedDataset<Quad>>(
24
+ datasetFactory,
25
+ data,
26
+ options,
27
+ );
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export {
2
+ default as createDataset,
3
+ createExtendedDatasetFactory as createDatasetFactory,
4
+ } from "./createExtendedDataset";
5
+ export { default as createDatasetFromSerializedInput } from "./createDatasetFromSerializedInput";
6
+ export { default as serializedToDataset } from "./createExtendedDatasetFromSerializedInput";
7
+
8
+ export { default as ExtendedDataset } from "./ExtendedDataset";
9
+ export { default as ExtendedDatasetFactory } from "./ExtendedDatasetFactory";