Skip to content

Commit

Permalink
feat: add fromTerm and fromQuad to DataFactory (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr authored Sep 18, 2024
1 parent 86a388e commit 48cb127
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/N3DataFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const DataFactory = {
defaultGraph,
quad,
triple: quad,
fromTerm,
fromQuad,
};
export default DataFactory;

Expand Down Expand Up @@ -386,3 +388,29 @@ function defaultGraph() {
function quad(subject, predicate, object, graph) {
return new Quad(subject, predicate, object, graph);
}

export function fromTerm(term) {
if (term instanceof Term)
return term;

// Term instantiated with another library
switch (term.termType) {
case 'NamedNode': return namedNode(term.value);
case 'BlankNode': return blankNode(term.value);
case 'Variable': return variable(term.value);
case 'DefaultGraph': return DEFAULTGRAPH;
case 'Literal': return literal(term.value, term.language || term.datatype);
case 'Quad': return fromQuad(term);
default: throw new Error(`Unexpected termType: ${term.termType}`);
}
}

export function fromQuad(inQuad) {
if (inQuad instanceof Quad)
return inQuad;

if (inQuad.termType !== 'Quad')
throw new Error(`Unexpected termType: ${inQuad.termType}`);

return quad(fromTerm(inQuad.subject), fromTerm(inQuad.predicate), fromTerm(inQuad.object), fromTerm(inQuad.graph));
}
90 changes: 90 additions & 0 deletions test/N3DataFactory-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DefaultGraph,
Quad,
} from '../src';
import * as DM from '@rdfjs/data-model';

describe('DataFactory', () => {
describe('namedNode', () => {
Expand Down Expand Up @@ -193,4 +194,93 @@ describe('DataFactory', () => {
));
});
});

describe('fromTerm and fromQuad', () => {
it('with a named node', () => {
const named = new NamedNode('http://ex.org/a');
expect(DataFactory.fromTerm(new NamedNode('http://ex.org/a'))).toEqual(new NamedNode('http://ex.org/a'));
expect(DataFactory.fromTerm(new NamedNode('http://ex.org/a')).equals(new NamedNode('http://ex.org/a'))).toEqual(true);
expect(DataFactory.fromTerm(named)).toBe(named);
expect(DataFactory.fromTerm(DM.namedNode('http://ex.org/a'))).toEqual(new NamedNode('http://ex.org/a'));
expect(DataFactory.fromTerm(DM.namedNode('http://ex.org/a')).equals(new NamedNode('http://ex.org/a'))).toEqual(true);
expect(DataFactory.fromTerm(DM.namedNode('http://ex.org/a'))).not.toBe(DM.namedNode('http://ex.org/a'));
expect(DataFactory.fromTerm(DM.namedNode('http://ex.org/a')).equals(DM.namedNode('http://ex.org/a'))).toBe(true);
});

it('with a blank node', () => {
const blank = new BlankNode('abc');
expect(DataFactory.fromTerm(new BlankNode('abc'))).toEqual(new BlankNode('abc'));
expect(DataFactory.fromTerm(new BlankNode('abc')).equals(new BlankNode('abc'))).toEqual(true);
expect(DataFactory.fromTerm(blank)).toBe(blank);
expect(DataFactory.fromTerm(DM.blankNode('abc'))).toEqual(new BlankNode('abc'));
expect(DataFactory.fromTerm(DM.blankNode('abc')).equals(new BlankNode('abc'))).toEqual(true);
expect(DataFactory.fromTerm(DM.blankNode('abc'))).not.toBe(DM.blankNode('abc'));
expect(DataFactory.fromTerm(DM.blankNode('abc')).equals(DM.blankNode('abc'))).toBe(true);
});

it('with a literal', () => {
const literal = new Literal('"abc"');
expect(DataFactory.fromTerm(new Literal('"abc"'))).toEqual(new Literal('"abc"'));
expect(DataFactory.fromTerm(new Literal('"abc"')).equals(new Literal('"abc"'))).toEqual(true);
expect(DataFactory.fromTerm(literal)).toBe(literal);
expect(DataFactory.fromTerm(DM.literal('abc'))).toEqual(new Literal('"abc"'));
expect(DataFactory.fromTerm(DM.literal('abc')).equals(new Literal('"abc"'))).toEqual(true);
expect(DataFactory.fromTerm(DM.literal('abc'))).not.toBe(DM.literal('abc'));
expect(DataFactory.fromTerm(DM.literal('abc')).equals(DM.literal('abc'))).toBe(true);
});

it('with a variable', () => {
const variable = new Variable('abc');
expect(DataFactory.fromTerm(new Variable('abc'))).toEqual(new Variable('abc'));
expect(DataFactory.fromTerm(new Variable('abc')).equals(new Variable('abc'))).toEqual(true);
expect(DataFactory.fromTerm(variable)).toBe(variable);
expect(DataFactory.fromTerm(DM.variable('abc'))).toEqual(new Variable('abc'));
expect(DataFactory.fromTerm(DM.variable('abc')).equals(new Variable('abc'))).toEqual(true);
expect(DataFactory.fromTerm(DM.variable('abc'))).not.toBe(DM.variable('abc'));
expect(DataFactory.fromTerm(DM.variable('abc')).equals(DM.variable('abc'))).toBe(true);
});

it('with a default graph', () => {
const defaultGraph = new DefaultGraph();
expect(DataFactory.fromTerm(new DefaultGraph())).toEqual(new DefaultGraph());
expect(DataFactory.fromTerm(new DefaultGraph()).equals(new DefaultGraph())).toEqual(true);
expect(DataFactory.fromTerm(defaultGraph)).toBe(defaultGraph);
expect(DataFactory.fromTerm(DM.defaultGraph())).toEqual(new DefaultGraph());
expect(DataFactory.fromTerm(DM.defaultGraph()).equals(new DefaultGraph())).toEqual(true);
expect(DataFactory.fromTerm(DM.defaultGraph())).not.toBe(DM.defaultGraph());
expect(DataFactory.fromTerm(DM.defaultGraph()).equals(DM.defaultGraph())).toBe(true);
});

it('with an unknown term', () => {
expect(() => DataFactory.fromTerm({})).toThrow();
expect(() => DataFactory.fromQuad({})).toThrow();
expect(() => DataFactory.fromQuad(DM.namedNode('http://ex.org/a'))).toThrow();
});

it('with a quad', () => {
const quad = new Quad(
new NamedNode('http://ex.org/a'),
new NamedNode('http://ex.org/b'),
new Literal('"abc"'),
new NamedNode('http://ex.org/d'),
);
const quad2 = DM.quad(
DM.namedNode('http://ex.org/a'),
DM.namedNode('http://ex.org/b'),
DM.literal('abc'),
DM.namedNode('http://ex.org/d'),
);
expect(DataFactory.fromTerm(quad)).toBe(quad);
expect(DataFactory.fromTerm(quad).equals(quad)).toEqual(true);
expect(DataFactory.fromTerm(quad2)).toEqual(quad);
expect(DataFactory.fromTerm(quad2).equals(quad)).toEqual(true);
expect(DataFactory.fromTerm(quad2).equals(quad2)).toEqual(true);

expect(DataFactory.fromQuad(quad)).toBe(quad);
expect(DataFactory.fromQuad(quad).equals(quad)).toEqual(true);
expect(DataFactory.fromQuad(quad2)).toEqual(quad);
expect(DataFactory.fromQuad(quad2).equals(quad)).toEqual(true);
expect(DataFactory.fromQuad(quad2).equals(quad2)).toEqual(true);
});
});
});

0 comments on commit 48cb127

Please sign in to comment.