iota-ternary
Version:
Fast utility functions for conversion between trytes, trits and bytes
48 lines (47 loc) • 956 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const TRYTES_TO_TRITS = [
0, 0, 0,
1, 0, 0,
-1, 1, 0,
0, 1, 0,
1, 1, 0,
-1, -1, 1,
0, -1, 1,
1, -1, 1,
-1, 0, 1,
0, 0, 1,
1, 0, 1,
-1, 1, 1,
0, 1, 1,
1, 1, 1,
-1, -1, -1,
0, -1, -1,
1, -1, -1,
-1, 0, -1,
0, 0, -1,
1, 0, -1,
-1, 1, -1,
0, 1, -1,
1, 1, -1,
-1, -1, 0,
0, -1, 0,
1, -1, 0,
-1, 0, 0
];
function trytesToTrits(trytes) {
var i, j, k;
const size = trytes.length;
const trits = new Array(size * 3);
for (i = 0, j = 0; i < size; i += 1, j += 3) {
k = (trytes.charCodeAt(i) - 64) * 3;
if (k < 0) {
k = 0;
}
trits[j + 0] = TRYTES_TO_TRITS[k + 0];
trits[j + 1] = TRYTES_TO_TRITS[k + 1];
trits[j + 2] = TRYTES_TO_TRITS[k + 2];
}
return trits;
}
exports.trytesToTrits = trytesToTrits;