UNPKG

@logosnetwork/logos-rpc-client

Version:

Promise-based client for interacting and building services on top of the Logos network.

79 lines 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var bignumber_js_1 = require("bignumber.js"); var Converter = { unit: function (input, input_unit, output_unit) { var value = new bignumber_js_1.default(input.toString()); // Step 1: to reason switch (input_unit) { case 'reason': value = value; break; case 'LOGOS': value = value.shiftedBy(30); break; default: var input_1 = parseInt(input_unit.toString()); if (!isNaN(input_1)) { value = value.shiftedBy(input_1); } else { throw new Error("Unkown input unit " + input_unit); } } // Step 2: to output var val = null; var matches = null; switch (output_unit) { case 'reason': return value.toFixed(0); case 'LOGOS': val = value.shiftedBy(-30).toFixed(15, 1); matches = val.match(/(\.\d+?)0+$/); if (matches) { val = val.replace(matches[0], matches[1]); if (val.endsWith('.0')) { return val.replace('.0', ''); } else { return val; } } else { return val; } default: var output = parseInt(output_unit.toString()); if (!isNaN(output) && output !== 0) { var val_1 = value.shiftedBy(-output).toFixed(15, 1); var matches_1 = val_1.match(/(\.\d+?)0+$/); if (matches_1) { val_1 = val_1.replace(matches_1[0], matches_1[1]); if (val_1.endsWith('.0')) { return val_1.replace('.0', ''); } else { return val_1; } } else { return val_1; } } else if (output === 0) { return value.toFixed(0); } else { throw new Error("Unknown output unit " + output_unit); } } }, minus: function (base, minus) { new bignumber_js_1.default(base).minus(new bignumber_js_1.default(minus)).toString(); }, plus: function (base, plus) { new bignumber_js_1.default(base).plus(new bignumber_js_1.default(plus)).toString(); } }; exports.default = Converter; //# sourceMappingURL=converter.js.map