Skip to content

Commit f37da10

Browse files
committed
Function term rewrite with 'termType'
Function parse rewrite based on function term
1 parent 10cad3b commit f37da10

File tree

3 files changed

+2169
-32
lines changed

3 files changed

+2169
-32
lines changed

lib/ParserStream.js

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ const concat = require('concat-stream')
22
const jsonld = require('jsonld')
33
const rdf = require('rdf-data-model')
44
const Readable = require('readable-stream')
5+
const dataModel = require('@rdfjs/data-model')
6+
const terms = require('rdf-terms')
57

68
class ParserStream extends Readable {
79
constructor (input, options) {
810
super({
911
objectMode: true,
10-
read: () => {}
12+
read: () => {
13+
}
1114
})
1215

1316
options = options || {}
1417

1518
this.baseIRI = options.baseIRI || ''
1619
this.context = options.context
17-
this.factory = options.factory || rdf
1820
this.blankNodes = {}
1921

2022
const concatStream = concat({encoding: 'string'}, (data) => {
@@ -30,59 +32,49 @@ class ParserStream extends Readable {
3032
this.emit('error', err)
3133
})
3234
})
33-
3435
input.pipe(concatStream)
3536

3637
input.on('error', (err) => {
3738
this.emit('error', err)
3839
})
3940
}
4041

41-
term (options) {
42-
if (options.type === 'IRI') {
43-
return this.factory.namedNode(options.value)
44-
}
45-
46-
if (options.type === 'blank node') {
47-
if (!(options.value in this.blankNodes)) {
48-
this.blankNodes[options.value] = this.factory.blankNode()
49-
}
50-
51-
return this.blankNodes[options.value]
42+
term (term) {
43+
switch (term.termType) {
44+
case 'NamedNode':
45+
return dataModel.namedNode(term.value)
46+
case 'BlankNode':
47+
return dataModel.blankNode(term.value.substr(2)) // Remove the '_:' prefix.
48+
case 'Literal':
49+
return dataModel.literal(term.value, term.language || term.datatype)
50+
case 'DefaultGraph':
51+
return dataModel.defaultGraph()
5252
}
53-
54-
return this.factory.literal(options.value, options.language || options.datatype)
5553
}
5654

5755
parse (data) {
5856
return ParserStream.toJson(data).then((json) => {
59-
// forward context as prefixes if available
57+
// forward context as prefixes if available
6058
if ('@context' in json) {
6159
Object.keys(json['@context']).forEach((prefix) => {
62-
this.emit('prefix', prefix, this.factory.namedNode(json['@context'][prefix]))
60+
this.emit('prefix', prefix, dataModel.namedNode(json['@context'][prefix]))
6361
})
6462
}
6563

6664
const toRdfOptions = {base: this.baseIRI}
6765

68-
// use context from options if given
66+
// use context from options if given
6967
if (this.context) {
7068
toRdfOptions.expandContext = this.context
7169
}
7270

7371
return jsonld.promises().toRDF(json, toRdfOptions)
7472
}).then((rawGraph) => {
75-
Object.keys(rawGraph).forEach((graphIri) => {
76-
const graph = graphIri !== '@default' ? this.factory.namedNode(graphIri) : null
77-
78-
rawGraph[graphIri].forEach((triple) => {
79-
this.push(this.factory.quad(
80-
this.term(triple.subject),
81-
this.term(triple.predicate),
82-
this.term(triple.object),
83-
graph))
84-
})
85-
})
73+
for (let index in rawGraph) {
74+
this.push(terms.mapTerms(rawGraph[index], this.term))
75+
}
76+
}).catch((error) => {
77+
this.emit('error', error)
8678
})
8779
}
8880

0 commit comments

Comments
 (0)