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

v2.0.0

Published

Loads RDF as JSON objects

Downloads

23,762

Readme

RDF Object

Build status Coverage Status npm version

RDF Object makes it easier to read RDF data by loading it as JSON objects.

This library accepts RDFJS-compliant quads.

Installation

$ yarn install rdf-object

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

Require

import {RdfObjectLoader} from "rdf-object";

or

const RdfObjectLoader = require("rdf-object").RdfObjectLoader;

Usage

RdfObjectLoader accepts RDF quad/triple streams (or arrays) as input, and loads the resulting graph in-memory as linked Resources.

A Resource is a wrapper over an RDFJS term that holds property links. Using a JSON-LD context, properties are easily accessible.

Examples

The following examples assume the following imports:

import {namedNode, literal, triple} from "@rdfjs/data-model"; // External library
import {RdfObjectLoader} from "rdf-object";

Create an object loader

RdfObjectLoader accepts a JSON-LD context as input argument.

This context can also be the URL to a JSON-LD context.

// Initialize our loader with a JSON-LD context
const context = {
  'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
  'type': 'rdf:type',
  'label': 'rdfs:label',
  'foaf': 'http://xmlns.com/foaf/0.1/',
  'knows': 'foaf:knows',
  'name': 'foaf:name',
  'ex': 'http://example.org/'
};
const myLoader = new RdfObjectLoader({ context });

Get resources after importing

Resources are stored inside the resources field of RdfObjectLoader, they are indexed by URI.

Each resource has the following fields:

// Import triples
myLoader.importArray([
  triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
]).then(() => {
  // Get property values by shortcut
  const myResource = myLoader.resources['http://example.org/myResource'];
  console.log(`URI:  ${myResource.value}`);
  console.log(`Term type: ${myResource.type}`);
  console.log(`Term value: ${myResource.value}`);
  console.log(`Term: ${myResource.term}`);
});

Alternatively, myLoader.import() can be invoked on an RDFJS stream of triples/quads. This can for example accept parsed turtle streams.

Multiple calls to importArray and import can be done at any time to easily combined multiple sources.

Get properties by shortcut

The property field on a Resource contains all property values. It maps all predicates to objects, where each predicate is a URI or JSON-LD shortcut, and each object is a Resource.

If multiple values are present for that property, only the first will be returned.

// Import triples
myLoader.importArray([
  triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
  triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Resource')),
]).then(() => {
  // Get property values by shortcut
  const myResource = myLoader.resources['http://example.org/myResource'];
  console.log(`URI:  ${myResource}`);
  console.log(`Type: ${myResource.property.type}`);
  console.log(`Label: ${myResource.property['rdfs:label']}`);
  console.log(`Label (bis): ${myResource.property['http://www.w3.org/1999/02/22-rdf-syntax-ns#label']}`);
});

Get multiple property values

Via the properties field on Resource, all values of a property can be retrieved.

JSON-LD can also be used on properties.

// Import triples
myLoader.importArray([
  triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), namedNode('http://example.org/Resource')),
  triple(namedNode('http://example.org/myResource'), namedNode('http://www.w3.org/2000/01/rdf-schema#label'), literal('My Resource')),
]).then(() => {
  // Get property values by shortcut
  const myResource = myLoader.resources['http://example.org/myResource'];
  console.log(`Labels: ${myResource.properties.label}`);
});

The raw properties (without JSON-LD shortcuts) are available in the propertiesUri field on Resource.

Set properties by shortcut

The property and properties field can also be used to set new values on a resource.

For example:

// Sets a single property value
myResource.property['rdfs:label'] = new Resource({ term: literal('Name') });

// Sets multiple property values
myResource.properties['rdfs:label'].push(new Resource({ term: literal('Name 1') }));
myResource.properties['rdfs:label'].push(new Resource({ term: literal('Name 2') }));

Resources are nested

As Resource properties map to other Resources, nested property paths can be followed easily.

// Import triples
myLoader.importArray([
  triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/knows'), namedNode('https://ruben.verborgh.org/profile/#me')),
  triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/knows'), namedNode('https://data.verborgh.org/people/joachim_van_herwegen')),
  triple(namedNode('https://www.rubensworks.net/#me'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Ruben Taelman')),
  triple(namedNode('https://ruben.verborgh.org/profile/#me'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Ruben Verborgh')),
  triple(namedNode('https://data.verborgh.org/people/joachim_van_herwegen'), namedNode('http://xmlns.com/foaf/0.1/name'), literal('Joachim Van Herwegen')),
]).then(() => {
  // Get friend names via nested resources
  const rubenT = myLoader.resources['https://www.rubensworks.net/#me'];
  console.log(`Friends of ${rubenT.property.name}:`);
  for (const friend of rubenT.properties.friends) {
    console.log(`* ${friend.property.name}`);
  }
});

Conveniently access RDF lists

RDF lists are automatically parsed and exposed as a JavaScript array via the list field on Resource.

// Import triples
myLoader.importArray([
  triple(namedNode('http://example.org/myResource'), namedNode('http://example.org/list'), namedNode('http://example.org/myList0')),
  triple(namedNode('http://example.org/myList0'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('A')),
  triple(namedNode('http://example.org/myList0'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://example.org/myList1')),
  triple(namedNode('http://example.org/myList1'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('B')),
  triple(namedNode('http://example.org/myList1'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://example.org/myList2')),
  triple(namedNode('http://example.org/myList2'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), literal('C')),
  triple(namedNode('http://example.org/myList2'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest'), namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil')),
]).then(() => {
  // Get friend names via nested resources
  const myResource = myLoader.resources['http://example.org/myResource'];
  console.log(`List values of ${myResource}`);
  for (const listElement of myResource.property['ex:list'].list) {
    console.log(`* ${listElement}`);
  }
});

If you don't want RDF lists to be parsed automatically, you can set normalizeLists to false in the RdfObjectLoader constructor.

Conveniently construct Resources

If you want to create custom Resources yourself, for example during testing, then you can create them for any given term:

myLoader.getOrMakeResource(namedNode('ex:myResource'));

Alternatively, you can use createCompactedResource to easily create a resource with compacted properties:

myLoader.createCompactedResource({
  '@id': 'http://example.org/myId',
  propertyLiteral: '"abc"',
  propertyWithList: {
    list: [
      '"abc"'
    ]
  },
  propertyWithNestedHash: {
    nestedProperty: {
      '@id': 'http://example.org/mySubId',
    }
  },
  propertyWithResource: myLoader.getOrMakeResource(namedNode('ex:myResource')),
});

Special field cases:

  • '@id' represents the IRI identifier.
  • 'list' is considered an RDF list.

Values can be nested hashes, for which other Resources will be created. String values will be converted into term sources following the semantics of rdf-string.js. Values can also be Resources or RDF/JS terms.

createCompactedResources is equivalent, but accepts an array (or hash) as input, and converts it into an array of resources.

License

This software is written by Ruben Taelman.

This code is released under the MIT license.