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-string

v1.6.3

Published

Convenience functions for creating and serializing RDF terms and quads

Downloads

86,005

Readme

RDF String

Build status Coverage Status npm version

This package contains utility functions to convert between the string-based and RDFJS representations of RDF terms, quads and triples.

If you are looking for a Turtle-based string syntax, have a look at RDF String Turtle

This allows for convenient and compact interaction with RDF terms and quads, as they can be serialized as plain JSON.

This string-based representation is based on the old triple representation of N3.js. Namely, quads are represented as follows:

{
  subject:   'http://example.org/cartoons#Tom',
  predicate: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
  object:    'http://example.org/cartoons#Cat'
  graph:     'http://example.org/myGraph'
}

Different terms types in quads are represented as follows:

  • URLs, URIs and IRIs are simple strings: 'http://example.org/cartoons#Tom'
  • Literals are represented as double quoted strings: '"Tom"', '"Tom"@en-gb', '"1"^^http://www.w3.org/2001/XMLSchema#integer'
  • Variables are prefixed by ?: '?variableName'

Usage

The following examples assume the following imports:

import * as RdfDataModel from "rdf-data-model";
import * as RdfString from "rdf-string";

Term to string

Convert an RDFJS term to the string-based representation.

// Prints http://example.org
console.log(RdfString.termToString(RdfDataModel.namedNode('http://example.org')));

// Prints _:b1
console.log(RdfString.termToString(RdfDataModel.blankNode('b1')));

// Prints "abc"
console.log(RdfString.termToString(RdfDataModel.literal('abc')));

// Prints "abc"@en-us
console.log(RdfString.termToString(RdfDataModel.literal('abc', 'en-us')));

// Prints "abc"^^http://example.org/
console.log(RdfString.termToString(RdfDataModel.literal('abc', namedNode('http://example.org/'))));

// Prints ?v1
console.log(RdfString.termToString(RdfDataModel.variable('v1')));

// Prints empty string
console.log(RdfString.termToString(RdfDataModel.defaultGraph()));

String to term

Convert an string-based term to the RDFJS representation.

Optionally, a custom RDFJS DataFactory can be provided as second argument to create terms instead of the built-in DataFactory.

// Outputs a named node
RdfString.stringToTerm('http://example.org');

// Outputs a blank node
RdfString.stringToTerm('_:b1');

// Outputs a literal
RdfString.stringToTerm('"abc"');

// Outputs a literal with a language tag
RdfString.stringToTerm('"abc"@en-us');

// Outputs a literal with a datatype
RdfString.stringToTerm('"abc"^^http://example.org/');

// Outputs a variable
RdfString.stringToTerm('?v1');

// Outputs a default graph
RdfString.stringToTerm('');

Quad to string-based quad

Convert an RDFJS quad to a string-based quad.

// Prints { subject: 'http://example.org', predicate: 'http://example.org', object: '"abc"', graph: '' }
console.log(RdfString.quadToStringQuad(RdfDataModel.triple(
  namedNode('http://example.org'),
  namedNode('http://example.org'),
  literal('abc'),
)));

String-based quad to quad

Converts a string-based quad to an RDFJS quad.

Optionally, a custom RDFJS DataFactory can be provided as second argument to create quads and terms instead of the built-in DataFactory.

// Outputs a quad
RdfString.stringQuadToQuad({
  subject: 'http://example.org',
  predicate: 'http://example.org',
  object: '"abc"',
  graph: '',
});

License

This software is written by Ruben Taelman. These utility functions are inspired by the implementation of N3.js.

This code is released under the MIT license.