@rdfx/data-factory 0.0.7

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/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @rdfx/data-factory
2
+
3
+ [RDF/JS DataFactory](https://rdf.js.org/data-model-spec/#datafactory-interface) implementation.
4
+
5
+ Adapted from [rdf-data-factory.js](https://github.com/rubensworks/rdf-data-factory.js) (MIT license).
@@ -0,0 +1,12 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * A term that represents an RDF blank node with a label.
5
+ */
6
+ export declare class BlankNode extends Term implements RDF.BlankNode {
7
+ readonly value: string;
8
+ readonly termType = "BlankNode";
9
+ constructor(value: string);
10
+ equals(other?: RDF.Term | null): boolean;
11
+ }
12
+ //# sourceMappingURL=BlankNode.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { Term } from "./Term.js";
2
+ /**
3
+ * A term that represents an RDF blank node with a label.
4
+ */
5
+ export class BlankNode extends Term {
6
+ constructor(value) {
7
+ super();
8
+ this.value = value;
9
+ this.termType = "BlankNode";
10
+ }
11
+ equals(other) {
12
+ return (!!other && other.termType === "BlankNode" && other.value === this.value);
13
+ }
14
+ }
15
+ //# sourceMappingURL=BlankNode.js.map
@@ -0,0 +1,79 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { BlankNode } from "./BlankNode.js";
3
+ import { DefaultGraph } from "./DefaultGraph.js";
4
+ import { Literal } from "./Literal.js";
5
+ import { NamedNode } from "./NamedNode.js";
6
+ import { Quad } from "./Quad.js";
7
+ import { Variable } from "./Variable.js";
8
+ /**
9
+ * A factory for instantiating RDF terms and quads.
10
+ */
11
+ export declare class DataFactory implements RDF.DataFactory<Quad, RDF.Quad> {
12
+ private static readonly XSD_STRING;
13
+ private readonly blankNodePrefix;
14
+ private blankNodeCounter;
15
+ constructor(options?: {
16
+ blankNodePrefix?: string;
17
+ });
18
+ /**
19
+ * @param value The optional blank node identifier.
20
+ * @return A new instance of BlankNode.
21
+ * If the `value` parameter is undefined a new identifier
22
+ * for the blank node is generated for each call.
23
+ * @see BlankNode
24
+ */
25
+ blankNode(value?: string): BlankNode;
26
+ /**
27
+ * @return An instance of DefaultGraph.
28
+ */
29
+ defaultGraph(): DefaultGraph;
30
+ /**
31
+ * Create a deep copy of the given quad using this data factory.
32
+ * @param original An RDF quad.
33
+ * @return A deep copy of the given quad.
34
+ */
35
+ fromQuad(original: RDF.Quad): Quad;
36
+ /**
37
+ * Create a deep copy of the given term using this data factory.
38
+ * @param original An RDF term.
39
+ * @return A deep copy of the given term.
40
+ */
41
+ fromTerm<T extends RDF.Term>(original: T): T;
42
+ /**
43
+ * @param value The literal value.
44
+ * @param languageOrDatatype The optional language, datatype, or directional language.
45
+ * If `languageOrDatatype` is a NamedNode,
46
+ * then it is used for the value of `NamedNode.datatype`.
47
+ * If `languageOrDatatype` is a NamedNode, it is used for the value
48
+ * of `NamedNode.language`.
49
+ * Otherwise, it is used as a directional language,
50
+ * from which the language is set to `languageOrDatatype.language`
51
+ * and the direction to `languageOrDatatype.direction`.
52
+ * @return A new instance of Literal.
53
+ * @see Literal
54
+ */
55
+ literal(value: string, languageOrDatatype?: string | RDF.NamedNode | RDF.DirectionalLanguage): Literal;
56
+ /**
57
+ * @param value The IRI for the named node.
58
+ * @return A new instance of NamedNode.
59
+ * @see NamedNode
60
+ */
61
+ namedNode<TIri extends string = string>(value: TIri): NamedNode<TIri>;
62
+ /**
63
+ * @param subject The quad subject term.
64
+ * @param predicate The quad predicate term.
65
+ * @param object The quad object term.
66
+ * @param graph The quad graph term.
67
+ * @return A new instance of Quad.
68
+ * @see Quad
69
+ */
70
+ quad(subject: Quad["subject"], predicate: Quad["predicate"], object: Quad["object"], graph?: Quad["graph"]): Quad;
71
+ /**
72
+ * This method is optional.
73
+ * @param value The variable name
74
+ * @return A new instance of Variable.
75
+ * @see Variable
76
+ */
77
+ variable(value: string): Variable;
78
+ }
79
+ //# sourceMappingURL=DataFactory.d.ts.map
@@ -0,0 +1,117 @@
1
+ import { BlankNode } from "./BlankNode.js";
2
+ import { DefaultGraph } from "./DefaultGraph.js";
3
+ import { Literal } from "./Literal.js";
4
+ import { NamedNode } from "./NamedNode.js";
5
+ import { Quad } from "./Quad.js";
6
+ import { Variable } from "./Variable.js";
7
+ /**
8
+ * A factory for instantiating RDF terms and quads.
9
+ */
10
+ export class DataFactory {
11
+ constructor(options) {
12
+ this.blankNodeCounter = 0;
13
+ options = options ?? {};
14
+ this.blankNodePrefix =
15
+ options.blankNodePrefix ?? `df_${dataFactoryCounter++}_`;
16
+ }
17
+ /**
18
+ * @param value The optional blank node identifier.
19
+ * @return A new instance of BlankNode.
20
+ * If the `value` parameter is undefined a new identifier
21
+ * for the blank node is generated for each call.
22
+ * @see BlankNode
23
+ */
24
+ blankNode(value) {
25
+ return new BlankNode(value ?? `${this.blankNodePrefix}${this.blankNodeCounter++}`);
26
+ }
27
+ /**
28
+ * @return An instance of DefaultGraph.
29
+ */
30
+ defaultGraph() {
31
+ return DefaultGraph.INSTANCE;
32
+ }
33
+ /**
34
+ * Create a deep copy of the given quad using this data factory.
35
+ * @param original An RDF quad.
36
+ * @return A deep copy of the given quad.
37
+ */
38
+ fromQuad(original) {
39
+ return this.fromTerm(original);
40
+ }
41
+ /**
42
+ * Create a deep copy of the given term using this data factory.
43
+ * @param original An RDF term.
44
+ * @return A deep copy of the given term.
45
+ */
46
+ fromTerm(original) {
47
+ // TODO: remove nasty any casts when this TS bug has been fixed:
48
+ // https://github.com/microsoft/TypeScript/issues/26933
49
+ switch (original.termType) {
50
+ case "NamedNode":
51
+ return this.namedNode(original.value);
52
+ case "BlankNode":
53
+ return this.blankNode(original.value);
54
+ case "Literal":
55
+ if (original.language) {
56
+ return this.literal(original.value, original.language);
57
+ }
58
+ if (!original.datatype.equals(DataFactory.XSD_STRING)) {
59
+ return (this.literal(original.value, this.fromTerm(original.datatype)));
60
+ }
61
+ return this.literal(original.value);
62
+ case "Variable":
63
+ return this.variable(original.value);
64
+ case "DefaultGraph":
65
+ return this.defaultGraph();
66
+ case "Quad":
67
+ return (this.quad(this.fromTerm(original.subject), (this.fromTerm(original.predicate)), this.fromTerm(original.object), this.fromTerm(original.graph)));
68
+ }
69
+ }
70
+ /**
71
+ * @param value The literal value.
72
+ * @param languageOrDatatype The optional language, datatype, or directional language.
73
+ * If `languageOrDatatype` is a NamedNode,
74
+ * then it is used for the value of `NamedNode.datatype`.
75
+ * If `languageOrDatatype` is a NamedNode, it is used for the value
76
+ * of `NamedNode.language`.
77
+ * Otherwise, it is used as a directional language,
78
+ * from which the language is set to `languageOrDatatype.language`
79
+ * and the direction to `languageOrDatatype.direction`.
80
+ * @return A new instance of Literal.
81
+ * @see Literal
82
+ */
83
+ literal(value, languageOrDatatype) {
84
+ return new Literal(value, languageOrDatatype);
85
+ }
86
+ /**
87
+ * @param value The IRI for the named node.
88
+ * @return A new instance of NamedNode.
89
+ * @see NamedNode
90
+ */
91
+ namedNode(value) {
92
+ return new NamedNode(value);
93
+ }
94
+ /**
95
+ * @param subject The quad subject term.
96
+ * @param predicate The quad predicate term.
97
+ * @param object The quad object term.
98
+ * @param graph The quad graph term.
99
+ * @return A new instance of Quad.
100
+ * @see Quad
101
+ */
102
+ quad(subject, predicate, object, graph) {
103
+ return new Quad(subject, predicate, object, graph ?? this.defaultGraph());
104
+ }
105
+ /**
106
+ * This method is optional.
107
+ * @param value The variable name
108
+ * @return A new instance of Variable.
109
+ * @see Variable
110
+ */
111
+ variable(value) {
112
+ return new Variable(value);
113
+ }
114
+ }
115
+ DataFactory.XSD_STRING = new NamedNode("http://www.w3.org/2001/XMLSchema#string");
116
+ let dataFactoryCounter = 0;
117
+ //# sourceMappingURL=DataFactory.js.map
@@ -0,0 +1,14 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * A singleton term instance that represents the default graph.
5
+ * It's only allowed to assign a DefaultGraph to the .graph property of a Quad.
6
+ */
7
+ export declare class DefaultGraph extends Term implements RDF.DefaultGraph {
8
+ static INSTANCE: DefaultGraph;
9
+ readonly termType = "DefaultGraph";
10
+ readonly value = "";
11
+ private constructor();
12
+ equals(other?: RDF.Term | null): boolean;
13
+ }
14
+ //# sourceMappingURL=DefaultGraph.d.ts.map
@@ -0,0 +1,17 @@
1
+ import { Term } from "./Term.js";
2
+ /**
3
+ * A singleton term instance that represents the default graph.
4
+ * It's only allowed to assign a DefaultGraph to the .graph property of a Quad.
5
+ */
6
+ export class DefaultGraph extends Term {
7
+ constructor() {
8
+ super();
9
+ this.termType = "DefaultGraph";
10
+ this.value = "";
11
+ }
12
+ equals(other) {
13
+ return !!other && other.termType === "DefaultGraph";
14
+ }
15
+ }
16
+ DefaultGraph.INSTANCE = new DefaultGraph();
17
+ //# sourceMappingURL=DefaultGraph.js.map
@@ -0,0 +1,20 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * A term that represents an RDF literal,
5
+ * containing a string with an optional language tag and optional direction
6
+ * or datatype.
7
+ */
8
+ export declare class Literal extends Term implements RDF.Literal {
9
+ readonly value: string;
10
+ private static readonly RDF_DIRECTIONAL_LANGUAGE_STRING;
11
+ private static readonly RDF_LANGUAGE_STRING;
12
+ private static readonly XSD_STRING;
13
+ readonly datatype: RDF.NamedNode;
14
+ readonly direction: "ltr" | "rtl" | "";
15
+ readonly language: string;
16
+ readonly termType = "Literal";
17
+ constructor(value: string, languageOrDatatype?: string | RDF.NamedNode | RDF.DirectionalLanguage);
18
+ equals(other?: RDF.Term | null): boolean;
19
+ }
20
+ //# sourceMappingURL=Literal.d.ts.map
@@ -0,0 +1,51 @@
1
+ import { NamedNode } from "./NamedNode.js";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * A term that represents an RDF literal,
5
+ * containing a string with an optional language tag and optional direction
6
+ * or datatype.
7
+ */
8
+ export class Literal extends Term {
9
+ constructor(value, languageOrDatatype) {
10
+ super();
11
+ this.value = value;
12
+ this.termType = "Literal";
13
+ if (typeof languageOrDatatype === "string") {
14
+ this.language = languageOrDatatype;
15
+ this.datatype = Literal.RDF_LANGUAGE_STRING;
16
+ this.direction = "";
17
+ }
18
+ else if (languageOrDatatype) {
19
+ if ("termType" in languageOrDatatype) {
20
+ this.language = "";
21
+ this.datatype = languageOrDatatype;
22
+ this.direction = "";
23
+ }
24
+ else {
25
+ this.language = languageOrDatatype.language;
26
+ this.datatype = languageOrDatatype.direction
27
+ ? Literal.RDF_DIRECTIONAL_LANGUAGE_STRING
28
+ : Literal.RDF_LANGUAGE_STRING;
29
+ this.direction = languageOrDatatype.direction ?? "";
30
+ }
31
+ }
32
+ else {
33
+ this.language = "";
34
+ this.datatype = Literal.XSD_STRING;
35
+ this.direction = "";
36
+ }
37
+ }
38
+ equals(other) {
39
+ return (!!other &&
40
+ other.termType === "Literal" &&
41
+ other.value === this.value &&
42
+ other.language === this.language &&
43
+ (other.direction === this.direction ||
44
+ (!other.direction && this.direction === "")) &&
45
+ this.datatype.equals(other.datatype));
46
+ }
47
+ }
48
+ Literal.RDF_DIRECTIONAL_LANGUAGE_STRING = new NamedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString");
49
+ Literal.RDF_LANGUAGE_STRING = new NamedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
50
+ Literal.XSD_STRING = new NamedNode("http://www.w3.org/2001/XMLSchema#string");
51
+ //# sourceMappingURL=Literal.js.map
@@ -0,0 +1,12 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * A term that contains an IRI.
5
+ */
6
+ export declare class NamedNode<TIri extends string = string> extends Term implements RDF.NamedNode<TIri> {
7
+ readonly value: TIri;
8
+ readonly termType = "NamedNode";
9
+ constructor(value: TIri);
10
+ equals(other?: RDF.Term | null): boolean;
11
+ }
12
+ //# sourceMappingURL=NamedNode.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { Term } from "./Term.js";
2
+ /**
3
+ * A term that contains an IRI.
4
+ */
5
+ export class NamedNode extends Term {
6
+ constructor(value) {
7
+ super();
8
+ this.value = value;
9
+ this.termType = "NamedNode";
10
+ }
11
+ equals(other) {
12
+ return (!!other && other.termType === "NamedNode" && other.value === this.value);
13
+ }
14
+ }
15
+ //# sourceMappingURL=NamedNode.js.map
package/dist/Quad.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * An instance of DefaultGraph represents the default graph.
5
+ * It's only allowed to assign a DefaultGraph to the .graph property of a Quad.
6
+ */
7
+ export declare class Quad extends Term implements RDF.Quad {
8
+ readonly subject: RDF.Quad["subject"];
9
+ readonly predicate: RDF.Quad["predicate"];
10
+ readonly object: RDF.Quad["object"];
11
+ readonly graph: RDF.Quad["graph"];
12
+ readonly termType = "Quad";
13
+ readonly value = "";
14
+ constructor(subject: RDF.Quad["subject"], predicate: RDF.Quad["predicate"], object: RDF.Quad["object"], graph: RDF.Quad["graph"]);
15
+ equals(other?: RDF.Term | null): boolean;
16
+ }
17
+ //# sourceMappingURL=Quad.d.ts.map
package/dist/Quad.js ADDED
@@ -0,0 +1,26 @@
1
+ import { Term } from "./Term.js";
2
+ /**
3
+ * An instance of DefaultGraph represents the default graph.
4
+ * It's only allowed to assign a DefaultGraph to the .graph property of a Quad.
5
+ */
6
+ export class Quad extends Term {
7
+ constructor(subject, predicate, object, graph) {
8
+ super();
9
+ this.subject = subject;
10
+ this.predicate = predicate;
11
+ this.object = object;
12
+ this.graph = graph;
13
+ this.termType = "Quad";
14
+ this.value = "";
15
+ }
16
+ equals(other) {
17
+ // `|| !other.termType` is for backwards-compatibility with old factories without RDF* support.
18
+ return (!!other &&
19
+ (other.termType === "Quad" || !other.termType) &&
20
+ this.subject.equals(other.subject) &&
21
+ this.predicate.equals(other.predicate) &&
22
+ this.object.equals(other.object) &&
23
+ this.graph.equals(other.graph));
24
+ }
25
+ }
26
+ //# sourceMappingURL=Quad.js.map
package/dist/Term.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export declare abstract class Term {
2
+ abstract readonly termType: "BlankNode" | "DefaultGraph" | "Literal" | "NamedNode" | "Quad" | "Variable";
3
+ abstract readonly value: string;
4
+ toJSON(): Record<string, unknown>;
5
+ toString(): string;
6
+ }
7
+ //# sourceMappingURL=Term.d.ts.map
package/dist/Term.js ADDED
@@ -0,0 +1,11 @@
1
+ import toNT from "@rdfjs/to-ntriples";
2
+ import { termToJson } from "./termToJson.js";
3
+ export class Term {
4
+ toJSON() {
5
+ return termToJson(this);
6
+ }
7
+ toString() {
8
+ return toNT(this);
9
+ }
10
+ }
11
+ //# sourceMappingURL=Term.js.map
@@ -0,0 +1,12 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ import { Term } from "./Term.js";
3
+ /**
4
+ * A term that represents a variable.
5
+ */
6
+ export declare class Variable extends Term implements RDF.Variable {
7
+ readonly value: string;
8
+ readonly termType = "Variable";
9
+ constructor(value: string);
10
+ equals(other?: RDF.Term | null): boolean;
11
+ }
12
+ //# sourceMappingURL=Variable.d.ts.map
@@ -0,0 +1,15 @@
1
+ import { Term } from "./Term.js";
2
+ /**
3
+ * A term that represents a variable.
4
+ */
5
+ export class Variable extends Term {
6
+ constructor(value) {
7
+ super();
8
+ this.value = value;
9
+ this.termType = "Variable";
10
+ }
11
+ equals(other) {
12
+ return (!!other && other.termType === "Variable" && other.value === this.value);
13
+ }
14
+ }
15
+ //# sourceMappingURL=Variable.js.map
@@ -0,0 +1,2 @@
1
+ export * from "./DataFactory.js";
2
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./DataFactory.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import type * as RDF from "@rdfjs/types";
2
+ export declare function termToJson(term: RDF.Term): Record<string, unknown>;
3
+ //# sourceMappingURL=termToJson.d.ts.map
@@ -0,0 +1,30 @@
1
+ export function termToJson(term) {
2
+ switch (term.termType) {
3
+ case "DefaultGraph":
4
+ return { termType: term.termType };
5
+ case "Literal":
6
+ return {
7
+ termType: term.termType,
8
+ datatype: term.datatype.value,
9
+ direction: term.direction,
10
+ language: term.language,
11
+ value: term.value,
12
+ };
13
+ case "NamedNode":
14
+ case "BlankNode":
15
+ case "Variable":
16
+ return {
17
+ termType: term.termType,
18
+ value: term.value,
19
+ };
20
+ case "Quad":
21
+ return {
22
+ termType: term.termType,
23
+ subject: termToJson(term.subject),
24
+ predicate: termToJson(term.predicate),
25
+ object: termToJson(term.object),
26
+ graph: termToJson(term.graph),
27
+ };
28
+ }
29
+ }
30
+ //# sourceMappingURL=termToJson.js.map
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "dependencies": {
3
+ "@rdfjs/to-ntriples": "~3.0.1",
4
+ "@rdfjs/types": "~2.0.1"
5
+ },
6
+ "devDependencies": {
7
+ "@rdfx/testing": "0.0.7",
8
+ "@types/rdfjs__to-ntriples": "~3.0.0"
9
+ },
10
+ "files": [
11
+ "LICENSE",
12
+ "README.md",
13
+ "dist/*.d.ts",
14
+ "dist/*.js"
15
+ ],
16
+ "license": "Apache-2.0",
17
+ "main": "./dist/index.js",
18
+ "name": "@rdfx/data-factory",
19
+ "packageManager": "npm@11.11.0",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/minorg/rdfx.git"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc -b",
26
+ "clean": "rimraf dist",
27
+ "depcheck": "depcheck .",
28
+ "dev": "tsc -w --preserveWatchOutput",
29
+ "dev:tests": "tsc -p __tests__ -w --preserveWatchOutput",
30
+ "test": "cd ../.. && vitest run /Users/minor/projects/rdfx/packages/data-factory/__tests__"
31
+ },
32
+ "type": "module",
33
+ "types": "./dist/index.d.ts",
34
+ "version": "0.0.7"
35
+ }