Skip to content

Commit

Permalink
feat: toSparql from object (#39)
Browse files Browse the repository at this point in the history
* feat: toSparql from object

* test: typo
  • Loading branch information
tpluscode authored Oct 24, 2024
1 parent 2cd890b commit d4af8b4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-goats-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"clownface-shacl-path": minor
---

Allow calling `toSparql` with a path object
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions src/lib/toSparql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NamedNode } from '@rdfjs/types'
import { SparqlTemplateResult, sparql } from '@tpluscode/rdf-string'
import { MultiPointer } from 'clownface'
import { assertWellFormedPath, fromNode, PathVisitor } from './path.js'
import { assertWellFormedPath, fromNode, PathVisitor, ShaclPropertyPath } from './path.js'
import * as Path from './path.js'

class ToSparqlPropertyPath extends PathVisitor<SparqlTemplateResult, { isRoot: boolean }> {
Expand Down Expand Up @@ -59,11 +59,14 @@ class ToSparqlPropertyPath extends PathVisitor<SparqlTemplateResult, { isRoot: b
/**
* Creates a SPARQL template string which represents a SHACL path as Property Path
*
* @param path SHACL Property Path
* @param pathOrNode SHACL Property Path
*/
export function toSparql(path: MultiPointer | NamedNode): SparqlTemplateResult {
export function toSparql(pathOrNode: MultiPointer | NamedNode | ShaclPropertyPath): SparqlTemplateResult {
const visitor = new ToSparqlPropertyPath()
return visitor.visit(fromNode(path))
const path = 'termType' in pathOrNode || '_context' in pathOrNode
? fromNode(pathOrNode)
: pathOrNode
return visitor.visit(path)
}

/**
Expand Down
13 changes: 12 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it } from 'mocha'
import { schema, sh, skos, foaf, rdf, owl } from '@tpluscode/rdf-ns-builders'
import type { GraphPointer } from 'clownface'
import RDF from '@zazuko/env-node'
import { findNodes, fromNode, toAlgebra, toSparql } from '../src/index.js'
import { findNodes, fromNode, toAlgebra, toSparql, PredicatePath } from '../src/index.js'
import { any, blankNode, namedNode, parse } from './nodeFactory.js'

const tbbt = RDF.namespace('http://example.com/')
Expand Down Expand Up @@ -322,6 +322,17 @@ describe('clownface-shacl-path', () => {
})

describe('toSparql', () => {
it('accepts existing path object', () => {
// given
const path = new PredicatePath(schema.knows)

// when
const sparql = toSparql(path).toString({ prologue: false })

// then
expect(sparql).to.eq('schema:knows')
})

it('converts direct path', () => {
// given
const path = schema.knows
Expand Down

0 comments on commit d4af8b4

Please sign in to comment.