Skip to content

Commit

Permalink
feat: graph traversals
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jul 16, 2020
1 parent b1a4500 commit c97a1c6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/Clownface.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ class Clownface {
return this
}

fromGraph (graph) {
if (typeof graph === 'undefined') {
throw new Error('predicate parameter is required')
}

const context = this._context.map(context => context.clone({
graph: graph === null ? undefined : graph
}))

return Clownface.fromContext(context)
}

get defaultGraph () {
return rdf.defaultGraphInstance
}

_toTermArray (predicates, type, languageOrDatatype) {
return toTermArray(predicates, type, languageOrDatatype, this.factory)
}
Expand Down
9 changes: 7 additions & 2 deletions lib/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ class Context {
this.term = term(value, undefined, undefined, factory)
}

clone ({ dataset = this.dataset, graph = this.graph, value, factory = this.factory, namespace = this.namespace }) {
return new Context({ dataset, graph, value, factory, namespace })
clone ({ dataset = this.dataset, graph, value, factory = this.factory, namespace = this.namespace }) {
let g = graph
if (typeof graph === 'undefined') {
g = undefined
}

return new Context({ dataset, graph: g, value, factory, namespace })
}

has (predicate, object) {
Expand Down
59 changes: 59 additions & 0 deletions test/Clownface/fromGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* global describe, it */

const assert = require('assert')
const { rdfs } = require('@tpluscode/rdf-ns-builders')
const clownface = require('../..')
const rdf = require('../support/factory')
const ns = require('../support/namespace')

describe('fromGraph', () => {
it('called with graph changes context to same term in named graph', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term })

const movedPointer = pointer.fromGraph(ns.ex.NamedGraph)

assert.deepStrictEqual(movedPointer.out(rdfs.label).value, 'named graph')
})

it('called with null changes context to same term across all graphs', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term, graph: ns.ex.NamedGraph })

const movedPointer = pointer.fromGraph(null)

assert.deepStrictEqual(movedPointer.out(rdfs.label).values.length, 2)
})

it('called with undefined should throw', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term })

assert.throws(() => pointer.fromGraph(undefined))
})

it('called with default graph instance changes context to same term in default graph', () => {
const term = ns.ex.Foo
const dataset = rdf.dataset([
rdf.quad(term, rdfs.label, rdf.literal('default graph')),
rdf.quad(term, rdfs.label, rdf.literal('named graph'), ns.ex.NamedGraph)
])
const pointer = clownface({ dataset, term })

const movedPointer = pointer.fromGraph(rdf.defaultGraphInstance)

assert.deepStrictEqual(movedPointer.out(rdfs.label).value, 'default graph')
})
})
1 change: 1 addition & 0 deletions test/Clownface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ describe('Clownface', () => {
require('./deleteIn')
require('./deleteOut')
require('./deleteList')
require('./fromGraph')
})

0 comments on commit c97a1c6

Please sign in to comment.