hive-keychain-commons
Version:
Platform-agnostic functions used in Hive Keychain mobile and extensions
161 lines (160 loc) • 5.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FormatUtils = void 0;
const moment_1 = __importDefault(require("moment"));
const __1 = require("..");
const withCommas = (nb, decimals = 3, removeTrailingZeros = false) => {
const currency = nb.split(' ')[1];
const value = parseFloat(nb).toFixed(decimals);
const parts = value.split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
let finalNumber = parts.join('.');
if (removeTrailingZeros) {
finalNumber = finalNumber.replace(/^([\d,]+)$|^([\d,]+)\.0*$|^([\d,]+\.[0-9]*?)0*$/, '$1$2$3');
}
if (currency) {
finalNumber = finalNumber + ' ' + currency;
}
return finalNumber;
};
const toHP = (vests, props) => props
? (parseFloat(vests) * parseFloat(props.total_vesting_fund_hive + '')) /
parseFloat(props.total_vesting_shares + '')
: 0;
const toFormattedHP = (vests, props) => {
return `${toHP(vests.toString(), props).toFixed(3)} HP`;
};
const fromHP = (hp, props) => (parseFloat(hp) / parseFloat(props.total_vesting_fund_hive + '')) *
parseFloat(props.total_vesting_shares + '');
const formatCurrencyValue = (value, digits = 3, removeTrailingZeros = false) => {
if (value === undefined || value === null) {
return '...';
}
return withCommas(value.toString().replace('HBD', '').replace('HIVE', '').trim(), digits, removeTrailingZeros);
};
const nFormatter = (num, digits) => {
const si = [
{
value: 1,
symbol: '',
},
{
value: 1e3,
symbol: 'k',
},
{
value: 1e6,
symbol: 'M',
},
{
value: 1e9,
symbol: 'G',
},
{
value: 1e12,
symbol: 'T',
},
{
value: 1e15,
symbol: 'P',
},
{
value: 1e18,
symbol: 'E',
},
];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
let i;
for (i = si.length - 1; i > 0; i--) {
if (num >= si[i].value) {
break;
}
}
return (num / si[i].value).toFixed(digits).replace(rx, '$1') + si[i].symbol;
};
const hasMoreThanXDecimal = (nb, decimal) => {
const splitedNumber = nb.toString().split('.');
return splitedNumber.length > 1 ? splitedNumber[1].length > decimal : false;
};
const toNumber = (value) => {
return parseFloat(value.toString().split(' ')[0].trim());
};
const getSymbol = (nai) => {
if (nai === '@@000000013')
return 'HBD';
if (nai === '@@000000021')
return 'HIVE';
if (nai === '@@000000037')
return 'HP';
};
const fromNaiAndSymbol = (obj) => {
return `${(obj.amount / 1000).toFixed(obj.precision)} ${exports.FormatUtils.getSymbol(obj.nai)}`;
};
const getAmountFromNai = (obj) => {
const res = fromNaiAndSymbol(obj);
return __1.Asset.fromString(res).amount;
};
const removeHtmlTags = (str) => {
return str.replace(/<(?:.|\n)*?>/gm, '');
};
const getValFromString = (str) => {
return parseFloat(str.split(' ')[0]);
};
const trimUselessZero = (nb, precision) => {
var _a;
const numberWithPrecision = nb.toFixed(precision);
const n = parseFloat(numberWithPrecision).toString();
if (n.split('.').length > 0 && ((_a = n.split('.')[1]) === null || _a === void 0 ? void 0 : _a.length) > 3)
return exports.FormatUtils.withCommas(n);
else
return exports.FormatUtils.withCommas(parseFloat(n).toFixed(3));
};
const getUSDFromVests = (vestAmount, globalProperties, currencyPrices) => {
return (exports.FormatUtils.toHP(vestAmount.toString(), globalProperties.globals) *
currencyPrices.hive.usd).toFixed(2);
};
const getOrdinalLabelTranslation = (activeRank) => {
const result = parseFloat(activeRank) % 10;
switch (result) {
case 1:
return 'html_popup_witness_ranking_ordinal_st_label';
case 2:
return 'html_popup_witness_ranking_ordinal_nd_label';
case 3:
return 'html_popup_witness_ranking_ordinal_rd_label';
default:
return 'html_popup_witness_ranking_ordinal_th_label';
}
};
const dateToFormattedString = (date) => {
return (0, moment_1.default)(date).format('MM/DD/YY');
};
const shortenString = (str, length = 3) => {
// TODO : remove during merge EVM (duplicate)
return (str === null || str === void 0 ? void 0 : str.length) > length * 2
? `${str.substring(length, 0)}...${str === null || str === void 0 ? void 0 : str.toString().slice(-length)}`
: str;
};
exports.FormatUtils = {
withCommas,
toHP,
toFormattedHP,
fromHP,
formatCurrencyValue,
nFormatter,
hasMoreThanXDecimal,
toNumber,
getSymbol,
getAmountFromNai,
fromNaiAndSymbol,
removeHtmlTags,
getValFromString,
trimUselessZero,
getUSDFromVests,
getOrdinalLabelTranslation,
dateToFormattedString,
shortenString,
};