@thorwallet/xchain-util
Version:
Helper utilities for XChain clients
92 lines • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fixedBN = exports.formatBNCurrency = exports.SymbolPosition = exports.formatBN = exports.validBNOrZero = exports.bnOrZero = exports.isValidBN = void 0;
const tslib_1 = require("tslib");
const bignumber_js_1 = tslib_1.__importDefault(require("bignumber.js"));
/**
* Shortcut to create a BigNumber
*
* @param {string | number | BigNumber.Instance} value
* @returns {BigNumber} The BigNumber interface from the given value.
*/
const bn = (value) => new bignumber_js_1.default(value);
/**
* Helper to check whether a BigNumber is valid or not
*
* @param {BigNumber} value
* @returns {boolean} `true` or `false`.
* */
const isValidBN = (value) => !value.isNaN();
exports.isValidBN = isValidBN;
/**
* Helper to create a big number from string or number
* If it fails to create a big number, a big number with value 0 will be returned instead
*
* @param {string|number|undefined} value
* @returns {BigNumber} The BigNumber interface from the given value. If invalid one is provided, will return `0`.
* */
const bnOrZero = (value) => {
const b = value ? bn(value) : bn(0);
return exports.isValidBN(b) ? b : bn(0);
};
exports.bnOrZero = bnOrZero;
/**
* Helper to validate a possible BigNumber
* If the given valie is invalid or undefined, 0 is returned as a BigNumber
*
* @param {BigNumber|undefined} value
* @returns {boolean} `true` or `false`.
*/
const validBNOrZero = (value) => (value && exports.isValidBN(value) ? value : bn(0));
exports.validBNOrZero = validBNOrZero;
/**
* Format a BaseNumber to a string depending on given decimal places
*
* @param {BigNumber} value
* @param {number} decimal The decimal place. (optional)
* @returns {string} The formatted string from the given BigNumber and decimal place.
* */
const formatBN = (value, decimal = 2) => value.toFormat(decimal);
exports.formatBN = formatBN;
/**
* The enumuration for symbol position.
* `before` or `after`
*/
var SymbolPosition;
(function (SymbolPosition) {
SymbolPosition["BEFORE"] = "before";
SymbolPosition["AFTER"] = "after";
})(SymbolPosition = exports.SymbolPosition || (exports.SymbolPosition = {}));
/**
* Formats a big number value by prefixing it with `$`
*
* @param {BigNumber} n
* @param {number} decimalPlaces The decimal place. (optional)
* @param {string} symbol The currency symbol. (optional)
* @param {position} position The symbol position. (optional)
* @returns {string} The formatted string from the given BigNumber, decimal places, symbol and position.
*/
const formatBNCurrency = (n, decimalPlaces = 2, symbol = '$', position = SymbolPosition.BEFORE) => {
const value = exports.formatBN(n, decimalPlaces);
if (position === SymbolPosition.BEFORE) {
return `${symbol}${value}`;
}
return `${value}${symbol}`;
};
exports.formatBNCurrency = formatBNCurrency;
/**
* Helper to get a fixed `BigNumber`
* Returns zero `BigNumber` if `value` is invalid
*
* @param {number|string|BigNumber|undefined} value
* @param {number} decimalPlaces The decimal place. (optional)
* @returns {BigNumber} The BigNumber interface from the given value and decimal.
* */
const fixedBN = (value, decimalPlaces = 2) => {
const n = bn(value || 0);
const fixedBN = exports.isValidBN(n) ? n.toFixed(decimalPlaces) : bn(0).toFixed(decimalPlaces);
return bn(fixedBN);
};
exports.fixedBN = fixedBN;
exports.default = bn;
//# sourceMappingURL=bn.js.map