Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | 8799x 8799x 37x 1948x 1948x 6x 45x 45x 45x 10x 10x 35x 12x 12x 23x 23x 18x 3x 15x 15x 8x 8x 5x 29x 29x 8x 146730x 146730x 146730x 146730x 146730x 146730x 12x 3x 9x 9x 15x 69x 8766x 1948x 45x 8x 10x 146730x 31x 16x 2x 7x 7x 1x 6x 1x 3x 1x 1x 1x 4x 55x 15x | import * as rdfjs from "@rdfjs/types";
/**
* An RDF/JS compliant NamedNode implementation.
*/
export class NamedNode implements rdfjs.NamedNode {
readonly termType = "NamedNode" as const;
readonly value: string;
constructor(value: string) {
this.value = value;
}
equals(other: rdfjs.Term | null | undefined): boolean {
return other?.termType === this.termType && other.value === this.value;
}
}
/**
* An RDF/JS compliant BlankNode implementation.
*/
export class BlankNode implements rdfjs.BlankNode {
readonly termType = "BlankNode" as const;
readonly value: string;
constructor(value: string) {
this.value = value;
}
equals(other: rdfjs.Term | null | undefined): boolean {
return other?.termType === this.termType && other.value === this.value;
}
}
/**
* An RDF/JS compliant Literal implementation.
*/
export class Literal implements rdfjs.Literal {
readonly termType = "Literal" as const;
readonly value: string;
readonly language: string;
readonly datatype: rdfjs.NamedNode;
constructor(value: string, languageOrDatatype?: string | rdfjs.NamedNode) {
this.value = value;
if (typeof languageOrDatatype === "string") {
this.language = languageOrDatatype.toLowerCase();
this.datatype = new NamedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
} else if (languageOrDatatype) {
this.language = "";
this.datatype = languageOrDatatype;
} else {
this.language = "";
this.datatype = new NamedNode("http://www.w3.org/2001/XMLSchema#string");
}
}
equals(other: rdfjs.Term | null | undefined): boolean {
if (other?.termType !== this.termType) {
return false;
}
const otherLiteral = other as rdfjs.Literal;
return (
this.value === otherLiteral.value &&
this.language === otherLiteral.language &&
this.datatype.equals(otherLiteral.datatype)
);
}
}
/**
* An RDF/JS compliant Variable implementation.
*/
export class Variable implements rdfjs.Variable {
readonly termType = "Variable" as const;
readonly value: string;
constructor(value: string) {
this.value = value;
}
equals(other: rdfjs.Term | null | undefined): boolean {
return other?.termType === this.termType && other.value === this.value;
}
}
/**
* An RDF/JS compliant DefaultGraph implementation (singleton).
*/
export class DefaultGraph implements rdfjs.DefaultGraph {
readonly termType = "DefaultGraph" as const;
readonly value = "";
equals(other: rdfjs.Term | null | undefined): boolean {
return other?.termType === this.termType;
}
}
/**
* An RDF/JS compliant Quad implementation.
*/
export class Quad implements rdfjs.Quad {
readonly termType = "Quad" as const;
readonly value = "";
readonly subject: rdfjs.Quad_Subject;
readonly predicate: rdfjs.Quad_Predicate;
readonly object: rdfjs.Quad_Object;
readonly graph: rdfjs.Quad_Graph;
constructor(
subject: rdfjs.Quad_Subject,
predicate: rdfjs.Quad_Predicate,
object: rdfjs.Quad_Object,
graph?: rdfjs.Quad_Graph
) {
this.subject = subject;
this.predicate = predicate;
this.object = object;
this.graph = graph ?? new DefaultGraph();
}
equals(other: rdfjs.Term | null | undefined): boolean {
if (other?.termType !== this.termType) {
return false;
}
const otherQuad = other as rdfjs.Quad;
return (
this.subject.equals(otherQuad.subject) &&
this.predicate.equals(otherQuad.predicate) &&
this.object.equals(otherQuad.object) &&
this.graph.equals(otherQuad.graph)
);
}
}
// Singleton instance of DefaultGraph
const defaultGraphInstance = new DefaultGraph();
/**
* An RDF/JS compliant DataFactory implementation.
*
* This factory provides methods to create RDF terms (named nodes, blank nodes, literals, variables)
* and quads according to the RDF/JS specification.
*
* @see https://rdf.js.org/data-model-spec/
*/
export class DataFactory implements rdfjs.DataFactory<rdfjs.Quad, rdfjs.Quad> {
blankNodeCounter: number = 0;
/**
* Creates a new named node with the given IRI.
* @param value The IRI of the named node.
* @returns A new NamedNode instance.
*/
namedNode<Iri extends string = string>(value: Iri): rdfjs.NamedNode<Iri> {
return new NamedNode(value) as rdfjs.NamedNode<Iri>;
}
/**
* Creates a new blank node with the given identifier.
* If no identifier is provided, a unique one will be generated.
* @param value Optional blank node identifier.
* @returns A new BlankNode instance.
*/
blankNode(value?: string): rdfjs.BlankNode {
return new BlankNode(value ?? `b${++this.blankNodeCounter}`);
}
/**
* Creates a new literal with the given value and optional language tag or datatype.
* @param value The lexical value of the literal.
* @param languageOrDatatype Either a language tag (string) or a datatype (NamedNode).
* @returns A new Literal instance.
*/
literal(value: string, languageOrDatatype?: string | rdfjs.NamedNode): rdfjs.Literal {
return new Literal(value, languageOrDatatype);
}
/**
* Creates a new variable with the given name.
* @param value The name of the variable (without the leading '?').
* @returns A new Variable instance.
*/
variable(value: string): rdfjs.Variable {
return new Variable(value);
}
/**
* Returns the singleton default graph instance.
* @returns The default graph instance.
*/
defaultGraph(): rdfjs.DefaultGraph {
return defaultGraphInstance;
}
/**
* Creates a new quad with the given subject, predicate, object, and optional graph.
* @param subject The subject of the quad.
* @param predicate The predicate of the quad.
* @param object The object of the quad.
* @param graph The graph of the quad (defaults to the default graph).
* @returns A new Quad instance.
*/
quad(
subject: rdfjs.Quad_Subject,
predicate: rdfjs.Quad_Predicate,
object: rdfjs.Quad_Object,
graph?: rdfjs.Quad_Graph
): rdfjs.Quad {
return new Quad(subject, predicate, object, graph);
}
/**
* Creates a copy of an existing term.
* @param original The term to copy.
* @returns A new term instance with the same value.
*/
fromTerm<T extends rdfjs.Term>(original: T): T {
switch (original.termType) {
case "NamedNode":
return this.namedNode(original.value) as unknown as T;
case "BlankNode":
return this.blankNode(original.value) as unknown as T;
case "Literal": {
const literal = original as rdfjs.Literal;
if (literal.language) {
return this.literal(literal.value, literal.language) as unknown as T;
}
return this.literal(literal.value, this.fromTerm(literal.datatype)) as unknown as T;
}
case "Variable":
return this.variable(original.value) as unknown as T;
case "DefaultGraph":
return this.defaultGraph() as unknown as T;
case "Quad": {
const quad = original as unknown as rdfjs.Quad;
return this.fromQuad(quad) as unknown as T;
}
default:
throw new Error(`Unknown term type: ${(original as rdfjs.Term).termType}`);
}
}
/**
* Creates a copy of an existing quad.
* @param original The quad to copy.
* @returns A new quad instance with the same values.
*/
fromQuad(original: rdfjs.Quad): rdfjs.Quad {
return this.quad(
this.fromTerm(original.subject),
this.fromTerm(original.predicate),
this.fromTerm(original.object),
this.fromTerm(original.graph)
);
}
/**
* Resets the blank node counter. Useful for testing.
*/
resetBlankNodeCounter(): void {
this.blankNodeCounter = 0;
}
}
/**
* A default DataFactory instance for convenient use.
*/
export const dataFactory = new DataFactory();
|