1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.doKeysMatch = exports.matchingNibbleLength = exports.nibblesCompare = exports.nibblesToBuffer = exports.bufferToNibbles = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | function bufferToNibbles(key) {
|
10 | const bkey = Buffer.from(key);
|
11 | const nibbles = [];
|
12 | for (let i = 0; i < bkey.length; i++) {
|
13 | let q = i * 2;
|
14 | nibbles[q] = bkey[i] >> 4;
|
15 | ++q;
|
16 | nibbles[q] = bkey[i] % 16;
|
17 | }
|
18 | return nibbles;
|
19 | }
|
20 | exports.bufferToNibbles = bufferToNibbles;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function nibblesToBuffer(arr) {
|
27 | const buf = Buffer.alloc(arr.length / 2);
|
28 | for (let i = 0; i < buf.length; i++) {
|
29 | let q = i * 2;
|
30 | buf[i] = (arr[q] << 4) + arr[++q];
|
31 | }
|
32 | return buf;
|
33 | }
|
34 | exports.nibblesToBuffer = nibblesToBuffer;
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function nibblesCompare(n1, n2) {
|
44 | const cmpLength = Math.min(n1.length, n2.length);
|
45 | let res = 0;
|
46 | for (let i = 0; i < cmpLength; i++) {
|
47 | if (n1[i] < n2[i]) {
|
48 | res = -1;
|
49 | break;
|
50 | }
|
51 | else if (n1[i] > n2[i]) {
|
52 | res = 1;
|
53 | break;
|
54 | }
|
55 | }
|
56 | if (res === 0) {
|
57 | if (n1.length < n2.length) {
|
58 | res = -1;
|
59 | }
|
60 | else if (n1.length > n2.length) {
|
61 | res = 1;
|
62 | }
|
63 | }
|
64 | return res;
|
65 | }
|
66 | exports.nibblesCompare = nibblesCompare;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | function matchingNibbleLength(nib1, nib2) {
|
74 | let i = 0;
|
75 | while (nib1[i] === nib2[i] && nib1.length > i) {
|
76 | i++;
|
77 | }
|
78 | return i;
|
79 | }
|
80 | exports.matchingNibbleLength = matchingNibbleLength;
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function doKeysMatch(keyA, keyB) {
|
87 | const length = matchingNibbleLength(keyA, keyB);
|
88 | return length === keyA.length && length === keyB.length;
|
89 | }
|
90 | exports.doKeysMatch = doKeysMatch;
|
91 |
|
\ | No newline at end of file |