Skip to content

Commit

Permalink
fix: rebase on relative IRIs if its called on an relative IRI ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
bergos committed Jan 14, 2024
1 parent c6da3c2 commit cdbc311
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rebase.js
@@ -1,3 +1,5 @@
const isIriRegExp = /^[a-z][a-z0-9+.-]*:/

function rebaseDataset (oldTerm, newTerm, { factory }) {
const rebase = rebaseQuad(oldTerm, newTerm, { factory })

Expand Down Expand Up @@ -39,6 +41,10 @@ function rebaseTerm (oldTerm, newTerm, { factory }) {
return term
}

if (isIriRegExp.test(term.value) !== isIriRegExp.test(oldTerm.value)) {
return term
}

return factory.namedNode(newTerm.value + term.value.slice(oldTerm.value.length))
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/Grapoi.test.js
Expand Up @@ -590,6 +590,24 @@ describe('Grapoi', () => {
grapoiEqual(result, expectedGrapoi)
datasetEqual(result.dataset, expectedGrapoi.dataset)
})

it('should rebase datasets with empty named nodes', () => {
const { expectedGrapoi, expectedTerm, grapoi } = datasets.rebaseEmpty()

const result = grapoi.rebase(expectedTerm)

grapoiEqual(result, expectedGrapoi)
datasetEqual(result.dataset, expectedGrapoi.dataset)
})

it('should rebase datasets with relative named nodes', () => {
const { expectedGrapoi, expectedTerm, grapoi } = datasets.rebaseRelative()

const result = grapoi.rebase(expectedTerm)

grapoiEqual(result, expectedGrapoi)
datasetEqual(result.dataset, expectedGrapoi.dataset)
})
})

describe('.replace', () => {
Expand Down
46 changes: 46 additions & 0 deletions test/support/datasets.multi.js
Expand Up @@ -158,6 +158,26 @@ triples.rebased = [
[links[1], ns.other.propertyB, ns.other.end]
]

triples.rebaseEmpty = [
[factory.namedNode(''), ns.ex.propertyA, links[0]],
[links[0], ns.ex.propertyB, factory.namedNode('end')]
]

triples.rebasedEmpty = [
[ns.exCom(''), ns.ex.propertyA, links[0]],
[links[0], ns.ex.propertyB, ns.exCom.end]
]

triples.rebaseRelative = [
[factory.namedNode('http'), ns.ex.propertyA, links[0]],
[links[0], ns.ex.propertyB, factory.namedNode('httpend')]
]

triples.rebasedRelative = [
[ns.exCom(''), ns.ex.propertyA, links[0]],
[links[0], ns.ex.propertyB, ns.exCom.end]
]

triples.replace = [
[ns.ex(''), ns.ex.propertyA, links[0]],
[links[0], ns.ex.propertyB, ns.ex.end],
Expand Down Expand Up @@ -692,6 +712,32 @@ multi.rebase = () => {
return { expectedGrapoi, expectedTerm, ...others }
}

multi.rebaseEmpty = () => {
const { ...others } = createPathListDataset(triples.rebaseEmpty, { terms: [factory.namedNode('')] })

const expectedTerm = ns.exCom('')

const expectedGrapoi = new Grapoi({
dataset: factory.dataset(triples.rebasedEmpty.map(parts => factory.quad(...parts))),
term: expectedTerm
})

return { expectedGrapoi, expectedTerm, ...others }
}

multi.rebaseRelative = () => {
const { ...others } = createPathListDataset(triples.rebaseRelative, { terms: [factory.namedNode('http')] })

const expectedTerm = ns.exCom('')

const expectedGrapoi = new Grapoi({
dataset: factory.dataset(triples.rebasedRelative.map(parts => factory.quad(...parts))),
term: expectedTerm
})

return { expectedGrapoi, expectedTerm, ...others }
}

multi.replace = () => {
const { ...others } = createPathListDataset(triples.replace, { terms: [ns.ex('')] })

Expand Down

0 comments on commit cdbc311

Please sign in to comment.