TortankJS
Node addon to parse and manipulate n3/turtle data. Uses Tortank.
Installation
Using the prebuilt node addon (GLIBC)
This will only work if you are on linux and if you have GLIBC 2.31+ installed (check with ldd --version
)
Example using docker
docker run --rm -it node:16-bookworm bash
mkdir example && cd example
npm init --yes
npm i rdf-tortank-linux
node
const tortank = require('rdf-tortank-linux')
Using the prebuilt node addon (MUSL)
This will only work if you are on linux and if you have libc.musl-x86_64 installed (check with ldd --version
)
Example using docker
docker run --rm -it node:16-bookworm bash
mkdir example && cd example
npm init --yes
npm i rdf-tortank-linux-musl
node
const tortank = require('rdf-tortank-linux-musl')
Using Rust
This is the preferred solution to target a different platform.
- Install Rust
- Install Node
- Inside your project
npm i --save-dev cargo-cp-artifact rdf-tortank
node
const tortank = require('rdf-tortank')
Example using docker
docker run --rm -it rust bash
apt update && apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && apt-get install -y nodejs
mkdir example && cd example
npm init --yes
npm i --save-dev cargo-cp-artifact rdf-tortank
node
const tortank = require('rdf-tortank')
Documentation
Statements
Filter a Model based on subject, predicate object. It uses same params as examples below, except there is no rhsPath / rhsData.
const data = `
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <bob@example.com>] .
`;
let params = ;
tortank.;
You can also use prefixes, assuming they are known by the model. In the previous example, you could also do this:
let paramsWithPrefix = ;
tortank.;
Difference
Creates a new, indepependent, model containing all the statements in the left model that are not in the right model.
// diff between model a and model b, store result in a file
const paramsByPath =
try catch
// diff between model a and model b, store result in memory as javascipt object
const lhsData = `
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <bob@example.com>] .
`;
const paramsByDataAndPath =
try catch
Intersection
Creates a new, indepependent, model containing all the statements in the left model that are also in the right model.
The parameters are exactly similar to difference (see example above).
try catch
Merge
Merge two models togeter. The parameters are exactly similar to difference and intersection (see example above).
try catch
Mapping Function for In-Memory js model
It is possible to provide a mapper function to transform each triple in the model to something else. If the function returns null or undefined, the triple will be filtered.
It only works for outputType js
and in memory model.
const ;
const data = `
@prefix foaf: <http://foaf.com/>.
[ foaf:name "Alice" ] foaf:knows [
foaf:name "Bob" ;
foaf:lastName "George", "Joshua" ;
foaf:knows [
foaf:name "Eve" ] ;
foaf:mbox <bob@example.com>] .
`;
let params = ;
tortank.; // for example, but could be merge, difference,..