digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.65k stars 195 forks source link

RDFJS compliance: the equals method #243

Open pietercolpaert opened 6 years ago

pietercolpaert commented 6 years ago

When using the toRDF method, the jsonld.js library does not return RDFJS spec compliant quads as the quads to not contain an equals() function as required by the spec.

Hotfixed this using the DataFactory class from the N3.js library as follows:

  addEqualsMethodToQuad (quad) {
    quad.equals = DataFactory.internal.Quad.prototype.equals;
    if (quad.subject.termType === 'NamedNode')
      quad.subject.equals = DataFactory.internal.NamedNode.prototype.equals;
    if (quad.subject.termType === 'BlankNode')
      quad.subject.equals = DataFactory.internal.BlankNode.prototype.equals;
    if (quad.predicate.termType === 'NamedNode')
      quad.predicate.equals = DataFactory.internal.NamedNode.prototype.equals;
    if (quad.predicate.termType === 'BlankNode')
      quad.predicate.equals = DataFactory.internal.BlankNode.prototype.equals;
    if (quad.object.termType === 'NamedNode')
      quad.object.equals = DataFactory.internal.NamedNode.prototype.equals;
    if (quad.object.termType === 'BlankNode')
      quad.object.equals = DataFactory.internal.BlankNode.prototype.equals;
    if (quad.object.termType === 'Literal')
      quad.object.equals = DataFactory.internal.Literal.prototype.equals;
    if (quad.graph.termType === 'NamedNode')
      quad.graph.equals = DataFactory.internal.NamedNode.prototype.equals;
    if (quad.graph.termType === 'BlankNode')
      quad.graph.equals = DataFactory.internal.BlankNode.prototype.equals;
    return quad;
  }

Fixing this into this library may allow for the N3.js library compatibility and the compatibility to the entire RDF-Ext family.

RubenVerborgh commented 6 years ago

The idea would actually be that parsers accept a factory argument, such that they can instantiate RDF.js objects of a particular library (like here).