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