File tree 2 files changed +37
-1
lines changed 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ class ParserStream extends Readable {
13
13
options = options || { }
14
14
15
15
this . baseIRI = options . baseIRI || ''
16
+ this . context = options . context
16
17
this . factory = options . factory || rdf
17
18
this . blankNodes = { }
18
19
@@ -62,7 +63,14 @@ class ParserStream extends Readable {
62
63
} )
63
64
}
64
65
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 )
66
74
} ) . then ( ( rawGraph ) => {
67
75
Object . keys ( rawGraph ) . forEach ( ( graphIri ) => {
68
76
const graph = graphIri !== '@default' ? this . factory . namedNode ( graphIri ) : null
Original file line number Diff line number Diff line change @@ -265,6 +265,34 @@ describe('rdf-parser-jsond', () => {
265
265
} )
266
266
} )
267
267
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
+
268
296
it ( 'should throw an error if JSON is invalid' , ( ) => {
269
297
let parser = new JSONLDParser ( )
270
298
let stream = parser . import ( stringToStream ( '{' ) )
You can’t perform that action at this time.
0 commit comments