UNPKG

1.82 kBJavaScriptView Raw
1import { isQuad, DefaultDataFactory, isQuadLike } from "@opennetwork/rdf-data-model";
2import { ReadonlyDataset } from "./readonly-dataset.js";
3export class Dataset extends ReadonlyDataset {
4 constructor(set = new Set()) {
5 super(set);
6 this.#set = set;
7 }
8 #set;
9 add(value) {
10 const quad = isQuad(value) ? value : DefaultDataFactory.fromQuad(value);
11 if (this.#set.has ? this.#set.has(quad) : this.has(quad)) {
12 return this;
13 }
14 this.#set.add(quad);
15 return this;
16 }
17 addAll(dataset) {
18 for (const value of dataset) {
19 this.add(value);
20 }
21 return this;
22 }
23 async import(dataset, eager) {
24 const values = [];
25 for await (const value of dataset) {
26 if (eager) {
27 this.add(value);
28 }
29 else {
30 values.push(value);
31 }
32 }
33 this.addAll(values);
34 return undefined;
35 }
36 delete(quad) {
37 this.match(quad).forEach(this.deleteSource.bind(this));
38 return this;
39 }
40 deleteSource(quad) {
41 this.#set.delete(quad);
42 }
43 replace(replacing, replacers) {
44 const replacingDataset = new Set(new ReadonlyDataset().union((isQuad(replacing) || isQuadLike(replacing)) ? [replacing] : replacing).filter(quad => this.has(quad)));
45 if (!replacingDataset.size) {
46 return;
47 }
48 for (const toDelete of replacingDataset) {
49 this.delete(toDelete);
50 }
51 if (isQuad(replacers) || isQuadLike(replacers)) {
52 this.add(replacers);
53 }
54 else {
55 this.addAll(replacers);
56 }
57 return this;
58 }
59 get size() {
60 return this.#set.size;
61 }
62}
63//# sourceMappingURL=dataset.js.map
\No newline at end of file