Created
December 4, 2021 12:10
-
-
Save hrchu/b463193ab2dd7dacd30e33469676835e to your computer and use it in GitHub Desktop.
Use N3.js in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/rdfjs/N3.js/ | |
import {Parser, StreamParser, Writer} from "n3"; | |
import * as fs from "fs"; | |
import {DataFactory} from 'n3'; | |
import {Writable} from 'stream'; | |
function fromQualsToAString() { | |
const writer = new Writer({prefixes: {c: 'http://example.org/cartoons#'}}); | |
const {namedNode, literal, defaultGraph, quad} = DataFactory; | |
writer.addQuad( | |
namedNode('http://example.org/cartoons#Tom'), | |
namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), | |
namedNode('http://example.org/cartoons#Cat') | |
); | |
writer.addQuad(quad( | |
namedNode('http://example.org/cartoons#Tom'), | |
namedNode('http://example.org/cartoons#name'), | |
literal('Tom') | |
)); | |
writer.end((error, result) => console.log(result)); | |
} | |
function parsingFromStream() { | |
const parser = new Parser(); | |
const rdfStream = fs.createReadStream('../cartoons.ttl'); | |
// @ts-ignore | |
parser.parse(rdfStream, console.log); | |
// const streamParser = new StreamParser(); | |
// const rdfStream = fs.createReadStream('cartoons.ttl'); | |
// rdfStream.pipe(streamParser); | |
// streamParser.pipe(new SlowConsumer()); | |
// | |
// function SlowConsumer() { | |
// const writer = new Writable({ objectMode: true }); | |
// writer._write = (quad, encoding, done) => { | |
// console.log(quad); | |
// setTimeout(done, 1000); | |
// }; | |
// return writer; | |
// } | |
} | |
function parsingFromDocument() { | |
const parser = new Parser(); | |
parser.parse( | |
`PREFIX c: <http://example.org/cartoons#> | |
c:Tom a c:Cat. | |
c:Jerry a c:Mouse; | |
c:smarterThan c:Tom.`, | |
(error, quad, prefixes) => { | |
if (quad) | |
console.log(quad); | |
else | |
console.log("# That's all, folks!", prefixes); | |
}); | |
} | |
function creating() { | |
const {namedNode, literal, defaultGraph, quad} = DataFactory; | |
const myQuad = quad( | |
namedNode('https://ruben.verborgh.org/profile/#me'), | |
namedNode('http://xmlns.com/foaf/0.1/givenName'), | |
literal('Ruben', 'en'), | |
defaultGraph(), | |
); | |
console.log(myQuad.termType); // Quad | |
console.log(myQuad.value); // '' | |
console.log(myQuad.subject.value); // https://ruben.verborgh.org/profile/#me | |
console.log(myQuad.predicate.value); | |
console.log(myQuad.object.value); // Ruben | |
// @ts-ignore | |
console.log(myQuad.object.datatype.value); // http://www.w3.org/1999/02/22-rdf-syntax-ns#langString | |
// @ts-ignore | |
console.log(myQuad.object.language); // en | |
} | |
fromQualsToAString() | |
// parsingFromStream() | |
// parsingFromDocument() | |
// creating() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment