prefix-hash-tree 0.0.6 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -17
- package/lib/pht-node.d.ts +1 -10
- package/lib/pht-node.js +6 -32
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
const sodium = require('sodium-universal')
|
|
2
2
|
const b4a = require('b4a')
|
|
3
|
-
const {
|
|
4
|
-
createPHTNode,
|
|
5
|
-
isLeaf,
|
|
6
|
-
get,
|
|
7
|
-
getAll,
|
|
8
|
-
set,
|
|
9
|
-
label,
|
|
10
|
-
size,
|
|
11
|
-
setPointers,
|
|
12
|
-
pointerLeft,
|
|
13
|
-
pointerRight
|
|
14
|
-
} = require('./lib/pht-node.js')
|
|
3
|
+
const { createPHTNode, isLeaf, get, getAll, set, label, size } = require('./lib/pht-node.js')
|
|
15
4
|
|
|
16
5
|
class PrefixHashTree {
|
|
17
6
|
constructor(opts = {}) {
|
|
@@ -132,13 +121,9 @@ class PrefixHashTree {
|
|
|
132
121
|
const child0 = createPHTNode({ label: `${label(leaf)}0` })
|
|
133
122
|
const child1 = createPHTNode({ label: `${label(leaf)}1` })
|
|
134
123
|
|
|
135
|
-
setPointers(child0, pointerLeft(leaf), label(child1))
|
|
136
|
-
setPointers(child1, label(child0), pointerRight(leaf))
|
|
137
|
-
|
|
138
124
|
const parent = createPHTNode({
|
|
139
125
|
label: label(leaf),
|
|
140
|
-
|
|
141
|
-
child1: label(child1)
|
|
126
|
+
isLeaf: false
|
|
142
127
|
})
|
|
143
128
|
|
|
144
129
|
if (depth === lcp.length) {
|
package/lib/pht-node.d.ts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
declare module 'prefix-hash-tree' {
|
|
2
2
|
export interface PHTNode {
|
|
3
3
|
label: string
|
|
4
|
-
|
|
5
|
-
child1: string | null
|
|
6
|
-
lPtr: string | null
|
|
7
|
-
rPtr: string | null
|
|
4
|
+
isLeaf: boolean
|
|
8
5
|
keys: Record<string, any>
|
|
9
6
|
}
|
|
10
7
|
|
|
@@ -21,10 +18,4 @@ declare module 'prefix-hash-tree' {
|
|
|
21
18
|
export function label(node: PHTNode): string
|
|
22
19
|
|
|
23
20
|
export function size(node: PHTNode): number
|
|
24
|
-
|
|
25
|
-
export function setPointers(node: PHTNode, left: string | null, right: string | null): void
|
|
26
|
-
|
|
27
|
-
export function pointerLeft(node: PHTNode): string | null
|
|
28
|
-
|
|
29
|
-
export function pointerRight(node: PHTNode): string | null
|
|
30
21
|
}
|
package/lib/pht-node.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
function createPHTNode({ label,
|
|
1
|
+
function createPHTNode({ label, isLeaf, keys } = {}) {
|
|
2
2
|
return {
|
|
3
3
|
label: label || '',
|
|
4
|
-
|
|
5
|
-
child1: child1 || null,
|
|
6
|
-
lPtr: lPtr || null,
|
|
7
|
-
rPtr: rPtr || null,
|
|
4
|
+
isLeaf: isLeaf !== undefined ? isLeaf : true,
|
|
8
5
|
keys: keys || {}
|
|
9
6
|
}
|
|
10
7
|
}
|
|
@@ -12,7 +9,7 @@ function createPHTNode({ label, child0, child1, lPtr, rPtr, keys } = {}) {
|
|
|
12
9
|
function isNode(obj) {
|
|
13
10
|
if (typeof obj !== 'object' || obj === null) return false
|
|
14
11
|
|
|
15
|
-
const validKeys = new Set(['label', '
|
|
12
|
+
const validKeys = new Set(['label', 'isLeaf', 'keys'])
|
|
16
13
|
|
|
17
14
|
const keys = Object.keys(obj)
|
|
18
15
|
|
|
@@ -21,10 +18,7 @@ function isNode(obj) {
|
|
|
21
18
|
|
|
22
19
|
return (
|
|
23
20
|
isBinaryString(obj.label) &&
|
|
24
|
-
|
|
25
|
-
isBinaryStringOrNull(obj.child1) &&
|
|
26
|
-
isBinaryStringOrNull(obj.lPtr) &&
|
|
27
|
-
isBinaryStringOrNull(obj.rPtr) &&
|
|
21
|
+
typeof obj.isLeaf === 'boolean' &&
|
|
28
22
|
typeof obj.keys === 'object' &&
|
|
29
23
|
obj.keys !== null &&
|
|
30
24
|
!Array.isArray(obj.keys)
|
|
@@ -41,12 +35,8 @@ function isBinaryString(s) {
|
|
|
41
35
|
return true
|
|
42
36
|
}
|
|
43
37
|
|
|
44
|
-
function isBinaryStringOrNull(v) {
|
|
45
|
-
return v === null || isBinaryString(v)
|
|
46
|
-
}
|
|
47
|
-
|
|
48
38
|
function isLeaf(node) {
|
|
49
|
-
return node.
|
|
39
|
+
return node.isLeaf
|
|
50
40
|
}
|
|
51
41
|
|
|
52
42
|
function get(node, key) {
|
|
@@ -69,19 +59,6 @@ function size(node) {
|
|
|
69
59
|
return Object.keys(node.keys).length
|
|
70
60
|
}
|
|
71
61
|
|
|
72
|
-
function setPointers(node, left, right) {
|
|
73
|
-
node.lPtr = left
|
|
74
|
-
node.rPtr = right
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function pointerLeft(node) {
|
|
78
|
-
return node.lPtr
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function pointerRight(node) {
|
|
82
|
-
return node.rPtr
|
|
83
|
-
}
|
|
84
|
-
|
|
85
62
|
module.exports = {
|
|
86
63
|
createPHTNode,
|
|
87
64
|
isNode,
|
|
@@ -90,8 +67,5 @@ module.exports = {
|
|
|
90
67
|
getAll,
|
|
91
68
|
set,
|
|
92
69
|
label,
|
|
93
|
-
size
|
|
94
|
-
setPointers,
|
|
95
|
-
pointerLeft,
|
|
96
|
-
pointerRight
|
|
70
|
+
size
|
|
97
71
|
}
|