rdf-terms

Convenience functions for handling RDFJS terms

Usage no npm install needed!

<script type="module">
  import rdfTerms from 'https://cdn.skypack.dev/rdf-terms';
</script>

README

RDF Terms

Build status Coverage Status Mutation testing badge npm version

This package contains utility functions for handling RDFJS terms of quads/triples.

Usage

The following examples assume the following imports:

import { DataFactory } from "rdf-data-factory";
import * as RdfTerms from "rdf-terms";

const factory = new DataFactory();

Constants

// Prints [ 'subject', 'predicate', 'object', 'graph' ]
console.log(RdfTerms.QUAD_TERM_NAMES);

// Prints [ 'subject', 'predicate', 'object' ]
console.log(RdfTerms.TRIPLE_TERM_NAMES);

// Prints [ 'NamedNode', 'BlankNode', 'Literal', 'Variable', 'DefaultGraph', 'Quad' ]
console.log(RdfTerms.TERM_TYPES);

Get quad terms

Get all terms from a quad.

A second optional parameter can be set to true to ignore graph terms in the default graph.

// Outputs: [ namedNode('http://example.org/s'), namedNode('http://example.org/p'), literal('abc'), namedNode('http://example.org/g') ]
RdfTerms.getTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
));

// Outputs: [ namedNode('http://example.org/s'), namedNode('http://example.org/p'), literal('abc'), defaultGraph() ]
RdfTerms.getTerms(factory.triple(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
));

// Outputs: [ namedNode('http://example.org/s'), namedNode('http://example.org/p'), literal('abc') ]
RdfTerms.getTerms(factory.triple(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
), true);

Get quad nested terms

Get all nested terms from a quad. This means that if a quad's term is a nested term, instead of returning that quad, all nested terms will be included, recursively.

A second optional parameter can be set to true to ignore graph terms in the default graph.

// Outputs: [ namedNode('http://example.org/s'), namedNode('http://example.org/p'), literal('abc'), namedNode('http://example.org/g') ]
RdfTerms.getTerms(factory.quad(
  factory.quad(
    namedNode('http://example.org/a'),
    namedNode('http://example.org/b'),
    namedNode('http://example.org/c'),
    namedNode('http://example.org/d'),
  ),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
));

// Outputs: [ namedNode('http://example.org/a'), namedNode('http://example.org/b'), namedNode('http://example.org/c'), namedNode('http://example.org/d'), namedNode('http://example.org/s'), namedNode('http://example.org/p'), literal('abc'), defaultGraph() ]

Get named quad terms

Get all terms from a quad labelled with the quad term name.

This is the reverse operation from collectNamedTerms.

// Outputs: [ { subject: namedNode('http://example.org/s') }, { predicate: namedNode('http://example.org/p') }, { object: literal('abc') }, { graph: namedNode('http://example.org/g') } ]
RdfTerms.getNamedTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
));

Collect named quad terms

Create a quad from a an array of named quad terms.

This is the reverse operation from getNamedTerms.

An second optional callback method can be provided to fill in missing terms

// Outputs: quad(namedNode('http://example.org/s'), namedNode('http://example.org/p'), literal('abc'), namedNode('http://example.org/g'))
RdfTerms.collectNamedTerms([
  { subject: factory.namedNode('http://example.org/s') },
  { predicate: factory.namedNode('http://example.org/p') },
  { object: factory.literal('abc') },
  { graph: factory.namedNode('http://example.org/g') },
]);

// Outputs: quad(namedNode('http://example.org/s'), namedNode('http://example.org/newNode'), literal('abc'), namedNode('http://example.org/g'))
RdfTerms.collectNamedTerms([
  { subject: factory.namedNode('http://example.org/s') },
  // Missing predicate
  { object: factory.literal('abc') },
  { graph: factory.namedNode('http://example.org/g') },
], (termName) => factory.namedNode('http://example.org/newNode'));

An third optional argument can be passed to set a custom data factory

RdfTerms.collectNamedTerms([
  { subject: factory.namedNode('http://example.org/s') },
  { predicate: factory.namedNode('http://example.org/p') },
  { object: factory.literal('abc') },
  { graph: factory.namedNode('http://example.org/g') },
], null, myDataFactory);

Iterate over quad terms

Invokes a callback for each term in the quad.

// Outputs:
// subject: http://example.org/s
// predicate: http://example.org/p
// object: abc
// graph: http://example.org/g
RdfTerms.forEachTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (value, key) => console.log(key + ': ' + value.value));

Filter quad terms

Returns all quad terms that return true for a given filter.

// Output: [namedNode('http://example.org/p')]
RdfTerms.filterTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (value, key) => key === 'predicate');

Filter quad term names

Returns all quad term names that return true for a given filter.

// Output: ['predicate']
RdfTerms.filterQuadTermNames(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (value, key) => value.equals(namedNode('http://example.org/p')));

