-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
62 lines (46 loc) · 1.16 KB
/
index.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
import { Readable } from 'readable-stream'
function buildQuadFilter (subject, predicate, object, graph) {
return function (quad) {
// TODO: implement RegExp support
if (subject && !quad.subject.equals(subject)) {
return false
}
if (predicate && !quad.predicate.equals(predicate)) {
return false
}
if (object && !quad.object.equals(object)) {
return false
}
if (graph && !quad.graph.equals(graph)) {
return false
}
return true
}
}
class FilterStream extends Readable {
constructor (input, subject, predicate, object, graph) {
super()
this._readableState.objectMode = true
const filter = typeof subject === 'function' ? subject : buildQuadFilter(subject, predicate, object, graph)
input.once('close', () => {
this.emit('close')
})
input.on('data', quad => {
if (filter(quad)) {
this.push(quad)
}
})
input.on('end', () => {
this.emit('end')
})
input.on('error', err => {
this.emit('error', err)
})
input.on('prefix', map => {
this.emit('prefix', map)
})
}
_read () {
}
}
export default FilterStream