@signumjs/util
Version:
Useful utilities and tools for building Signum Network applications
135 lines • 3.74 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChainValue = exports.ChainValueFormats = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
bignumber_js_1.default.config({
EXPONENTIAL_AT: [-9, 20],
DECIMAL_PLACES: 8
});
const FormatDotDecimal = {
decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3,
secondaryGroupSize: 0,
fractionGroupSeparator: '',
fractionGroupSize: 0,
suffix: ''
};
const FormatCommaDecimal = {
decimalSeparator: ',',
groupSeparator: '.',
groupSize: 3,
secondaryGroupSize: 0,
fractionGroupSeparator: '',
fractionGroupSize: 0,
suffix: ''
};
exports.ChainValueFormats = {
DotDecimal: FormatDotDecimal,
CommaDecimal: FormatCommaDecimal
};
function assureValidValue(v) {
if (!(v && /^-?\d*(\.\d+)?$/.test(v))) {
throw new Error(`Invalid value: ${v}`);
}
}
class ChainValue {
_big;
_decimals;
constructor(decimals) {
if (decimals < 0 || decimals > 8) {
throw new Error('Decimals must be between 0 and 8');
}
this._big = new bignumber_js_1.default(0);
this._decimals = decimals;
}
static create(decimals) {
return new ChainValue(decimals);
}
getDecimals() {
return this._decimals;
}
getRaw() {
return this._big;
}
getAtomic() {
return this._big.dp(0).toString();
}
setAtomic(a) {
if (typeof (a) === 'number') {
this._big = new bignumber_js_1.default(Math.floor(a));
}
else {
assureValidValue(a);
this._big = new bignumber_js_1.default(a);
}
return this;
}
getCompound() {
return this._big.dividedBy(10 ** this._decimals).dp(this._decimals).toString();
}
setCompound(c) {
if (typeof (c) === 'string') {
assureValidValue(c);
}
this._big = new bignumber_js_1.default(c || 0).multipliedBy(10 ** this._decimals);
return this;
}
equals(value) {
return this._big.eq(value._big);
}
lessOrEqual(chainValue) {
return this._big.lte(chainValue._big);
}
less(chainValue) {
return this._big.lt(chainValue._big);
}
greaterOrEqual(chainValue) {
return this._big.gte(chainValue._big);
}
greater(chainValue) {
return this._big.gt(chainValue._big);
}
add(chainValue) {
this._big = this._big.plus(chainValue._big);
return this;
}
subtract(chainValue) {
this._big = this._big.minus(chainValue._big);
return this;
}
multiply(value) {
let v = value;
if (typeof (value) === 'string') {
assureValidValue(value);
v = parseFloat(value);
}
this._big = this._big.multipliedBy(v);
return this;
}
divide(value) {
let v = value;
if (typeof (value) === 'string') {
assureValidValue(value);
v = parseFloat(value);
}
if (v === 0) {
throw new Error('Division by zero');
}
this._big = this._big.div(v);
return this;
}
toFormat(prefix, format = exports.ChainValueFormats.DotDecimal) {
return this._big.dividedBy(10 ** this._decimals).toFormat({ ...format, prefix });
}
clone() {
const newValue = new ChainValue(this._decimals);
newValue._big = this._big;
return newValue;
}
}
exports.ChainValue = ChainValue;
//# sourceMappingURL=chainValue.js.map