@ldo/dataset 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Jackson Morgan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/Readme.md ADDED
@@ -0,0 +1,106 @@
1
+ # Dataset
2
+
3
+ An implementation of the full [RDF/JS Dataset API](https://rdf.js.org/dataset-spec/)
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ npm i @ldo/dataset
9
+ ```
10
+
11
+ ## Simple Example
12
+
13
+ ```typescript
14
+ import { createDataset } from "@ldo/dataset";
15
+ import { quad, namedNode } from "@rdfjs/data-model";
16
+ const dataset = createDataset();
17
+ dataset.add(
18
+ quad(
19
+ namedNode("http://example.org/cartoons#Zuko"),
20
+ namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
21
+ namedNode("http://example.org/cartoons#Firebender")
22
+ )
23
+ );
24
+ /*
25
+ Prints:
26
+ <http://example.org/cartoons#Zuko> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.org/cartoons#Firebender> .
27
+ */
28
+ console.log(dataset.toString());
29
+ ```
30
+
31
+ ## Advanced Example
32
+
33
+ The extended dataset implements all the [additional methods](https://rdf.js.org/dataset-spec/#dataset-interface) of the RDFJS dataset spec.
34
+
35
+ Usage:
36
+ ```typescript
37
+ import { createDataset } from "@ldo/dataset";
38
+ import { quad, namedNode, literal } from "@rdfjs/data-model";
39
+ // Required for advanced features:
40
+ import { dataset as initializeDatasetCore } from "@rdfjs/dataset";
41
+ import { ExtendedDatasetFactory } from "@ldo/dataset";
42
+ import { Dataset, Quad, DatasetCoreFactory, DatasetCore } from "@rdfjs/types";
43
+
44
+ /**
45
+ * Create a dataset with default settings
46
+ */
47
+ const defaultDataset = createDataset();
48
+
49
+ /**
50
+ * Create a dataset with default settings and initialized values
51
+ */
52
+ const initializedQuads = [
53
+ quad(
54
+ namedNode("http://example.org/cartoons#Tom"),
55
+ namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
56
+ namedNode("http://example.org/cartoons#Cat")
57
+ ),
58
+ quad(
59
+ namedNode("http://example.org/cartoons#Tom"),
60
+ namedNode("http://example.org/cartoons#name"),
61
+ literal("Tom")
62
+ ),
63
+ ];
64
+ const defaultDataset2 = createDataset(initializedQuads);
65
+
66
+ /**
67
+ * (Advanced Feature) Create a dataset by injecting a chosen datasetCore and datasetCoreFactory
68
+ */
69
+ const datasetFactory: DatasetCoreFactory = {
70
+ dataset: (quads?: Dataset<Quad> | Quad[]): DatasetCore => {
71
+ return initializeDatasetCore(
72
+ Array.isArray(quads) ? quads : quads?.toArray()
73
+ );
74
+ },
75
+ };
76
+ const extendedDatasetFactory = new ExtendedDatasetFactory(datasetFactory);
77
+ const customDataset = extendedDatasetFactory.dataset(initializedQuads);
78
+
79
+ /**
80
+ * Do all the methods of the RDFJS Dataset interface. For a full list of methods, go to
81
+ * https://rdf.js.org/dataset-spec/#data-interfaces
82
+ */
83
+ defaultDataset.add(
84
+ quad(
85
+ namedNode("http://example.org/cartoons#Miuki"),
86
+ namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
87
+ namedNode("http://example.org/cartoons#Cat")
88
+ )
89
+ );
90
+ const combinedDataset = defaultDataset.union(defaultDataset2);
91
+ const differenceDataset = combinedDataset.difference(customDataset);
92
+ // Prints true because "defaultDataset2" and "customDataset" have equal values
93
+ // combinedDataset = defaultDataset ∪ defaultDataset2
94
+ // differenceDatasset = defaultDataset \ customDataset
95
+ // Therefore differenceDataset == defaultDataset
96
+ console.log(differenceDataset.equals(defaultDataset));
97
+ ```
98
+
99
+ ## Sponsorship
100
+ This project was made possible by a grant from NGI Zero Entrust via nlnet. Learn more on the [NLnet project page](https://nlnet.nl/project/SolidUsableApps/).
101
+
102
+ [<img src="https://nlnet.nl/logo/banner.png" alt="nlnet foundation logo" width="300" />](https://nlnet.nl/)
103
+ [<img src="https://nlnet.nl/image/logos/NGI0Entrust_tag.svg" alt="NGI Zero Entrust Logo" width="300" />](https://nlnet.nl/)
104
+
105
+ ## Liscense
106
+ MIT
@@ -0,0 +1,31 @@
1
+ import { type DatasetCore, type Dataset, type BaseQuad, type Stream, type Term, type DatasetCoreFactory } from "@rdfjs/types";
2
+ export default class ExtendedDataset<InAndOutQuad extends BaseQuad = BaseQuad> implements Dataset<InAndOutQuad, InAndOutQuad> {
3
+ protected dataset: DatasetCore<InAndOutQuad, InAndOutQuad>;
4
+ protected datasetCoreFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>;
5
+ constructor(dataset: DatasetCore<InAndOutQuad, InAndOutQuad>, datasetFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>);
6
+ private createBlankDataset;
7
+ addAll(quads: InAndOutQuad[] | Dataset<InAndOutQuad>): this;
8
+ contains(other: Dataset<InAndOutQuad>): boolean;
9
+ deleteMatches(subject?: Term, predicate?: Term, object?: Term, graph?: Term): this;
10
+ difference(other: DatasetCore<InAndOutQuad>): Dataset<InAndOutQuad, InAndOutQuad>;
11
+ equals(other: Dataset<InAndOutQuad, InAndOutQuad>): boolean;
12
+ every(iteratee: (quad: InAndOutQuad, dataset: this) => boolean): boolean;
13
+ filter(iteratee: (quad: InAndOutQuad, dataset: this) => boolean): Dataset<InAndOutQuad, InAndOutQuad>;
14
+ forEach(iteratee: (quad: InAndOutQuad, dataset: this) => void): void;
15
+ import(stream: Stream<InAndOutQuad>): Promise<this>;
16
+ intersection(other: Dataset<InAndOutQuad, InAndOutQuad>): Dataset<InAndOutQuad, InAndOutQuad>;
17
+ map(iteratee: (quad: InAndOutQuad, dataset: this) => InAndOutQuad): Dataset<InAndOutQuad, InAndOutQuad>;
18
+ reduce<A = unknown>(iteratee: (accumulator: A, quad: InAndOutQuad, dataset: this) => A, initialValue?: A): A;
19
+ some(iteratee: (quad: InAndOutQuad, dataset: this) => boolean): boolean;
20
+ toArray(): InAndOutQuad[];
21
+ toCanonical(): string;
22
+ toStream(): Stream<InAndOutQuad>;
23
+ toString(): string;
24
+ union(other: Dataset<InAndOutQuad, InAndOutQuad>): Dataset<InAndOutQuad, InAndOutQuad>;
25
+ match(subject?: Term | null, predicate?: Term | null, object?: Term | null, graph?: Term | null): Dataset<InAndOutQuad, InAndOutQuad>;
26
+ get size(): number;
27
+ add(quad: InAndOutQuad): this;
28
+ delete(quad: InAndOutQuad): this;
29
+ has(quad: InAndOutQuad): boolean;
30
+ [Symbol.iterator](): Iterator<InAndOutQuad, InAndOutQuad, undefined>;
31
+ }
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const rdf_utils_1 = require("@ldo/rdf-utils");
4
+ const readable_stream_1 = require("readable-stream");
5
+ class ExtendedDataset {
6
+ constructor(dataset, datasetFactory) {
7
+ this.dataset = dataset;
8
+ this.datasetCoreFactory = datasetFactory;
9
+ }
10
+ createBlankDataset() {
11
+ return new ExtendedDataset(this.datasetCoreFactory.dataset(), this.datasetCoreFactory);
12
+ }
13
+ addAll(quads) {
14
+ for (const quad of quads) {
15
+ this.add(quad);
16
+ }
17
+ return this;
18
+ }
19
+ contains(other) {
20
+ if (other.size > this.size) {
21
+ return false;
22
+ }
23
+ for (const quad of other) {
24
+ if (!this.has(quad)) {
25
+ return false;
26
+ }
27
+ }
28
+ return true;
29
+ }
30
+ deleteMatches(subject, predicate, object, graph) {
31
+ const matching = this.match(subject, predicate, object, graph);
32
+ for (const quad of matching) {
33
+ this.dataset.delete(quad);
34
+ }
35
+ return this;
36
+ }
37
+ difference(other) {
38
+ const dataset = this.createBlankDataset();
39
+ for (const quad of this) {
40
+ if (!other.has(quad)) {
41
+ dataset.add(quad);
42
+ }
43
+ }
44
+ return dataset;
45
+ }
46
+ equals(other) {
47
+ if (this.size !== other.size) {
48
+ return false;
49
+ }
50
+ for (const quad of this) {
51
+ if (!other.has(quad)) {
52
+ return false;
53
+ }
54
+ }
55
+ return true;
56
+ }
57
+ every(iteratee) {
58
+ for (const quad of this) {
59
+ if (!iteratee(quad, this)) {
60
+ return false;
61
+ }
62
+ }
63
+ return true;
64
+ }
65
+ filter(iteratee) {
66
+ const dataset = this.createBlankDataset();
67
+ for (const quad of this) {
68
+ if (iteratee(quad, this)) {
69
+ dataset.add(quad);
70
+ }
71
+ }
72
+ return dataset;
73
+ }
74
+ forEach(iteratee) {
75
+ for (const quad of this) {
76
+ iteratee(quad, this);
77
+ }
78
+ }
79
+ import(stream) {
80
+ return new Promise((resolve, reject) => {
81
+ stream
82
+ .on("data", (quad) => {
83
+ this.add(quad);
84
+ })
85
+ .on("end", () => {
86
+ resolve(this);
87
+ })
88
+ .on("error", (err) => reject(err));
89
+ });
90
+ }
91
+ intersection(other) {
92
+ const dataset = this.createBlankDataset();
93
+ const iteratingDataset = this.size < other.size ? this : other;
94
+ const comparingDataset = this.size < other.size ? other : this;
95
+ for (const quad of iteratingDataset) {
96
+ if (comparingDataset.has(quad)) {
97
+ dataset.add(quad);
98
+ }
99
+ }
100
+ return dataset;
101
+ }
102
+ map(iteratee) {
103
+ const dataset = this.createBlankDataset();
104
+ for (const quad of this) {
105
+ dataset.add(iteratee(quad, this));
106
+ }
107
+ return dataset;
108
+ }
109
+ reduce(iteratee, initialValue) {
110
+ if (this.size === 0 && initialValue == undefined) {
111
+ throw new Error("Cannot reduce an empty Dataset without an initial value.");
112
+ }
113
+ const thisIterator = this[Symbol.iterator]();
114
+ let iteratorResult = thisIterator.next();
115
+ let accumulatedValue = initialValue;
116
+ while (!iteratorResult.done) {
117
+ accumulatedValue = iteratee(accumulatedValue, iteratorResult.value, this);
118
+ iteratorResult = thisIterator.next();
119
+ }
120
+ return accumulatedValue;
121
+ }
122
+ some(iteratee) {
123
+ for (const quad of this) {
124
+ if (iteratee(quad, this)) {
125
+ return true;
126
+ }
127
+ }
128
+ return false;
129
+ }
130
+ toArray() {
131
+ const array = [];
132
+ for (const quad of this) {
133
+ array.push(quad);
134
+ }
135
+ return array;
136
+ }
137
+ toCanonical() {
138
+ throw new Error("Method not implemented.");
139
+ }
140
+ toStream() {
141
+ const iterator = this[Symbol.iterator]();
142
+ let curNext = iterator.next();
143
+ const stream = new readable_stream_1.Readable({
144
+ objectMode: true,
145
+ read() {
146
+ if (curNext.done || !curNext.value) {
147
+ this.push(null);
148
+ return;
149
+ }
150
+ this.push(curNext.value);
151
+ curNext = iterator.next();
152
+ },
153
+ });
154
+ return stream;
155
+ }
156
+ toString() {
157
+ return (0, rdf_utils_1.datasetToString)(this, { format: "N-Triples" });
158
+ }
159
+ union(other) {
160
+ const dataset = this.createBlankDataset();
161
+ for (const quad of this) {
162
+ dataset.add(quad);
163
+ }
164
+ for (const quad of other) {
165
+ dataset.add(quad);
166
+ }
167
+ return dataset;
168
+ }
169
+ match(subject, predicate, object, graph) {
170
+ return new ExtendedDataset(this.dataset.match(subject, predicate, object, graph), this.datasetCoreFactory);
171
+ }
172
+ get size() {
173
+ return this.dataset.size;
174
+ }
175
+ add(quad) {
176
+ this.dataset.add(quad);
177
+ return this;
178
+ }
179
+ delete(quad) {
180
+ this.dataset.delete(quad);
181
+ return this;
182
+ }
183
+ has(quad) {
184
+ return Boolean(this.dataset.has(quad));
185
+ }
186
+ [Symbol.iterator]() {
187
+ return this.dataset[Symbol.iterator]();
188
+ }
189
+ }
190
+ exports.default = ExtendedDataset;
191
+ //# sourceMappingURL=ExtendedDataset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExtendedDataset.js","sourceRoot":"","sources":["../src/ExtendedDataset.ts"],"names":[],"mappings":";;AASA,8CAAiD;AACjD,qDAA2C;AAK3C,MAAqB,eAAe;IAgBlC,YACE,OAAgD,EAChD,cAA8D;QAE9D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,kBAAkB,GAAG,cAAc,CAAC;IAC3C,CAAC;IAKO,kBAAkB;QACxB,OAAO,IAAI,eAAe,CACxB,IAAI,CAAC,kBAAkB,CAAC,OAAO,EAAE,EACjC,IAAI,CAAC,kBAAkB,CACxB,CAAC;IACJ,CAAC;IAQD,MAAM,CAAC,KAA6C;QAClD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;YACxB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;SAChB;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAOD,QAAQ,CAAC,KAA4B;QACnC,IAAI,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE;YAC1B,OAAO,KAAK,CAAC;SACd;QACD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;YACxB,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;gBACnB,OAAO,KAAK,CAAC;aACd;SACF;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAUD,aAAa,CACX,OAAc,EACd,SAAgB,EAChB,MAAa,EAC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C;IAQM,GAAG,CAAC,IAAkB;QAC3B,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACvB,OAAO,IAAI,CAAC;IACd,CAAC;IAOM,MAAM,CAAC,IAAkB;QAC9B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IAMM,GAAG,CAAC,IAAkB;QAC3B,OAAO,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IACzC,CAAC;IAKM,CAAC,MAAM,CAAC,QAAQ,CAAC;QACtB,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC;IACzC,CAAC;CACF;AA1XD,kCA0XC"}
@@ -0,0 +1,7 @@
1
+ import type { DatasetFactory, BaseQuad, Dataset, DatasetCoreFactory } from "@rdfjs/types";
2
+ import ExtendedDataset from "./ExtendedDataset";
3
+ export default class ExtendedDatasetFactory<InAndOutQuad extends BaseQuad = BaseQuad> implements DatasetFactory<InAndOutQuad, InAndOutQuad> {
4
+ private datasetCoreFactory;
5
+ constructor(datasetCoreFactory: DatasetCoreFactory<InAndOutQuad, InAndOutQuad>);
6
+ dataset(quads?: Dataset<InAndOutQuad, InAndOutQuad> | InAndOutQuad[]): ExtendedDataset<InAndOutQuad>;
7
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const ExtendedDataset_1 = __importDefault(require("./ExtendedDataset"));
7
+ class ExtendedDatasetFactory {
8
+ constructor(datasetCoreFactory) {
9
+ this.datasetCoreFactory = datasetCoreFactory;
10
+ }
11
+ dataset(quads) {
12
+ return new ExtendedDataset_1.default(this.datasetCoreFactory.dataset(quads), this.datasetCoreFactory);
13
+ }
14
+ }
15
+ exports.default = ExtendedDatasetFactory;
16
+ //# sourceMappingURL=ExtendedDatasetFactory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExtendedDatasetFactory.js","sourceRoot":"","sources":["../src/ExtendedDatasetFactory.ts"],"names":[],"mappings":";;;;;AAMA,wEAAgD;AAKhD,MAAqB,sBAAsB;IAKzC,YACE,kBAAkE;QAElE,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;IAC/C,CAAC;IAED,OAAO,CACL,KAA4D;QAE5D,OAAO,IAAI,yBAAe,CAIxB,IAAI,CAAC,kBAAkB,CAAC,OAAO,CAAC,KAAK,CAAC,EACtC,IAAI,CAAC,kBAAkB,CACxB,CAAC;IACJ,CAAC;CACF;AAtBD,yCAsBC"}
@@ -0,0 +1,3 @@
1
+ import type { Dataset, DatasetFactory, Quad } from "@rdfjs/types";
2
+ import type { ParserOptions } from "@ldo/rdf-utils";
3
+ export default function createDatasetFromSerializedInput<ReturnDataset extends Dataset = Dataset>(datasetFactory: DatasetFactory<Quad>, data: string, options?: ParserOptions): Promise<ReturnDataset>;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const rdf_utils_1 = require("@ldo/rdf-utils");
4
+ async function createDatasetFromSerializedInput(datasetFactory, data, options) {
5
+ const quads = await (0, rdf_utils_1.serializedToQuads)(data, options);
6
+ return datasetFactory.dataset(quads);
7
+ }
8
+ exports.default = createDatasetFromSerializedInput;
9
+ //# sourceMappingURL=createDatasetFromSerializedInput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createDatasetFromSerializedInput.js","sourceRoot":"","sources":["../src/createDatasetFromSerializedInput.ts"],"names":[],"mappings":";;AAEA,8CAAmD;AAcpC,KAAK,UAAU,gCAAgC,CAG5D,cAAoC,EACpC,IAAY,EACZ,OAAuB;IAEvB,MAAM,KAAK,GAAG,MAAM,IAAA,6BAAiB,EAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACrD,OAAO,cAAc,CAAC,OAAO,CAAC,KAAK,CAA6B,CAAC;AACnE,CAAC;AATD,mDASC"}
@@ -0,0 +1,5 @@
1
+ import type { Dataset, Quad } from "@rdfjs/types";
2
+ import ExtendedDatasetFactory from "./ExtendedDatasetFactory";
3
+ import type ExtendedDataset from "./ExtendedDataset";
4
+ export declare function createExtendedDatasetFactory(): ExtendedDatasetFactory<Quad>;
5
+ export default function createExtendedDataset(quads?: Dataset<Quad> | Quad[]): ExtendedDataset<Quad>;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createExtendedDatasetFactory = void 0;
7
+ const ExtendedDatasetFactory_1 = __importDefault(require("./ExtendedDatasetFactory"));
8
+ const dataset_1 = require("@rdfjs/dataset");
9
+ function createExtendedDatasetFactory() {
10
+ const datasetFactory = {
11
+ dataset: (quads) => {
12
+ return (0, dataset_1.dataset)(Array.isArray(quads) ? quads : quads?.toArray());
13
+ },
14
+ };
15
+ return new ExtendedDatasetFactory_1.default(datasetFactory);
16
+ }
17
+ exports.createExtendedDatasetFactory = createExtendedDatasetFactory;
18
+ function createExtendedDataset(quads) {
19
+ const extendedDatasetFactory = createExtendedDatasetFactory();
20
+ return extendedDatasetFactory.dataset(quads);
21
+ }
22
+ exports.default = createExtendedDataset;
23
+ //# sourceMappingURL=createExtendedDataset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createExtendedDataset.js","sourceRoot":"","sources":["../src/createExtendedDataset.ts"],"names":[],"mappings":";;;;;;AAMA,sFAA8D;AAC9D,4CAAkE;AAOlE,SAAgB,4BAA4B;IAC1C,MAAM,cAAc,GAA6B;QAC/C,OAAO,EAAE,CAAC,KAA8B,EAAqB,EAAE;YAC7D,OAAO,IAAA,iBAAqB,EAC1B,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,OAAO,EAAE,CAChD,CAAC;QACJ,CAAC;KACF,CAAC;IACF,OAAO,IAAI,gCAAsB,CAAO,cAAc,CAAC,CAAC;AAC1D,CAAC;AATD,oEASC;AAOD,SAAwB,qBAAqB,CAC3C,KAA8B;IAE9B,MAAM,sBAAsB,GAAG,4BAA4B,EAAE,CAAC;IAC9D,OAAO,sBAAsB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC/C,CAAC;AALD,wCAKC"}
@@ -0,0 +1,4 @@
1
+ import type { Quad } from "@rdfjs/types";
2
+ import type { ParserOptions } from "@ldo/rdf-utils";
3
+ import type ExtendedDataset from "./ExtendedDataset";
4
+ export default function createExtendedDatasetFromSerializedInput(data: string, options?: ParserOptions): Promise<ExtendedDataset<Quad>>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const createDatasetFromSerializedInput_1 = __importDefault(require("./createDatasetFromSerializedInput"));
7
+ const createExtendedDataset_1 = require("./createExtendedDataset");
8
+ async function createExtendedDatasetFromSerializedInput(data, options) {
9
+ const datasetFactory = (0, createExtendedDataset_1.createExtendedDatasetFactory)();
10
+ return (0, createDatasetFromSerializedInput_1.default)(datasetFactory, data, options);
11
+ }
12
+ exports.default = createExtendedDatasetFromSerializedInput;
13
+ //# sourceMappingURL=createExtendedDatasetFromSerializedInput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createExtendedDatasetFromSerializedInput.js","sourceRoot":"","sources":["../src/createExtendedDatasetFromSerializedInput.ts"],"names":[],"mappings":";;;;;AAEA,0GAAkF;AAClF,mEAAuE;AAcxD,KAAK,UAAU,wCAAwC,CACpE,IAAY,EACZ,OAAuB;IAEvB,MAAM,cAAc,GAAG,IAAA,oDAA4B,GAAE,CAAC;IACtD,OAAO,IAAA,0CAAgC,EACrC,cAAc,EACd,IAAI,EACJ,OAAO,CACR,CAAC;AACJ,CAAC;AAVD,2DAUC"}
@@ -0,0 +1,5 @@
1
+ export { default as createDataset, createExtendedDatasetFactory as createDatasetFactory, } from "./createExtendedDataset";
2
+ export { default as createDatasetFromSerializedInput } from "./createDatasetFromSerializedInput";
3
+ export { default as serializedToDataset } from "./createExtendedDatasetFromSerializedInput";
4
+ export { default as ExtendedDataset } from "./ExtendedDataset";
5
+ export { default as ExtendedDatasetFactory } from "./ExtendedDatasetFactory";
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ExtendedDatasetFactory = exports.ExtendedDataset = exports.serializedToDataset = exports.createDatasetFromSerializedInput = exports.createDatasetFactory = exports.createDataset = void 0;
7
+ var createExtendedDataset_1 = require("./createExtendedDataset");
8
+ Object.defineProperty(exports, "createDataset", { enumerable: true, get: function () { return __importDefault(createExtendedDataset_1).default; } });
9
+ Object.defineProperty(exports, "createDatasetFactory", { enumerable: true, get: function () { return createExtendedDataset_1.createExtendedDatasetFactory; } });
10
+ var createDatasetFromSerializedInput_1 = require("./createDatasetFromSerializedInput");
11
+ Object.defineProperty(exports, "createDatasetFromSerializedInput", { enumerable: true, get: function () { return __importDefault(createDatasetFromSerializedInput_1).default; } });
12
+ var createExtendedDatasetFromSerializedInput_1 = require("./createExtendedDatasetFromSerializedInput");
13
+ Object.defineProperty(exports, "serializedToDataset", { enumerable: true, get: function () { return __importDefault(createExtendedDatasetFromSerializedInput_1).default; } });
14
+ var ExtendedDataset_1 = require("./ExtendedDataset");
15
+ Object.defineProperty(exports, "ExtendedDataset", { enumerable: true, get: function () { return __importDefault(ExtendedDataset_1).default; } });
16
+ var ExtendedDatasetFactory_1 = require("./ExtendedDatasetFactory");
17
+ Object.defineProperty(exports, "ExtendedDatasetFactory", { enumerable: true, get: function () { return __importDefault(ExtendedDatasetFactory_1).default; } });
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,iEAGiC;AAF/B,uIAAA,OAAO,OAAiB;AACxB,6HAAA,4BAA4B,OAAwB;AAEtD,uFAAiG;AAAxF,qKAAA,OAAO,OAAoC;AACpD,uGAA4F;AAAnF,gKAAA,OAAO,OAAuB;AAEvC,qDAA+D;AAAtD,mIAAA,OAAO,OAAmB;AACnC,mEAA6E;AAApE,iJAAA,OAAO,OAA0B"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@ldo/dataset",
3
+ "version": "0.0.1-1.0.0-alpha.1.0",
4
+ "description": "An RDFJS dataset implementation",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc --project tsconfig.build.json",
8
+ "watch": "tsc --watch",
9
+ "test": "jest --coverage",
10
+ "example:extendedDataset": "ts-node ./example/extendedDatasetExample.ts",
11
+ "example:loadData": "ts-node ./example/loadDataExample.ts",
12
+ "prepublishOnly": "npm run test && npm run build",
13
+ "lint": "eslint src/** --fix --no-error-on-unmatched-pattern"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/o-development/ldobjects.git"
18
+ },
19
+ "author": "Jackson Morgan",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/o-development/ldobjects/issues"
23
+ },
24
+ "homepage": "https://github.com/o-development/ldobjects/tree/main/packages/dataset#readme",
25
+ "devDependencies": {
26
+ "@rdfjs/data-model": "^1.2.0",
27
+ "@rdfjs/types": "^1.0.1",
28
+ "@types/jest": "^27.0.3",
29
+ "@types/jsonld": "^1.5.6",
30
+ "@types/rdfjs__dataset": "^1.0.4",
31
+ "@types/readable-stream": "^2.3.13",
32
+ "jest": "^27.4.5",
33
+ "ts-jest": "^27.1.2",
34
+ "ts-node": "^9.1.1"
35
+ },
36
+ "dependencies": {
37
+ "@ldo/rdf-utils": "^0.0.1-alpha.24",
38
+ "@rdfjs/dataset": "^1.1.0",
39
+ "buffer": "^6.0.3",
40
+ "readable-stream": "^4.2.0"
41
+ },
42
+ "files": [
43
+ "dist",
44
+ "src"
45
+ ],
46
+ "publishConfig": {
47
+ "access": "public"
48
+ },
49
+ "gitHead": "d7b2ebdb3c49c15615dd6c1f6bd170c867191bd3"
50
+ }
@@ -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";