-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (109 loc) · 3.2 KB
/
Copy pathindex.js
File metadata and controls
113 lines (109 loc) · 3.2 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class KeyTable {
constructor(data) {
this.data = data || {}
}
g() {
return super.findme
}
set(key, value) {
if (typeof key === 'object') {
for (let k in key) {
this.set(k, key[k])
}
return key
}
const set = (o, sub) => {
if (sub.length === 0) {
return o[sub] = value
}
const c = sub[0]
if (o.hasOwnProperty(c)) {
return set(o[c], sub.substr(1))
}
// Find key matching first character
let keys = Object.keys(o)
for (let i = 0; i < keys.length; i++) {
const sub2 = keys[i]
const match = KeyTable.matchStart(sub, sub2)
if (match) {
if (match < sub2.length) {
const next = {
[sub2.substr(match)]: o[sub2],
}
o[sub2.substr(0, match)] = next
delete o[sub2]
return set(next, sub.substr(match))
} else {
return set(o[sub2], sub.substr(match))
}
}
}
o[sub] = {
'': value
}
return value
}
return set(this.data, `${key}`)
}
get(key) {
const get = (o, sub) => {
console.log(sub)
if (sub.length === 0) {
console.log(o)
return o['']
}
const c = sub[0]
if (o.hasOwnProperty(c)) {
return get(o[c], sub.substr(1))
}
// Find key matching first character
let keys = Object.keys(o)
for (let i = 0; i < keys.length; i++) {
const sub2 = keys[i]
let match = KeyTable.matchStart(sub, sub2)
if (match) {
return get(o[sub2], sub.substr(match))
}
}
return undefined
}
return get(this.data, `${key}`)
}
get length() {
let count = 0
for (let key of this.getKeys()) {
count++
}
return count
}
get keys() {
return this.getKeys()
}
* getKeys(prefix) {
function * walk(o, sub, key) {
const keys = Object.keys(o)
for (let i = 0; i < keys.length; i++) {
const sub2 = keys[i]
if (sub2 === '' && sub === '') {
yield key
} else if (sub === '') {
yield * walk(o[sub2], '', key + sub2)
} else {
const match = KeyTable.matchStart(sub, sub2)
if (match) {
yield * walk(o[sub2], sub.substr(match), key + sub2)
}
}
}
}
yield * walk(this.data, prefix || '', '')
}
}
KeyTable.matchStart = (s1, s2) => {
const l = Math.max(s1.length, s2.length)
for (let i = 0; i < l; i++) {
if (s1[i] !== s2[i]) return i
}
return l
}
module.exports = KeyTable