-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathResultParser.js
71 lines (55 loc) · 1.64 KB
/
ResultParser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import JsonParser from '@bergos/jsonparse'
import { Transform } from 'readable-stream'
/**
* A Transform stream that parses JSON SPARQL results and emits one object per row with the variable names as keys and
* RDF/JS terms as values.
*/
class ResultParser extends Transform {
/**
* @param {Object} options
* @param {DataFactory} options.factory RDF/JS DataFactory used to create the quads and terms
*/
constructor ({ factory }) {
super({
readableObjectMode: true
})
this.factory = factory
this.jsonParser = new JsonParser()
this.jsonParser.onError = err => this.destroy(err)
this.jsonParser.onValue = value => this.onValue(value)
}
_write (chunk, encoding, callback) {
this.jsonParser.write(chunk)
callback()
}
onValue (raw) {
if (this.jsonParser.stack.length !== 3) {
return
}
if (this.jsonParser.stack[1].key !== 'results' || this.jsonParser.stack[2].key !== 'bindings') {
return
}
if (Object.keys(raw).length === 0) {
return
}
const row = {}
for (const [key, value] of Object.entries(raw)) {
row[key] = this.valueToTerm(value)
}
this.push(row)
}
valueToTerm (value) {
if (value.type === 'uri') {
return this.factory.namedNode(value.value)
}
if (value.type === 'bnode') {
return this.factory.blankNode(value.value)
}
if (value.type === 'literal' || value.type === 'typed-literal') {
const datatype = (value.datatype && this.factory.namedNode(value.datatype))
return this.factory.literal(value.value, datatype || value['xml:lang'])
}
return null
}
}
export default ResultParser