Skip to content

Commit c8d716b

Browse files
committed
added support for context via options
1 parent aee9c2e commit c8d716b

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/ParserStream.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class ParserStream extends Readable {
1313
options = options || {}
1414

1515
this.baseIRI = options.baseIRI || ''
16+
this.context = options.context
1617
this.factory = options.factory || rdf
1718
this.blankNodes = {}
1819

@@ -62,7 +63,14 @@ class ParserStream extends Readable {
6263
})
6364
}
6465

65-
return jsonld.promises().toRDF(json, {base: this.baseIRI})
66+
const toRdfOptions = {base: this.baseIRI}
67+
68+
// use context from options if given
69+
if (this.context) {
70+
toRdfOptions.expandContext = this.context
71+
}
72+
73+
return jsonld.promises().toRDF(json, toRdfOptions)
6674
}).then((rawGraph) => {
6775
Object.keys(rawGraph).forEach((graphIri) => {
6876
const graph = graphIri !== '@default' ? this.factory.namedNode(graphIri) : null

test/test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,34 @@ describe('rdf-parser-jsond', () => {
265265
})
266266
})
267267

268+
it('should use context option', () => {
269+
const example = {
270+
'@id': 'subject',
271+
'predicate': 'object'
272+
}
273+
274+
const context = {
275+
'@vocab': 'http://example.org/'
276+
}
277+
278+
const parser = new JSONLDParser({
279+
baseIRI: 'http://example.org/',
280+
context: context
281+
})
282+
const stream = parser.import(stringToStream(JSON.stringify(example)))
283+
const output = []
284+
285+
stream.on('data', (triple) => {
286+
output.push(triple)
287+
})
288+
289+
return rdf.waitFor(stream).then(() => {
290+
assert.equal(output.length, 1)
291+
assert.equal(output[0].subject.termType, 'NamedNode')
292+
assert.equal(output[0].subject.value, 'http://example.org/subject')
293+
})
294+
})
295+
268296
it('should throw an error if JSON is invalid', () => {
269297
let parser = new JSONLDParser()
270298
let stream = parser.import(stringToStream('{'))

0 commit comments

Comments
 (0)