From 48cb127377be67b822e219d6d84199570907bda1 Mon Sep 17 00:00:00 2001 From: Jesse Wright <63333554+jeswr@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:37:10 +0200 Subject: [PATCH] feat: add fromTerm and fromQuad to DataFactory (#449) --- src/N3DataFactory.js | 28 ++++++++++++ test/N3DataFactory-test.js | 90 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) diff --git a/src/N3DataFactory.js b/src/N3DataFactory.js index f38c87fa..a0624a99 100644 --- a/src/N3DataFactory.js +++ b/src/N3DataFactory.js @@ -20,6 +20,8 @@ const DataFactory = { defaultGraph, quad, triple: quad, + fromTerm, + fromQuad, }; export default DataFactory; @@ -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)); +} diff --git a/test/N3DataFactory-test.js b/test/N3DataFactory-test.js index f6d37b95..e97dde60 100644 --- a/test/N3DataFactory-test.js +++ b/test/N3DataFactory-test.js @@ -7,6 +7,7 @@ import { DefaultGraph, Quad, } from '../src'; +import * as DM from '@rdfjs/data-model'; describe('DataFactory', () => { describe('namedNode', () => { @@ -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); + }); + }); });