The N3 library is an implementation of the RDF.js low-level specification that lets you handle RDF in JavaScript easily. It offers:
- Parsing triples/quads from Turtle, TriG, N-Triples, N-Quads, RDF* and Notation3 (N3)
- Writing triples/quads to Turtle, TriG, N-Triples, N-Quads and RDF*
- Storage of triples/quads in memory
Tip (requires Deno): Run the following examples by typing this in your terminal:
deno run \
--allow-net --allow-run --allow-env --allow-read \
https://deno.land/x/mdrb@2.0.0/mod.ts \
https://raw.githubusercontent.com/doga/N3/main/README.md
Create an RDF quad in-memory, and print out its details.
description = ''' Running this example is safe, it will not read or write anything to your filesystem. '''
import * as N3 from 'https://esm.sh/gh/doga/N3@1.18.2/mod.mjs';
const
{ DataFactory } = N3,
{ namedNode, literal, defaultGraph, quad } = DataFactory,
myQuad = quad(
namedNode('https://person.example/#me'), // Subject
namedNode('http://xmlns.com/foaf/0.1/givenName'), // Predicate
literal('Xyz', 'en'), // Object
defaultGraph(), // Graph
);
console.info(myQuad.termType); // Quad
console.info(myQuad.value); // ''
console.info(myQuad.subject.value); // https://person.example/#me
console.info(myQuad.object.value); // Xyz
console.info(myQuad.object.datatype.value); // http://www.w3.org/1999/02/22-rdf-syntax-ns#langString
console.info(myQuad.object.language); // en
Output for the code above:
Quad
https://person.example/#me
Xyz
http://www.w3.org/1999/02/22-rdf-syntax-ns#langString
en
Read an RDF document from string.
description = ''' Running this example is safe, it will not read or write anything to your filesystem. '''
import * as N3 from 'https://esm.sh/gh/doga/N3@1.18.2/mod.mjs';
const
parser = new N3.Parser(),
document = `
PREFIX c: <http://example.org/cartoons#>
c:Tom a c:Cat.
c:Jerry
a c:Mouse;
c:smarterThan c:Tom.`
;
parser.parse(
document,
(error, quad, prefixes) => {
if (quad)
console.info('Reading quad:\n', quad);
else
console.info(`Finished reading the RDF document.\nPrefixes used were: ${JSON.stringify(prefixes)}`);
}
);
Output for the code above:
Reading quad:
P {
id: "",
_subject: O { id: "http://example.org/cartoons#Tom" },
_predicate: O { id: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" },
_object: O { id: "http://example.org/cartoons#Cat" },
_graph: B { id: "" }
}
Reading quad:
P {
id: "",
_subject: O { id: "http://example.org/cartoons#Jerry" },
_predicate: O { id: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" },
_object: O { id: "http://example.org/cartoons#Mouse" },
_graph: B { id: "" }
}
Reading quad:
P {
id: "",
_subject: O { id: "http://example.org/cartoons#Jerry" },
_predicate: O { id: "http://example.org/cartoons#smarterThan" },
_object: O { id: "http://example.org/cartoons#Tom" },
_graph: B { id: "" }
}
Finished reading the RDF document.
Prefixes used were: {"c":"http://example.org/cartoons#"}
Create RDF quads in-memory and store them in an in-memory RDF store.
description = ''' Running this example is safe, it will not read or write anything to your filesystem. '''
import * as N3 from 'https://esm.sh/gh/doga/N3@1.18.2/mod.mjs';
const
{ DataFactory } = N3,
{ namedNode, literal, defaultGraph, quad } = DataFactory,
store = new N3.Store();
store.add(
quad(
namedNode('http://ex.org/Pluto'),
namedNode('http://ex.org/type'),
namedNode('http://ex.org/Dog')
)
);
store.add(
quad(
namedNode('http://ex.org/Mickey'),
namedNode('http://ex.org/type'),
namedNode('http://ex.org/Mouse')
)
);
// Retrieve all quads
for (const quad of store)
console.log(quad);
// Retrieve Mickey's quads
for (const quad of store.match(namedNode('http://ex.org/Mickey'), null, null))
console.log(quad);
Output for the code above:
P {
id: "",
_subject: O { id: "http://ex.org/Pluto" },
_predicate: O { id: "http://ex.org/type" },
_object: O { id: "http://ex.org/Dog" },
_graph: B { id: "" }
}
P {
id: "",
_subject: O { id: "http://ex.org/Mickey" },
_predicate: O { id: "http://ex.org/type" },
_object: O { id: "http://ex.org/Mouse" },
_graph: B { id: "" }
}
P {
id: "",
_subject: O { id: "http://ex.org/Mickey" },
_predicate: O { id: "http://ex.org/type" },
_object: O { id: "http://ex.org/Mouse" },
_graph: B { id: "" }
}
This is a fork of dfjs/N3.js.
∎