-
Notifications
You must be signed in to change notification settings - Fork 3
/
resourcesToGraph.js
41 lines (30 loc) · 992 Bytes
/
resourcesToGraph.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
import rdf from 'rdf-ext'
import resource from './resource.js'
function resourcesToGraph (dataset, options = {}) {
const factory = options.factory || rdf
const input = factory.dataset()
for (const quad of dataset) {
input.add(quad)
}
const output = factory.dataset()
const resourceIRIs = [...input]
.reduce((iriSet, quad) => {
if (quad.subject.termType !== 'NamedNode') {
return iriSet
}
iriSet.add(quad.subject.value.split('#')[0])
return iriSet
}, new Set())
resourceIRIs.forEach(resourceIRI => {
const resourceNode = factory.namedNode(resourceIRI)
const resourceTriples = resource(input, resourceNode)
resourceTriples.forEach(triple => {
if (triple.subject.termType !== 'BlankNode') {
input.delete(triple)
}
})
output.addAll(resourceTriples.map(quad => factory.quad(quad.subject, quad.predicate, quad.object, resourceNode)))
})
return output
}
export default resourcesToGraph