-
Notifications
You must be signed in to change notification settings - Fork 2
/
TermMap.js
73 lines (56 loc) · 1.14 KB
/
TermMap.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
72
73
import toNT from '@rdfjs/to-ntriples'
class TermMap {
constructor (entries) {
this.index = new Map()
if (entries) {
for (const [term, value] of entries) {
this.set(term, value)
}
}
}
get size () {
return this.index.size
}
clear () {
this.index.clear()
}
delete (term) {
return this.index.delete(toNT(term))
}
* entries () {
for (const [, { term, value }] of this.index) {
yield [term, value]
}
}
forEach (callback, thisArg) {
for (const entry of this.entries()) {
callback.call(thisArg, entry[1], entry[0], this)
}
}
get (term) {
const item = this.index.get(toNT(term))
return item && item.value
}
has (term) {
return this.index.has(toNT(term))
}
* keys () {
for (const [, { term }] of this.index) {
yield term
}
}
set (term, value) {
const key = toNT(term)
this.index.set(key, { term, value })
return this
}
* values () {
for (const [, { value }] of this.index) {
yield value
}
}
[Symbol.iterator] () {
return this.entries()[Symbol.iterator]()
}
}
export default TermMap