Files: 1fc94adc199518c16723617727d7d19d75724d5b / treeNode.js
1423 bytesRaw
1 | const Uint1Array = require('uint1array') |
2 | const EMPTY_STATE_ROOT = Buffer.from('4cf812be9be2c6008325050f43d06676a08612c7', 'hex') |
3 | |
4 | function toTypedArray (array) { |
5 | return new Uint1Array(new Uint8Array(array).buffer) |
6 | } |
7 | |
8 | // helper functions for nodes |
9 | const EXTENSION = exports.EXTENSION = 0 |
10 | const LBRANCH = exports.LBRANCH = 1 |
11 | const RBRANCH = exports.RBRANCH = 2 |
12 | const VALUE = exports.VALUE = 3 |
13 | |
14 | exports.setBranch = function (node, branch) { |
15 | node['/'][LBRANCH] = branch[0] |
16 | node['/'][RBRANCH] = branch[1] |
17 | } |
18 | |
19 | exports.getBranch = function (node) { |
20 | return node['/'].slice(LBRANCH, LBRANCH + 2) |
21 | } |
22 | |
23 | exports.getValue = function (node) { |
24 | return node['/'][VALUE] |
25 | } |
26 | |
27 | exports.deleteValue = function (node) { |
28 | delete node['/'][VALUE] |
29 | } |
30 | |
31 | exports.getExtension = function (node) { |
32 | if (node['/'][EXTENSION]) { |
33 | const len = node['/'][EXTENSION][0] |
34 | const extension = toTypedArray(node['/'][EXTENSION][1]) |
35 | return extension.subarray(0, len) |
36 | } else { |
37 | return toTypedArray([]) |
38 | } |
39 | } |
40 | |
41 | exports.setExtension = function (node, ex) { |
42 | if (ex && ex.length) { |
43 | node['/'][EXTENSION] = [ex.length, Buffer.from(ex.buffer)] |
44 | } else { |
45 | delete node['/'][EXTENSION] |
46 | } |
47 | } |
48 | |
49 | exports.setValue = function (node, val) { |
50 | node['/'][VALUE] = val |
51 | } |
52 | |
53 | exports.isEmpty = function (node) { |
54 | if (Buffer.isBuffer(node['/'])) { |
55 | return !Buffer.compare(node['/'], EMPTY_STATE_ROOT) |
56 | } else { |
57 | return node['/'].every(el => !el) |
58 | } |
59 | } |
60 |
Built with git-ssb-web