npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rdf-data-factory

v1.1.2

Published

A TypeScript/JavaScript implementation of the RDF/JS data factory.

Downloads

194,844

Readme

RDF Data Factory

Build status Coverage Status npm version

This package contains an implementation of the RDF/JS Data model. It works in both JavaScript and TypeScript.

Concretely, it provides an implementation of the following interfaces:

  • DataFactory: A factory for instantiating RDF terms and quads.
  • NamedNode: A term that contains an IRI.
  • BlankNode: A term that represents an RDF blank node with a label.
  • Literal: A term that represents an RDF literal, containing a string with an optional language tag or datatype.
  • Variable: A term that represents a variable.
  • DefaultGraph: A singleton term instance that represents the default graph.

If using TypeScript, it is recommended to use this in conjunction with @types/rdf-js.

Installation

$ npm install rdf-data-factory

or

$ yarn add rdf-data-factory

This package also works out-of-the-box in browsers via tools such as webpack and browserify.

Usage

It is recommended to always create terms via a DataFactory instance:

import { DataFactory } from 'rdf-data-factory';
import * as RDF from 'rdf-js';

const factory: RDF.DataFactory = new DataFactory();

You can pass the following option to define a blank node prefix:

const factory: RDF.DataFactory = new DataFactory({ blankNodePrefix: 'bnode_' });

If no blankNodePrefix is passed, it will generate a unique prefix of the form df_[0-9]+_, which ensures there will be no blank nodes clashes when instantiating multiple factories.

Creating named nodes

const term: RDF.NamedNode = factory.namedNode('http://example.org');
console.log(term.value); // 'http://example.org'
console.log(term.termType); // 'NamedNode'
console.log(term.equals(term)); // true

Creating blank nodes

With a given blank node label:

const term: RDF.BlankNode = factory.blankNode('bnode');
console.log(term.value); // 'bnode'
console.log(term.termType); // 'BlankNode'
console.log(term.equals(term)); // true

Autogenerate a blank node label using an internal blank node counter:

const term: RDF.BlankNode = factory.blankNode();
console.log(term.value); // 'df-0'
console.log(term.termType); // 'BlankNode'
console.log(term.equals(term)); // true

Reset the blank node label counter:

factory.resetBlankNodeCounter();

Creating literals

Plain string literal:

const term: RDF.Literal = factory.literal('abc');
console.log(term.value); // 'abc'
console.log(term.termType); // 'Literal'
console.log(term.language); // ''
console.log(term.datatype); // namedNode('http://www.w3.org/2001/XMLSchema#string')
console.log(term.equals(term)); // true

Languaged tagged string literal:

const term: RDF.Literal = factory.literal('abc', 'en-us');
console.log(term.value); // 'abc'
console.log(term.termType); // 'Literal'
console.log(term.language); // 'en-us'
console.log(term.datatype); // namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString')
console.log(term.equals(term)); // true

Datatyped literal:

const term: RDF.Literal = factory.literal('1.2', factory.namedNode('http://www.w3.org/2001/XMLSchema#double'));
console.log(term.value); // 'abc'
console.log(term.termType); // 'Literal'
console.log(term.language); // ''
console.log(term.datatype); // namedNode('http://www.w3.org/2001/XMLSchema#double')
console.log(term.equals(term)); // true

Creating variables

const term: RDF.Variable = factory.variable('myVar');
console.log(term.value); // 'myVar'
console.log(term.termType); // 'Variable'
console.log(term.equals(term)); // true

Getting the default graph

This will always produce the same default graph instance;

const term: RDF.DefaultGraph = factory.defaultGraph();
console.log(term.value); // ''
console.log(term.termType); // 'DefaultGraph'
console.log(term.equals(term)); // true

Creating quads

Create a triple in the default graph:

const quad: RDF.Quad = factory.quad(
  factory.namedNode('ex:s'),
  factory.namedNode('ex:p'),
  factory.literal('o'),
);
console.log(term.subject); // An RDF.Term
console.log(term.predicate); // An RDF.Term
console.log(term.object); // An RDF.Term
console.log(term.graph); // An RDF.Term, in this case defaultGraph()
console.log(quad.equals(quad)); // true

Create a triple in a named graph:

const quad: RDF.Quad = factory.quad(
  factory.namedNode('ex:s'),
  factory.namedNode('ex:p'),
  factory.literal('o'),
  factory.namedNode('ex:g'),
);
console.log(term.subject); // An RDF.Term
console.log(term.predicate); // An RDF.Term
console.log(term.object); // An RDF.Term
console.log(term.graph); // An RDF.Term
console.log(quad.equals(quad)); // true

Since a Quad is also a Term, it is possible to annotate Quad's by nesting them:

const quad: RDF.Quad = factory.quad(
  factory.quad(
    factory.namedNode('ex:s'),
    factory.namedNode('ex:p1'),
    factory.literal('o'),
  ),
  factory.namedNode('ex:p2'),
  factory.literal('o'),
);

Copying terms

Create a deep copy of the given term:

const term1 = factory.namedNode('ex:s');
const term1 = factory.fromTerm(term1);

This is useful if you need to transform terms from another data factory.

Copying quads

Create a deep copy of the given quad:

const quad1: RDF.Quad = factory.quad(
  factory.namedNode('ex:s'),
  factory.namedNode('ex:p'),
  factory.literal('o'),
);
const quad2 = factory.fromQuad(quad1);

This is useful if you need to transform quads from another data factory.

Nested quads will be copied recursively to produce an actual deep copy.

License

This software is written by Ruben Taelman.

This code is released under the MIT license.