-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefixMap.js
69 lines (53 loc) · 1.3 KB
/
PrefixMap.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
import { finished } from 'readable-stream'
class PrefixMap extends Map {
constructor (prefixes = [], { factory }) {
super(prefixes)
this.factory = factory
}
resolve (term) {
if (term.value.includes('://')) {
return null
}
const [prefix, path] = term.value.split(':', 2)
if (path === undefined) {
return null
}
if (!this.has(prefix)) {
return null
}
return this.factory.namedNode(`${this.get(prefix).value}${path}`)
}
shrink (term) {
if (!term) {
return null
}
const [pair] = [...this]
.filter(([, namespace]) => term.value.startsWith(namespace.value))
.sort((a, b) => b[1].value.length - a[1].value.length)
if (pair === undefined) {
return null
}
return this.factory.namedNode(`${pair[0]}:${term.value.slice(pair[1].value.length)}`)
}
import (stream) {
stream.on('prefix', (prefix, namespace) => {
this.set(prefix, namespace)
})
return new Promise((resolve, reject) => {
finished(stream, err => {
if (err) {
reject(err)
} else {
resolve(this)
}
})
})
}
export (stream) {
for (const [prefix, namespace] of this) {
stream.emit('prefix', prefix, namespace)
}
return this
}
}
export default PrefixMap