@signumjs/util
Version:
Useful utilities and tools for building Signum Network applications
227 lines • 6.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Amount = exports.AmountFormats = exports.FormatCommaDecimal = exports.FormatDotDecimal = void 0;
const constants_1 = require("./constants");
const chainValue_1 = require("./chainValue");
/**
* Amount formatting preset for dot decimal formatting 'Ꞩ 1,000,000.123456'
*
* @category value-objects
*/
exports.FormatDotDecimal = {
prefix: constants_1.CurrencySymbol + ' ',
decimalSeparator: '.',
groupSeparator: ',',
groupSize: 3,
secondaryGroupSize: 0,
fractionGroupSeparator: '',
fractionGroupSize: 0,
suffix: ''
};
/**
* Amount formatting preset for comma decimal formatting 'Ꞩ 1.000.000,123456'
*
* @category value-objects
*/
exports.FormatCommaDecimal = {
prefix: constants_1.CurrencySymbol + ' ',
decimalSeparator: ',',
groupSeparator: '.',
groupSize: 3,
secondaryGroupSize: 0,
fractionGroupSeparator: '',
fractionGroupSize: 0,
suffix: ''
};
/**
* Amount formatting presets, see {@link Amount.toString}
*
* @category value-objects
*/
exports.AmountFormats = {
/**
* 1,000,000.123456
*/
DotDecimal: exports.FormatDotDecimal,
/**
* 1.000.000,123456
*/
CommaDecimal: exports.FormatCommaDecimal
};
/**
* A Value Object to facilitate SIGNA and Planck conversions/calculations.
*
* This class is a convenient wrapper around {@link ChainValue} with `decimals = 8`
*
* Note: This class uses a big number representation (ES5 compatible) under the hood, so
* number limits and numeric calculations are much more precise than JS number type
*
* @category value-objects
*/
class Amount {
_value;
constructor(planck) {
this._value = new chainValue_1.ChainValue(8).setAtomic(planck);
}
/**
* @return The Signa Currency Symbol
* @deprecated Due to Multiverse feature it's not recommended to use this hard coded stuff.
*
*/
static CurrencySymbol() {
return constants_1.CurrencySymbol;
}
/**
* Same as `Amount.fromPlanck(0)` or `Amount.fromSigna(0)`
*/
static Zero() {
return new Amount(0);
}
/**
* Creates a Burst Value object from Planck
* @param planck The value in Planck
*/
static fromPlanck(planck) {
return new Amount(planck);
}
/**
* Creates a Value object from SIGNA
* @param signa The value in SIGNA
*/
static fromSigna(signa) {
const b = new Amount('0');
b.setSigna(typeof signa === 'number' ? signa.toString(10) : signa);
return b;
}
/**
* Leaky value getter
* @return the underlying value in its big number representation (immutable)
*/
getRaw() {
return this._value.getRaw();
}
/**
* @return Gets Planck representation
*/
getPlanck() {
return this._value.getAtomic();
}
/**
* Sets value as Planck, i.e. overwrites current hold value
* @param p The planck value
* @return This value object
*/
setPlanck(p) {
this._value.setAtomic(p);
return this;
}
/**
* Gets SIGNA representation
* @return value in SIGNA
*/
getSigna() {
return this._value.getCompound();
}
/**
* Sets value as SIGNA, i.e. overwrites current hold value
* @param b value in SIGNA
* @return This value object
*/
setSigna(b) {
this._value.setCompound(b);
return this;
}
/**
* Checks for equality
* @param amount The other value to be compared
* @return true if equal, otherwise false
*/
equals(amount) {
return this._value.equals(amount._value);
}
/**
* Checks for lesser or equality
* @param amount The other value to be compared
* @return true if less or equal, otherwise false
*/
lessOrEqual(amount) {
return this._value.lessOrEqual(amount._value);
}
/**
* Checks for lesser value
* @param amount The other value to be compared
* @return true if less, otherwise false
*/
less(amount) {
return this._value.less(amount._value);
}
/**
* Checks for greater or equality value
* @param amount The other value to be compared
* @return true if greater or equal, otherwise false
*/
greaterOrEqual(amount) {
return this._value.greaterOrEqual(amount._value);
}
/**
* Checks for greater value
* @param amount The other value to be compared
* @return true if greater, otherwise false
*/
greater(amount) {
return this._value.greater(amount._value);
}
/**
* Adds two values
* @param amount The other value to be added
* @return the _mutated_ Amount object
*/
add(amount) {
this._value.add(amount._value);
return this;
}
/**
* Subtracts from value
* @param amount The other value to be subtracted
* @return the _mutated_ Amount object
*/
subtract(amount) {
this._value.subtract(amount._value);
return this;
}
/**
* Multiplies with a _numeric_ value (not Amount)
* @param value A numeric value to be multiplied with
* @return the _mutated_ Amount object
*/
multiply(value) {
this._value.multiply(value);
return this;
}
/**
* Divides by a _numeric_ value (not Amount)
* @param value A numeric value to be divided by
* @return the _mutated_ Amount object
*/
divide(value) {
this._value.divide(value);
return this;
}
/**
* Gets a string representation in form `Ꞩ 100`
* @param format The format object, Default: {@link AmountFormats.DotDecimal}
* @return The formatted string
*/
toString(format = exports.AmountFormats.DotDecimal) {
return this._value.toFormat(format.prefix, format);
}
/**
* Clones/Copies the current Amount to a new object
* @return new Amount instance
*/
clone() {
return Amount.fromPlanck(this.getPlanck());
}
}
exports.Amount = Amount;
//# sourceMappingURL=amount.js.map