Map quad terms

Map all quad terms to form a new quad.

// Output: quad(namedNode('http://subject'), namedNode('http://predicate'), namedNode('http://object'), namedNode('http://graph'))
RdfTerms.mapTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (value, key) => namedNode('http://' + key));

An second optional argument can be passed to set a custom data factory

// Output: quad(namedNode('http://subject'), namedNode('http://predicate'), namedNode('http://object'), namedNode('http://graph'))
RdfTerms.mapTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
),
  (value, key) => namedNode('http://' + key),
  myDataFactory);

Reduce quad terms

Reduce the quad terms to a single value.

// Output: "START: http://example.org/s, http://example.org/p, abc, http://example.org/g"
RdfTerms.reduceTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (previous, value, key) => previous + ', ' + value.value, 'START: ');

Every quad terms

Determines whether all terms satisfy the specified test.

// Output: false
RdfTerms.everyTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');

// Output: true
RdfTerms.everyTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  namedNode('http://example.org/o'),
  namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');

Some quad terms

Determines whether at least one term satisfies the specified test.

// Output: true
RdfTerms.someTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');

// Output: true
RdfTerms.someTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  namedNode('http://example.org/o'),
  namedNode('http://example.org/g'),
), (value, key) => value.termType === 'NamedNode');

// Output: false
RdfTerms.someTerms(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  namedNode('http://example.org/o'),
  namedNode('http://example.org/g'),
), (value, key) => value.termType === 'Variable');

Match term

Determines if the given term matches with the given term.

// Output: true
RdfTerms.matchTerm(
  namedNode('http://example.org/s'),
  undefined,
);

// Output: true
RdfTerms.matchTerm(
  namedNode('http://example.org/s'),
  variable('v'),
);

// Output: true
RdfTerms.matchTerm(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/s'),
);

// Output: true
RdfTerms.matchTerm(
  factory.quad(
    namedNode('http://example.org/s'),
    namedNode('http://example.org/p'),
    literal('abc'),
    namedNode('http://example.org/g'),
  ),
  factory.quad(
    namedNode('http://example.org/s'),
    namedNode('http://example.org/p'),
    variable('o'),
    namedNode('http://example.org/g'),
  ),
);

Match pattern

Determines if the given quad matches with the given quad terms.

// Output: true
RdfTerms.matchPattern(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
),
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
);

// Output: true
RdfTerms.matchPattern(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
),
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
);

// Output: true
RdfTerms.matchPattern(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
),
  namedNode('http://example.org/s'),
  variable('someVariableP'),
  literal('abc'),
);

// Output: true
RdfTerms.matchPattern(factory.quad(
  factory.quad(
    namedNode('http://example.org/a'),
    namedNode('http://example.org/b'),
    namedNode('http://example.org/c'),
    namedNode('http://example.org/d'),
  ),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
),
  factory.quad(
    namedNode('http://example.org/a'),
    variable('someVariableP'),
    namedNode('http://example.org/c'),
    namedNode('http://example.org/d'),
  ),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
);


// Output: false
RdfTerms.matchPattern(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
),
  namedNode('http://example.org/s'),
  variable('someVariableP'),
  literal('abcdef'),
);

Match pattern complete

Determines if the given quad matches with the given quad pattern (A quad that contains zero or more variables).

// Output: true
RdfTerms.matchPatternComplete(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
));

// Output: true
RdfTerms.matchPatternComplete(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), factory.quad(
  namedNode('http://example.org/s'),
  variable('varA'),
  literal('abc'),
  variable('varB'),
));

// Output: false
RdfTerms.matchPatternComplete(factory.quad(
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
), factory.quad(
  namedNode('http://example.org/s'),
  variable('varA'),
  literal('abcdef'),
  variable('varB'),
));

Unique terms

Create an array of unique terms from the given array.

// Output: [namedNode('http://example.org/s')]
RdfTerms.uniqTerms([
  namedNode('http://example.org/s'),
  namedNode('http://example.org/s'),
]);

Get terms of type

Find all terms of the given type in the given array.

// Output: [namedNode('http://example.org/s'), namedNode('http://example.org/p'), namedNode('http://example.org/g')]
RdfTerms.getTermsOfType([
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
], 'NamedNode');

The functions getNamedNodes, getBlankNodes, getLiterals, getVariables, getDefaultGraphs are convenience variants of this function, which do not require the term type string as parameter, and perform appropriate casting in TypeScript.

// Output: [namedNode('http://example.org/s'), namedNode('http://example.org/p'), namedNode('http://example.org/g')]
RdfTerms.getNamedNodes([
  namedNode('http://example.org/s'),
  namedNode('http://example.org/p'),
  literal('abc'),
  namedNode('http://example.org/g'),
]);

License

This software is written by Ruben Taelman.

This code is released under the MIT license.