zazuko / rdf-validate-shacl

Validate RDF data purely in JavaScript. An implementation of the W3C SHACL specification on top of the RDFJS stack.
MIT License
98 stars 13 forks source link

Code Snippet in README #74

Closed tobiasschweizer closed 3 years ago

tobiasschweizer commented 3 years ago

The code snippet in the README throws an error:

SyntaxError: await is only valid in async function

This can easily be solved by putting the code in an async function (e.g., main) and calling it right after.

I could submit a small PR. What do you think?

ktk commented 3 years ago

yes please!

tobiasschweizer commented 3 years ago

ok, will do :-)

tobiasschweizer commented 3 years ago

@ktk I opened #75

Since I work with JSON-LD data, I added the following method for convenience (dep: jsonld):

const jsonld = require('jsonld')

async function loadRDFFromJSONLD(filePath) {
  const doc = fs.readFileSync(filePath);
  const jsonldDoc = JSON.parse(doc);
  const dataRDF = await jsonld.toRDF(jsonldDoc, {format: 'application/n-quads'});

  const input = new Readable({
    read: () => {
      input.push(dataRDF)
      input.push(null)
    }
  })

  const parser = new ParserN3({ factory })
  return factory.dataset().import(parser.import(input))
}

If you think that's useful, I'd be glad to contribute that too.