Skip to content

Commit

Permalink
Merge pull request #24 from rubensworks/feature/rdfstar
Browse files Browse the repository at this point in the history
Add support for RDF*
  • Loading branch information
bergos authored Sep 2, 2020
2 parents abcc3b7 + 041057b commit ca30c53
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
7 changes: 6 additions & 1 deletion dist/rdf-data-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,15 @@ function Quad (subject, predicate, object, graph) {
}

Quad.prototype.equals = function (other) {
return !!other && other.subject.equals(this.subject) && other.predicate.equals(this.predicate) &&
// `|| !other.termType` is for backwards-compatibility with old factories without RDF* support.
return !!other && (other.termType === 'Quad' || !other.termType) &&
other.subject.equals(this.subject) && other.predicate.equals(this.predicate) &&
other.object.equals(this.object) && other.graph.equals(this.graph)
}

Quad.prototype.termType = 'Quad'
Quad.prototype.value = ''

module.exports = Quad

},{"./default-graph":4}],8:[function(require,module,exports){
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as RDF from "rdf-js";

export const defaultGraphInstance: RDF.DefaultGraph;
export function namedNode(value: string): RDF.NamedNode;
export function namedNode<Iri extends string = string>(value: Iri): RDF.NamedNode<Iri>;
export function blankNode(value?: string): RDF.BlankNode;
export function literal(value: string, languageOrDatatype?: string | RDF.NamedNode): RDF.Literal;
export function variable(value: string): RDF.Variable;
Expand Down
7 changes: 6 additions & 1 deletion lib/quad.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ function Quad (subject, predicate, object, graph) {
}

Quad.prototype.equals = function (other) {
return !!other && other.subject.equals(this.subject) && other.predicate.equals(this.predicate) &&
// `|| !other.termType` is for backwards-compatibility with old factories without RDF* support.
return !!other && (other.termType === 'Quad' || !other.termType) &&
other.subject.equals(this.subject) && other.predicate.equals(this.predicate) &&
other.object.equals(this.object) && other.graph.equals(this.graph)
}

Quad.prototype.termType = 'Quad'
Quad.prototype.value = ''

module.exports = Quad
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/rdfjs-base/data-model",
"dependencies": {
"@types/rdf-js": "^2.0.1"
"@types/rdf-js": "*"
},
"devDependencies": {
"browserify": "^16.2.2",
Expand Down
63 changes: 63 additions & 0 deletions test/quad.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function runTests (DataFactory) {
assert.equal(predicate.equals(quad.predicate), true)
assert.equal(object.equals(quad.object), true)
assert.equal(graph.equals(quad.graph), true)

assert.equal(quad.termType, 'Quad')
assert.equal(quad.value, '')
})

it('should create an object .graph set to DefaultGraph if the argument isn\'t given', function () {
Expand All @@ -31,6 +34,9 @@ function runTests (DataFactory) {
var quad = DataFactory.quad(subject, predicate, object)

assert.equal(quad.graph.equals(graph), true)

assert.equal(quad.termType, 'Quad')
assert.equal(quad.value, '')
})

describe('.equals', function () {
Expand All @@ -45,6 +51,17 @@ function runTests (DataFactory) {
assert.equal(quad1.equals(quad2), true)
})

it('should return true even if the other equal quad is from a non-RDF* factory', function () {
var subject = DataFactory.namedNode('http://example.org/subject')
var predicate = DataFactory.namedNode('http://example.org/predicate')
var object = DataFactory.namedNode('http://example.org/object')
var graph = DataFactory.namedNode('http://example.org/graph')
var quad1 = DataFactory.quad(subject, predicate, object, graph)
var quad2 = { subject, predicate, object, graph }

assert.equal(quad1.equals(quad2), true)
})

it('should return false if the subject of the other quad is not the same', function () {
var subject1 = DataFactory.namedNode('http://example.org/subject')
var subject2 = DataFactory.namedNode('http://example.com/subject')
Expand All @@ -57,6 +74,18 @@ function runTests (DataFactory) {
assert.equal(quad1.equals(quad2), false)
})

it('should return false even if the other non-equal quad is from a non-RDF* factory', function () {
var subject1 = DataFactory.namedNode('http://example.org/subject')
var subject2 = DataFactory.namedNode('http://example.com/subject')
var predicate = DataFactory.namedNode('http://example.org/predicate')
var object = DataFactory.namedNode('http://example.org/object')
var graph = DataFactory.namedNode('http://example.org/graph')
var quad1 = DataFactory.quad(subject1, predicate, object, graph)
var quad2 = { subject: subject2, predicate, object, graph }

assert.equal(quad1.equals(quad2), false)
})

it('should return false if the predicate of the other quad is not the same', function () {
var subject = DataFactory.namedNode('http://example.org/subject')
var predicate1 = DataFactory.namedNode('http://example.org/predicate')
Expand Down Expand Up @@ -102,6 +131,40 @@ function runTests (DataFactory) {

assert.equal(quad.equals(null), false)
})

it('should return false if value is another term', function () {
var subject = DataFactory.namedNode('http://example.org/subject')
var predicate = DataFactory.namedNode('http://example.org/predicate')
var object = DataFactory.namedNode('http://example.org/object')
var graph = DataFactory.namedNode('http://example.org/graph')
var quad = DataFactory.quad(subject, predicate, object, graph)

assert.equal(quad.equals(DataFactory.namedNode('http://example.org/subject')), false)
assert.equal(quad.equals(DataFactory.literal('abc')), false)
assert.equal(quad.equals(DataFactory.variable('var')), false)
assert.equal(quad.equals(DataFactory.blankNode('bnode')), false)
assert.equal(quad.equals(DataFactory.defaultGraph()), false)
})

it('should return true for an equal nested quad', function () {
var subject = DataFactory.quad(
DataFactory.namedNode('http://example.org/subjectInner1'),
DataFactory.namedNode('http://example.org/predicateInner1'),
DataFactory.namedNode('http://example.org/objectInner1')
)
var predicate = DataFactory.namedNode('http://example.org/predicate')
var object = DataFactory.quad(
DataFactory.namedNode('http://example.org/subjectInner2'),
DataFactory.namedNode('http://example.org/predicateInner2'),
DataFactory.namedNode('http://example.org/objectInner2'),
DataFactory.namedNode('http://example.org/graphInner2')
)
var graph = DataFactory.namedNode('http://example.org/graph')
var quad1 = DataFactory.quad(subject, predicate, object, graph)
var quad2 = DataFactory.quad(subject, predicate, object, graph)

assert.equal(quad1.equals(quad2), true)
})
})
})
}
Expand Down

0 comments on commit ca30c53

Please sign in to comment.