@ldo/rdf-utils 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.
@@ -0,0 +1,6 @@
1
+ import type { BaseQuad, Dataset, Quad } from "@rdfjs/types";
2
+ export interface DatasetChanges<InAndOutQuad extends BaseQuad = BaseQuad> {
3
+ added?: Dataset<InAndOutQuad, InAndOutQuad>;
4
+ removed?: Dataset<InAndOutQuad, InAndOutQuad>;
5
+ }
6
+ export declare function changesToSparqlUpdate(changes: DatasetChanges<Quad>): Promise<string>;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.changesToSparqlUpdate = void 0;
4
+ const data_model_1 = require("@rdfjs/data-model");
5
+ const datasetConverters_1 = require("./datasetConverters");
6
+ async function changesToSparqlUpdate(changes) {
7
+ let output = "";
8
+ if (changes.removed) {
9
+ const removedTriples = changes.removed.map((quad) => (0, data_model_1.quad)(quad.subject, quad.predicate, quad.object));
10
+ output += `DELETE DATA { ${await (0, datasetConverters_1.datasetToString)(removedTriples, {
11
+ format: "N-Triples",
12
+ })} }`;
13
+ }
14
+ if (changes.added && changes.removed) {
15
+ output += "; ";
16
+ }
17
+ if (changes.added) {
18
+ const addedTriples = changes.added.map((quad) => (0, data_model_1.quad)(quad.subject, quad.predicate, quad.object));
19
+ output += `INSERT DATA { ${await (0, datasetConverters_1.datasetToString)(addedTriples, {
20
+ format: "N-Triples",
21
+ })} }`;
22
+ }
23
+ return output.replaceAll("\n", " ");
24
+ }
25
+ exports.changesToSparqlUpdate = changesToSparqlUpdate;
26
+ //# sourceMappingURL=datasetChanges.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datasetChanges.js","sourceRoot":"","sources":["../src/datasetChanges.ts"],"names":[],"mappings":";;;AACA,kDAAuD;AACvD,2DAAsD;AAe/C,KAAK,UAAU,qBAAqB,CAAC,OAA6B;IACvE,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,IAAI,OAAO,CAAC,OAAO,EAAE;QACnB,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAClD,IAAA,iBAAU,EAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,CACtD,CAAC;QACF,MAAM,IAAI,iBAAiB,MAAM,IAAA,mCAAe,EAAC,cAAc,EAAE;YAC/D,MAAM,EAAE,WAAW;SACpB,CAAC,IAAI,CAAC;KACR;IACD,IAAI,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,OAAO,EAAE;QACpC,MAAM,IAAI,IAAI,CAAC;KAChB;IACD,IAAI,OAAO,CAAC,KAAK,EAAE;QACjB,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAC9C,IAAA,iBAAU,EAAC,IAAI,CAAC,OAAO,EAAE,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,CACtD,CAAC;QACF,MAAM,IAAI,iBAAiB,MAAM,IAAA,mCAAe,EAAC,YAAY,EAAE;YAC7D,MAAM,EAAE,WAAW;SACpB,CAAC,IAAI,CAAC;KACR;IACD,OAAO,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AACtC,CAAC;AAtBD,sDAsBC"}
@@ -0,0 +1,6 @@
1
+ import type { DatasetCore, Quad } from "@rdfjs/types";
2
+ import type { ContextDefinition, JsonLdDocument } from "jsonld";
3
+ import type { WriterOptions as WriterOptionsImport } from "n3";
4
+ export type WriterOptions = WriterOptionsImport;
5
+ export declare function datasetToString(dataset: DatasetCore<Quad>, options: WriterOptions): string;
6
+ export declare function datasetToJsonLd(_dataset: DatasetCore, _context: ContextDefinition): Promise<JsonLdDocument>;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.datasetToJsonLd = exports.datasetToString = void 0;
4
+ const n3_1 = require("n3");
5
+ function datasetToString(dataset, options) {
6
+ const writer = new n3_1.Writer(options);
7
+ const quadArr = [];
8
+ for (const quad of dataset) {
9
+ quadArr.push(quad);
10
+ }
11
+ return writer.quadsToString(quadArr);
12
+ }
13
+ exports.datasetToString = datasetToString;
14
+ async function datasetToJsonLd(_dataset, _context) {
15
+ throw new Error("JSONLD serialization is not omplemented");
16
+ }
17
+ exports.datasetToJsonLd = datasetToJsonLd;
18
+ //# sourceMappingURL=datasetConverters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datasetConverters.js","sourceRoot":"","sources":["../src/datasetConverters.ts"],"names":[],"mappings":";;;AAGA,2BAA4B;AAM5B,SAAgB,eAAe,CAC7B,OAA0B,EAC1B,OAAsB;IAEtB,MAAM,MAAM,GAAG,IAAI,WAAM,CAAC,OAAO,CAAC,CAAC;IACnC,MAAM,OAAO,GAAW,EAAE,CAAC;IAC3B,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE;QAC1B,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;KACpB;IACD,OAAO,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;AACvC,CAAC;AAVD,0CAUC;AAEM,KAAK,UAAU,eAAe,CACnC,QAAqB,EACrB,QAA2B;IAE3B,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;AAC7D,CAAC;AALD,0CAKC"}
@@ -0,0 +1,4 @@
1
+ export * from "./nodeSerialization";
2
+ export * from "./datasetChanges";
3
+ export * from "./datasetConverters";
4
+ export * from "./serializedToQuads";
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./nodeSerialization"), exports);
18
+ __exportStar(require("./datasetChanges"), exports);
19
+ __exportStar(require("./datasetConverters"), exports);
20
+ __exportStar(require("./serializedToQuads"), exports);
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,mDAAiC;AACjC,sDAAoC;AACpC,sDAAoC"}
@@ -0,0 +1,38 @@
1
+ import type { Quad, Term, BlankNode, DefaultGraph, Literal, NamedNode } from "@rdfjs/types";
2
+ export type NodeTermTypes<Node extends Term> = Set<Node["termType"]>;
3
+ export type SubjectNode = NamedNode | BlankNode;
4
+ export declare const SubjectTermTypes: NodeTermTypes<SubjectNode>;
5
+ export type PredicateNode = NamedNode;
6
+ export declare const PredicateTermTypes: NodeTermTypes<PredicateNode>;
7
+ export type ObjectNode = NamedNode | BlankNode | Literal;
8
+ export declare const ObjectTermTypes: NodeTermTypes<ObjectNode>;
9
+ export type GraphNode = NamedNode | BlankNode | DefaultGraph;
10
+ export declare const GraphTermTypes: NodeTermTypes<GraphNode>;
11
+ export type AnyNode = SubjectNode | PredicateNode | ObjectNode | GraphNode;
12
+ export declare const AnyTermTypes: NodeTermTypes<AnyNode>;
13
+ export interface SimpleQuad extends Quad {
14
+ subject: SubjectNode;
15
+ predicate: PredicateNode;
16
+ object: ObjectNode;
17
+ graph: GraphNode;
18
+ }
19
+ export type QuadMatch = [
20
+ SubjectNode | undefined | null,
21
+ PredicateNode | undefined | null,
22
+ ObjectNode | undefined | null,
23
+ GraphNode | undefined | null
24
+ ];
25
+ export declare function subjectNodeToString(node: SubjectNode): string;
26
+ export declare function predicateNodeToString(node: PredicateNode): string;
27
+ export declare function objectNodeToString(node: ObjectNode): string;
28
+ export declare function grpahNodeToString(node: GraphNode): string;
29
+ export declare function nodeToString(node: AnyNode): string;
30
+ export declare function quadToString(quad: Quad): string;
31
+ export declare function quadMatchToString(quadMatch: QuadMatch): string;
32
+ export declare function stringToSubjectNode(input: string): SubjectNode;
33
+ export declare function stringToPredicateNode(input: string): PredicateNode;
34
+ export declare function stringToObjectNode(input: string): ObjectNode;
35
+ export declare function stringToGraphNode(input: string): GraphNode;
36
+ export declare function stringToNode(input: string, expectTermType?: NodeTermTypes<Term>): AnyNode;
37
+ export declare function stringToQuad(input: string): Quad;
38
+ export declare function stringToQuadMatch(input: string): QuadMatch;
@@ -0,0 +1,115 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stringToQuadMatch = exports.stringToQuad = exports.stringToNode = exports.stringToGraphNode = exports.stringToObjectNode = exports.stringToPredicateNode = exports.stringToSubjectNode = exports.quadMatchToString = exports.quadToString = exports.nodeToString = exports.grpahNodeToString = exports.objectNodeToString = exports.predicateNodeToString = exports.subjectNodeToString = exports.AnyTermTypes = exports.GraphTermTypes = exports.ObjectTermTypes = exports.PredicateTermTypes = exports.SubjectTermTypes = void 0;
4
+ const rdf_string_1 = require("rdf-string");
5
+ exports.SubjectTermTypes = new Set([
6
+ "NamedNode",
7
+ "BlankNode",
8
+ ]);
9
+ exports.PredicateTermTypes = new Set([
10
+ "NamedNode",
11
+ ]);
12
+ exports.ObjectTermTypes = new Set([
13
+ "NamedNode",
14
+ "BlankNode",
15
+ "Literal",
16
+ ]);
17
+ exports.GraphTermTypes = new Set([
18
+ "NamedNode",
19
+ "BlankNode",
20
+ "DefaultGraph",
21
+ ]);
22
+ exports.AnyTermTypes = new Set([
23
+ "NamedNode",
24
+ "BlankNode",
25
+ "DefaultGraph",
26
+ "Literal",
27
+ ]);
28
+ function subjectNodeToString(node) {
29
+ return nodeToString(node);
30
+ }
31
+ exports.subjectNodeToString = subjectNodeToString;
32
+ function predicateNodeToString(node) {
33
+ return nodeToString(node);
34
+ }
35
+ exports.predicateNodeToString = predicateNodeToString;
36
+ function objectNodeToString(node) {
37
+ return nodeToString(node);
38
+ }
39
+ exports.objectNodeToString = objectNodeToString;
40
+ function grpahNodeToString(node) {
41
+ return nodeToString(node);
42
+ }
43
+ exports.grpahNodeToString = grpahNodeToString;
44
+ function nodeToString(node) {
45
+ return (0, rdf_string_1.termToString)(node);
46
+ }
47
+ exports.nodeToString = nodeToString;
48
+ function quadToString(quad) {
49
+ return JSON.stringify((0, rdf_string_1.quadToStringQuad)(quad));
50
+ }
51
+ exports.quadToString = quadToString;
52
+ function quadMatchToString(quadMatch) {
53
+ return JSON.stringify({
54
+ subject: quadMatch[0] ? subjectNodeToString(quadMatch[0]) : undefined,
55
+ predicate: quadMatch[1] ? predicateNodeToString(quadMatch[1]) : undefined,
56
+ object: quadMatch[2] ? objectNodeToString(quadMatch[2]) : undefined,
57
+ graph: quadMatch[3] ? grpahNodeToString(quadMatch[3]) : undefined,
58
+ });
59
+ }
60
+ exports.quadMatchToString = quadMatchToString;
61
+ function stringToSubjectNode(input) {
62
+ return stringToNode(input, exports.SubjectTermTypes);
63
+ }
64
+ exports.stringToSubjectNode = stringToSubjectNode;
65
+ function stringToPredicateNode(input) {
66
+ return stringToNode(input, exports.PredicateTermTypes);
67
+ }
68
+ exports.stringToPredicateNode = stringToPredicateNode;
69
+ function stringToObjectNode(input) {
70
+ return stringToNode(input, exports.ObjectTermTypes);
71
+ }
72
+ exports.stringToObjectNode = stringToObjectNode;
73
+ function stringToGraphNode(input) {
74
+ return stringToNode(input, exports.GraphTermTypes);
75
+ }
76
+ exports.stringToGraphNode = stringToGraphNode;
77
+ function stringToNode(input, expectTermType) {
78
+ const node = (0, rdf_string_1.stringToTerm)(input);
79
+ if (expectTermType && !expectTermType.has(node.termType)) {
80
+ throw new Error(`Expected term to be one of term type: [${Array.from(expectTermType).reduce((agg, termType) => `${agg}${termType}, `, "")}], but got ${node.termType}.`);
81
+ }
82
+ return node;
83
+ }
84
+ exports.stringToNode = stringToNode;
85
+ function stringToQuad(input) {
86
+ try {
87
+ return (0, rdf_string_1.stringQuadToQuad)(JSON.parse(input));
88
+ }
89
+ catch (err) {
90
+ throw new Error("Invalid Quad String");
91
+ }
92
+ }
93
+ exports.stringToQuad = stringToQuad;
94
+ function stringToQuadMatch(input) {
95
+ try {
96
+ const jsonRep = JSON.parse(input);
97
+ return [
98
+ jsonRep.subject != undefined
99
+ ? stringToSubjectNode(jsonRep.subject)
100
+ : undefined,
101
+ jsonRep.predicate != undefined
102
+ ? stringToPredicateNode(jsonRep.predicate)
103
+ : undefined,
104
+ jsonRep.object != undefined
105
+ ? stringToObjectNode(jsonRep.object)
106
+ : undefined,
107
+ jsonRep.graph != undefined ? stringToGraphNode(jsonRep.graph) : undefined,
108
+ ];
109
+ }
110
+ catch (err) {
111
+ throw new Error("Invalid Quad Match String");
112
+ }
113
+ }
114
+ exports.stringToQuadMatch = stringToQuadMatch;
115
+ //# sourceMappingURL=nodeSerialization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nodeSerialization.js","sourceRoot":"","sources":["../src/nodeSerialization.ts"],"names":[],"mappings":";;;AAQA,2CAKoB;AAcP,QAAA,gBAAgB,GAA+B,IAAI,GAAG,CAAC;IAClE,WAAW;IACX,WAAW;CACZ,CAAC,CAAC;AAEU,QAAA,kBAAkB,GAAiC,IAAI,GAAG,CAAC;IACtE,WAAW;CACZ,CAAC,CAAC;AAEU,QAAA,eAAe,GAA8B,IAAI,GAAG,CAAC;IAChE,WAAW;IACX,WAAW;IACX,SAAS;CACV,CAAC,CAAC;AAEU,QAAA,cAAc,GAA6B,IAAI,GAAG,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IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;KAC5C;IAAC,OAAO,GAAG,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;KACxC;AACH,CAAC;AAND,oCAMC;AAOD,SAAgB,iBAAiB,CAAC,KAAa;IAC7C,IAAI;QACF,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAClC,OAAO;YACL,OAAO,CAAC,OAAO,IAAI,SAAS;gBAC1B,CAAC,CAAC,mBAAmB,CAAC,OAAO,CAAC,OAAO,CAAC;gBACtC,CAAC,CAAC,SAAS;YACb,OAAO,CAAC,SAAS,IAAI,SAAS;gBAC5B,CAAC,CAAC,qBAAqB,CAAC,OAAO,CAAC,SAAS,CAAC;gBAC1C,CAAC,CAAC,SAAS;YACb,OAAO,CAAC,MAAM,IAAI,SAAS;gBACzB,CAAC,CAAC,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC;gBACpC,CAAC,CAAC,SAAS;YACb,OAAO,CAAC,KAAK,IAAI,SAAS,CAAC,CAAC,CAAC,iBAAiB,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS;SAC1E,CAAC;KACH;IAAC,OAAO,GAAG,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;KAC9C;AACH,CAAC;AAlBD,8CAkBC"}
@@ -0,0 +1,4 @@
1
+ import type { Quad } from "@rdfjs/types";
2
+ import type { ParserOptions as ParserOptionsImport } from "n3";
3
+ export type ParserOptions = ParserOptionsImport;
4
+ export declare function serializedToQuads(data: string, options?: ParserOptions): Promise<Quad[]>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.serializedToQuads = void 0;
4
+ const n3_1 = require("n3");
5
+ async function serializedToQuads(data, options) {
6
+ if (options && options.format === "application/json-ld") {
7
+ throw new Error("Not Implemented");
8
+ }
9
+ const parser = new n3_1.Parser(options);
10
+ return parser.parse(data);
11
+ }
12
+ exports.serializedToQuads = serializedToQuads;
13
+ //# sourceMappingURL=serializedToQuads.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serializedToQuads.js","sourceRoot":"","sources":["../src/serializedToQuads.ts"],"names":[],"mappings":";;;AAEA,2BAA4B;AAIrB,KAAK,UAAU,iBAAiB,CACrC,IAAY,EACZ,OAAuB;IAGvB,IAAI,OAAO,IAAI,OAAO,CAAC,MAAM,KAAK,qBAAqB,EAAE;QACvD,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;KA0BpC;IAED,MAAM,MAAM,GAAG,IAAI,WAAM,CAAC,OAAwB,CAAC,CAAC;IACpD,OAAO,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;AAC5B,CAAC;AApCD,8CAoCC"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@ldo/rdf-utils",
3
+ "version": "0.0.1-alpha.14",
4
+ "description": "Some RDF Utilities to support LDO librariers",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc --project tsconfig.build.json",
8
+ "watch": "tsc --watch",
9
+ "test": "jest --coverage",
10
+ "test:watch": "jest --watch",
11
+ "prepublishOnly": "npm run test && npm run build",
12
+ "lint": "eslint src/** --fix --no-error-on-unmatched-pattern"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/o-development/ldobjects.git"
17
+ },
18
+ "author": "Jackson Morgan",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/o-development/ldobjects/issues"
22
+ },
23
+ "homepage": "https://github.com/o-development/ldobjects/tree/main/packages/rdf-utils#readme",
24
+ "devDependencies": {
25
+ "@rdfjs/types": "^1.0.1",
26
+ "@types/jsonld": "^1.5.9",
27
+ "ts-jest": "^29.1.1"
28
+ },
29
+ "dependencies": {
30
+ "@rdfjs/data-model": "^1.2.0",
31
+ "n3": "^1.17.1",
32
+ "rdf-string": "^1.6.3"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "src"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "gitHead": "5acfea4c43c07c02df9ed4e9166e61a540e26db8"
42
+ }
@@ -0,0 +1,40 @@
1
+ import type { BaseQuad, Dataset, Quad } from "@rdfjs/types";
2
+ import { quad as createQuad } from "@rdfjs/data-model";
3
+ import { datasetToString } from "./datasetConverters";
4
+
5
+ /**
6
+ * An interface representing the changes made
7
+ */
8
+ export interface DatasetChanges<InAndOutQuad extends BaseQuad = BaseQuad> {
9
+ added?: Dataset<InAndOutQuad, InAndOutQuad>;
10
+ removed?: Dataset<InAndOutQuad, InAndOutQuad>;
11
+ }
12
+
13
+ /**
14
+ * Takes Dataset Changes and converts them to SPARQL UPDATE
15
+ * @param changes: Dataset Changes
16
+ * @returns String SPARQL Update
17
+ */
18
+ export async function changesToSparqlUpdate(changes: DatasetChanges<Quad>) {
19
+ let output = "";
20
+ if (changes.removed) {
21
+ const removedTriples = changes.removed.map((quad) =>
22
+ createQuad(quad.subject, quad.predicate, quad.object),
23
+ );
24
+ output += `DELETE DATA { ${await datasetToString(removedTriples, {
25
+ format: "N-Triples",
26
+ })} }`;
27
+ }
28
+ if (changes.added && changes.removed) {
29
+ output += "; ";
30
+ }
31
+ if (changes.added) {
32
+ const addedTriples = changes.added.map((quad) =>
33
+ createQuad(quad.subject, quad.predicate, quad.object),
34
+ );
35
+ output += `INSERT DATA { ${await datasetToString(addedTriples, {
36
+ format: "N-Triples",
37
+ })} }`;
38
+ }
39
+ return output.replaceAll("\n", " ");
40
+ }
@@ -0,0 +1,55 @@
1
+ import type { DatasetCore, Quad } from "@rdfjs/types";
2
+ import type { ContextDefinition, JsonLdDocument } from "jsonld";
3
+ import type { WriterOptions as WriterOptionsImport } from "n3";
4
+ import { Writer } from "n3";
5
+ // import SerializerJsonld from "@rdfjs/serializer-jsonld";
6
+ // import { Readable } from "readable-stream";
7
+
8
+ export type WriterOptions = WriterOptionsImport;
9
+
10
+ export function datasetToString(
11
+ dataset: DatasetCore<Quad>,
12
+ options: WriterOptions,
13
+ ): string {
14
+ const writer = new Writer(options);
15
+ const quadArr: Quad[] = [];
16
+ for (const quad of dataset) {
17
+ quadArr.push(quad);
18
+ }
19
+ return writer.quadsToString(quadArr);
20
+ }
21
+
22
+ export async function datasetToJsonLd(
23
+ _dataset: DatasetCore,
24
+ _context: ContextDefinition,
25
+ ): Promise<JsonLdDocument> {
26
+ throw new Error("JSONLD serialization is not omplemented");
27
+ }
28
+
29
+ // export async function datasetToJsonLd(
30
+ // dataset: Dataset,
31
+ // context: ContextDefinition
32
+ // ): Promise<JsonLdDocument> {
33
+ // return new Promise((resolve, reject) => {
34
+ // const serializerJsonld = new SerializerJsonld();
35
+ // const input = new Readable({
36
+ // objectMode: true,
37
+ // read: () => {
38
+ // dataset.forEach((quad) => {
39
+ // input.push(quad);
40
+ // });
41
+ // input.push(null);
42
+ // },
43
+ // });
44
+ // const output = serializerJsonld.import(input);
45
+
46
+ // output.on("data", (jsonld) => {
47
+ // resolve(jsonld);
48
+ // });
49
+ // /* istanbul ignore next */
50
+ // output.on("error", (err) => {
51
+ // /* istanbul ignore next */
52
+ // reject(err);
53
+ // });
54
+ // });
55
+ // }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./nodeSerialization";
2
+ export * from "./datasetChanges";
3
+ export * from "./datasetConverters";
4
+ export * from "./serializedToQuads";
@@ -0,0 +1,260 @@
1
+ import type {
2
+ Quad,
3
+ Term,
4
+ BlankNode,
5
+ DefaultGraph,
6
+ Literal,
7
+ NamedNode,
8
+ } from "@rdfjs/types";
9
+ import {
10
+ termToString,
11
+ quadToStringQuad,
12
+ stringToTerm,
13
+ stringQuadToQuad,
14
+ } from "rdf-string";
15
+
16
+ /**
17
+ * =============================================================================
18
+ * Types
19
+ * =============================================================================
20
+ */
21
+
22
+ /**
23
+ * Generic type to extract a set of termsTypes for a Term
24
+ */
25
+ export type NodeTermTypes<Node extends Term> = Set<Node["termType"]>;
26
+
27
+ export type SubjectNode = NamedNode | BlankNode;
28
+ export const SubjectTermTypes: NodeTermTypes<SubjectNode> = new Set([
29
+ "NamedNode",
30
+ "BlankNode",
31
+ ]);
32
+ export type PredicateNode = NamedNode;
33
+ export const PredicateTermTypes: NodeTermTypes<PredicateNode> = new Set([
34
+ "NamedNode",
35
+ ]);
36
+ export type ObjectNode = NamedNode | BlankNode | Literal;
37
+ export const ObjectTermTypes: NodeTermTypes<ObjectNode> = new Set([
38
+ "NamedNode",
39
+ "BlankNode",
40
+ "Literal",
41
+ ]);
42
+ export type GraphNode = NamedNode | BlankNode | DefaultGraph;
43
+ export const GraphTermTypes: NodeTermTypes<GraphNode> = new Set([
44
+ "NamedNode",
45
+ "BlankNode",
46
+ "DefaultGraph",
47
+ ]);
48
+ export type AnyNode = SubjectNode | PredicateNode | ObjectNode | GraphNode;
49
+ export const AnyTermTypes: NodeTermTypes<AnyNode> = new Set([
50
+ "NamedNode",
51
+ "BlankNode",
52
+ "DefaultGraph",
53
+ "Literal",
54
+ ]);
55
+
56
+ export interface SimpleQuad extends Quad {
57
+ /**
58
+ * The subject.
59
+ * @see GraphNode
60
+ */
61
+ subject: SubjectNode;
62
+ /**
63
+ * The predicate.
64
+ * @see GraphNode
65
+ */
66
+ predicate: PredicateNode;
67
+ /**
68
+ * The object.
69
+ * @see GraphNode
70
+ */
71
+ object: ObjectNode;
72
+ /**
73
+ * The named graph.
74
+ * @see GraphNode
75
+ */
76
+ graph: GraphNode;
77
+ }
78
+
79
+ export type QuadMatch = [
80
+ SubjectNode | undefined | null,
81
+ PredicateNode | undefined | null,
82
+ ObjectNode | undefined | null,
83
+ GraphNode | undefined | null,
84
+ ];
85
+
86
+ /**
87
+ * =============================================================================
88
+ * To String
89
+ * =============================================================================
90
+ */
91
+
92
+ /**
93
+ * Converts a Subject Node to a String
94
+ * @param node Input Node
95
+ * @returns String Representation
96
+ */
97
+ export function subjectNodeToString(node: SubjectNode) {
98
+ return nodeToString(node);
99
+ }
100
+
101
+ /**
102
+ * Converts a Predicate Node to a String
103
+ * @param node Input Node
104
+ * @returns String Representation
105
+ */
106
+ export function predicateNodeToString(node: PredicateNode) {
107
+ return nodeToString(node);
108
+ }
109
+
110
+ /**
111
+ * Converts a Object Node to a String
112
+ * @param node Input Node
113
+ * @returns String Representation
114
+ */
115
+ export function objectNodeToString(node: ObjectNode) {
116
+ return nodeToString(node);
117
+ }
118
+
119
+ /**
120
+ * Converts a Graph Node to a String
121
+ * @param node Input Node
122
+ * @returns String Representation
123
+ */
124
+ export function grpahNodeToString(node: GraphNode) {
125
+ return nodeToString(node);
126
+ }
127
+
128
+ /**
129
+ * Converts a Any Node to a String
130
+ * @param node Input Node
131
+ * @returns String Representation
132
+ */
133
+ export function nodeToString(node: AnyNode): string {
134
+ return termToString(node);
135
+ }
136
+
137
+ /**
138
+ * Converts a Quad to a String
139
+ * @param node Input Quad
140
+ * @returns String Representation
141
+ */
142
+ export function quadToString(quad: Quad): string {
143
+ return JSON.stringify(quadToStringQuad(quad));
144
+ }
145
+
146
+ /**
147
+ * Converts a Quad Match to a String
148
+ * @param node Input Quad
149
+ * @returns String Representation
150
+ */
151
+ export function quadMatchToString(quadMatch: QuadMatch): string {
152
+ return JSON.stringify({
153
+ subject: quadMatch[0] ? subjectNodeToString(quadMatch[0]) : undefined,
154
+ predicate: quadMatch[1] ? predicateNodeToString(quadMatch[1]) : undefined,
155
+ object: quadMatch[2] ? objectNodeToString(quadMatch[2]) : undefined,
156
+ graph: quadMatch[3] ? grpahNodeToString(quadMatch[3]) : undefined,
157
+ });
158
+ }
159
+
160
+ /**
161
+ * =============================================================================
162
+ * From String
163
+ * =============================================================================
164
+ */
165
+
166
+ /**
167
+ * Converts a String to a Subject Node
168
+ * @param input String Inupt
169
+ * @returns Node
170
+ */
171
+ export function stringToSubjectNode(input: string): SubjectNode {
172
+ return stringToNode(input, SubjectTermTypes) as SubjectNode;
173
+ }
174
+
175
+ /**
176
+ * Converts a String to a Predicate Node
177
+ * @param input String Inupt
178
+ * @returns Node
179
+ */
180
+ export function stringToPredicateNode(input: string): PredicateNode {
181
+ return stringToNode(input, PredicateTermTypes) as PredicateNode;
182
+ }
183
+
184
+ /**
185
+ * Converts a String to an Object Node
186
+ * @param input String Inupt
187
+ * @returns Node
188
+ */
189
+ export function stringToObjectNode(input: string): ObjectNode {
190
+ return stringToNode(input, ObjectTermTypes) as ObjectNode;
191
+ }
192
+
193
+ /**
194
+ * Converts a String to a Graph Node
195
+ * @param input String Inupt
196
+ * @returns Node
197
+ */
198
+ export function stringToGraphNode(input: string): GraphNode {
199
+ return stringToNode(input, GraphTermTypes) as GraphNode;
200
+ }
201
+
202
+ /**
203
+ * Converts a String to a Node
204
+ * @param input String Inupt
205
+ * @returns Node
206
+ */
207
+ export function stringToNode(
208
+ input: string,
209
+ expectTermType?: NodeTermTypes<Term>,
210
+ ): AnyNode {
211
+ const node = stringToTerm(input);
212
+ if (expectTermType && !expectTermType.has(node.termType)) {
213
+ throw new Error(
214
+ `Expected term to be one of term type: [${Array.from(
215
+ expectTermType,
216
+ ).reduce((agg, termType) => `${agg}${termType}, `, "")}], but got ${
217
+ node.termType
218
+ }.`,
219
+ );
220
+ }
221
+ return node as AnyNode;
222
+ }
223
+
224
+ /**
225
+ * Converts a String to a Quad
226
+ * @param input String Inupt
227
+ * @returns Quad
228
+ */
229
+ export function stringToQuad(input: string) {
230
+ try {
231
+ return stringQuadToQuad(JSON.parse(input));
232
+ } catch (err) {
233
+ throw new Error("Invalid Quad String");
234
+ }
235
+ }
236
+
237
+ /**
238
+ * Converts a String to a Quad Match
239
+ * @param input String Inupt
240
+ * @returns QuadMatch
241
+ */
242
+ export function stringToQuadMatch(input: string): QuadMatch {
243
+ try {
244
+ const jsonRep = JSON.parse(input);
245
+ return [
246
+ jsonRep.subject != undefined
247
+ ? stringToSubjectNode(jsonRep.subject)
248
+ : undefined,
249
+ jsonRep.predicate != undefined
250
+ ? stringToPredicateNode(jsonRep.predicate)
251
+ : undefined,
252
+ jsonRep.object != undefined
253
+ ? stringToObjectNode(jsonRep.object)
254
+ : undefined,
255
+ jsonRep.graph != undefined ? stringToGraphNode(jsonRep.graph) : undefined,
256
+ ];
257
+ } catch (err) {
258
+ throw new Error("Invalid Quad Match String");
259
+ }
260
+ }
@@ -0,0 +1,43 @@
1
+ import type { Quad } from "@rdfjs/types";
2
+ import type { ParserOptions as ParserOptionsImport } from "n3";
3
+ import { Parser } from "n3";
4
+
5
+ export type ParserOptions = ParserOptionsImport;
6
+
7
+ export async function serializedToQuads(
8
+ data: string,
9
+ options?: ParserOptions,
10
+ ): Promise<Quad[]> {
11
+ // JSON-LD Parsing
12
+ if (options && options.format === "application/json-ld") {
13
+ throw new Error("Not Implemented");
14
+ // return new Promise((resolve, reject) => {
15
+ // JSON.parse(data);
16
+ // const parserJsonld = new ParserJsonld();
17
+
18
+ // const input = new Readable({
19
+ // read: () => {
20
+ // input.push(data);
21
+ // input.push(null);
22
+ // },
23
+ // });
24
+
25
+ // const output = parserJsonld.import(input);
26
+ // const quads: Quad[] = [];
27
+ // output.on("data", (quad) => {
28
+ // quads.push(quad);
29
+ // });
30
+ // output.on("end", () => {
31
+ // resolve((datasetFactory.dataset(quads) as unknown) as ReturnDataset);
32
+ // });
33
+ // /* istanbul ignore next */
34
+ // output.on("error", (err) => {
35
+ // /* istanbul ignore next */
36
+ // reject(err);
37
+ // });
38
+ // });
39
+ }
40
+ // N3 Parsing
41
+ const parser = new Parser(options as ParserOptions);
42
+ return parser.parse(data);
43
+ }