RDF Terms
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
console.log(RdfTerms.QUAD_TERM_NAMES);
console.log(RdfTerms.TRIPLE_TERM_NAMES);
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.
RdfTerms.getTerms(factory.quad(
namedNode('http://example.org/s'),
namedNode('http://example.org/p'),
literal('abc'),
namedNode('http://example.org/g'),
));
RdfTerms.getTerms(factory.triple(
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.
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'),
));
Get named quad terms
Get all terms from a quad labelled with the quad term name.
This is the reverse operation from collectNamedTerms
.
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
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') },
]);
RdfTerms.collectNamedTerms([
{ subject: factory.namedNode('http://example.org/s') },
{ 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.
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.
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.
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.
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
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.
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.
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');
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.
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');
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');
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.
RdfTerms.matchTerm(
namedNode('http://example.org/s'),
undefined,
);
RdfTerms.matchTerm(
namedNode('http://example.org/s'),
variable('v'),
);
RdfTerms.matchTerm(
namedNode('http://example.org/s'),
namedNode('http://example.org/s'),
);
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.
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'),
);
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'),
);
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'),
);
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'),
);
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).
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'),
));
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'),
));
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.
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.
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.
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.