Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RDF* #24

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "*"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this dependency is relaxed to * instead of ^4?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A ^4 range excludes future major updates when that package becomes 5.0. This can cause npm to install duplicate versions of the typings package leading to compile issues from TypeScript, especially in monorepo scenarios

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused: compile errors is exactly what one would want if the typings are incompatible, no?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's false negatives I'm talking about. Not really about incompatible types. The error looks similar to

src/index.ts(3,14): error TS2742: The inferred type of 'NamedNode' cannot be named without a reference to '../packages/in-monorepo/node_modules/rdf-js'. This is likely not portable. A type annotation is necessary.

},
"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