test-ic-wallet-middleware-common
Version:
Ic middleware wallet common objects
35 lines (34 loc) • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AmountProvider = void 0;
class AmountProvider {
static stringToBigInt(value, decimal) {
if (!isNaN(value)) { // validate is string valid number
const parts = value.split(".");
const hole = parts[0];
const dec = parts[1] ?? "";
if (dec.length <= decimal) { // make sure that length of decimal part more that string input
let addZeros = "";
for (let index = 0; index < decimal - dec.length; index++) {
addZeros = "0" + addZeros;
}
return BigInt(hole + dec + addZeros);
}
}
return undefined;
}
static toBigInt(value, decimal) {
if (typeof value === typeof (0n)) {
return value;
}
else {
return this.stringToBigInt(value, decimal);
}
}
static bigIntToDisplay(value, decimal) {
const numb = Number(value);
const divisor = Math.pow(10, decimal);
return (numb / divisor).toString();
}
}
exports.AmountProvider = AmountProvider;