UNPKG

iota-ternary

Version:

Fast utility functions for conversion between trytes, trits and bytes

42 lines (41 loc) 854 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function numberToTrits(number) { if (number < 0) { return convertNegativeValue(-number); } else { return convertPositiveValue(number); } } exports.numberToTrits = numberToTrits; function convertNegativeValue(v) { const trits = []; while (v) { var r = v % 3; v = Math.floor(v / 3); if (r > 1) { trits.push(1); v++; } else { trits.push(r && -r); } } return trits; } function convertPositiveValue(v) { const trits = []; while (v) { var r = v % 3; v = Math.floor(v / 3); if (r > 1) { trits.push(-1); v++; } else { trits.push(r); } } return trits; }