/
index.js
60 lines (47 loc) · 1.54 KB
/
index.js
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
const otModule = require('bindings')('ot-wrapper-node');
function arrayToLittleEndianBytes(inputArray) {
const bytes = [];
for (let i = 0; i < inputArray.length; i += 8) {
let byte = 0;
for (let j = 0; j < 8 && i + j < inputArray.length; j++) {
const choice = inputArray[i + j];
if (choice === 1 || choice === true) {
byte |= (1 << j);
}
}
bytes.push(byte);
}
return bytes;
}
/**
* @param {ArrayLike<number> | ArrayLike<boolean>} arr A list of choices as numbers or booleans.
* For a value at index i:
* - if the ith value is 0 or false, then the 1st block in the ith pair is selected;
* - if the ith value is 1 or true, then the 2nd block in the ith pair is selected.
* @returns {[Uint8Array, number]} encoded choices as Uint8Array and the number of them
*/
function arrayToChoiceBits(arr) {
if (!Array.isArray(arr)) {
throw new Error('expected array');
}
let allValidNumbers = true;
let allBooleans = true;
for (const value of arr) {
if (typeof value !== 'boolean') {
allBooleans = false;
}
if (value !== 0 && value !== 1) {
allValidNumbers = false;
}
// If both flags are false, we can break early.
if (!allBooleans && !allValidNumbers) {
break;
}
}
if (!allValidNumbers && !allBooleans) {
throw new Error('expected array of numbers (0 and 1) or booleans')
}
return [Uint8Array.from(arrayToLittleEndianBytes(Array.from(arr))), arr.length];
}
otModule.arrayToChoiceBits = arrayToChoiceBits;
module.exports = otModule; // reexport;