@fruitsjs/util
Version:
Useful utilities and tools for building Fruits Eco-Blockchain applications
24 lines • 1.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertDecStringToHexString = void 0;
require("./internal/padStartPolyfill");
const bignumber_js_1 = require("bignumber.js");
const twosComplementBinary_1 = require("./internal/twosComplementBinary");
const convertDecStringToHexString = (decimal, padding = 2) => {
let bn = typeof decimal === 'string' ? new bignumber_js_1.default(decimal) : decimal;
if (bn.isNaN()) {
throw new Error(`Invalid decimal argument: [${decimal}] - Expected a valid decimal value`);
}
if (padding < 0) {
throw new Error(`Invalid padding argument: [${padding}] - Expected a positive value`);
}
const isNegative = bn.lt(0);
if (isNegative) {
bn = (0, twosComplementBinary_1.twosComplementBinary)(bn);
}
const hex = bn.toString(16);
const padSize = Math.ceil(hex.length / padding);
return hex.padStart(padSize * padding, isNegative ? 'f' : '0');
};
exports.convertDecStringToHexString = convertDecStringToHexString;
//# sourceMappingURL=convertDecStringToHexString.js.map