PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@iota/iota-sdk/dist/cjs/utils

Просмотр файла: formatBalance.js

"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var formatBalance_exports = {};
__export(formatBalance_exports, {
  CoinFormat: () => CoinFormat,
  formatAmount: () => formatAmount,
  formatBalance: () => formatBalance,
  formatWithSubscript: () => formatWithSubscript
});
module.exports = __toCommonJS(formatBalance_exports);
var import_bignumber = __toESM(require("bignumber.js"));
function formatAmountParts(amount) {
  if (typeof amount === "undefined" || amount === null) {
    return ["--"];
  }
  let postfix = "";
  let bn = new import_bignumber.default(amount.toString());
  const bnAbs = bn.abs();
  if (bnAbs.gte(1e9)) {
    bn = bn.shiftedBy(-9);
    postfix = "B";
  } else if (bnAbs.gte(1e6)) {
    bn = bn.shiftedBy(-6);
    postfix = "M";
  } else if (bnAbs.gte(1e4)) {
    bn = bn.shiftedBy(-3);
    postfix = "K";
  }
  if (bnAbs.gte(1)) {
    bn = bn.decimalPlaces(2, import_bignumber.default.ROUND_DOWN);
  }
  if (bnAbs.gt(0) && bnAbs.lt(1)) {
    const leadingZeros = countDecimalLeadingZeros(bn.toFormat());
    if (leadingZeros >= 4) {
      return [formatWithSubscript(bn.toFormat(), leadingZeros), postfix];
    } else {
      return [bn.toFormat(leadingZeros + 1), postfix];
    }
  }
  return [bn.toFormat(), postfix];
}
function formatAmount(...args) {
  return formatAmountParts(...args).filter(Boolean).join(" ");
}
function countDecimalLeadingZeros(input) {
  if (input === null) {
    return 0;
  }
  const [, decimals] = input.toString().split(".");
  if (!decimals) {
    return 0;
  }
  let count = 0;
  for (const digit of decimals) {
    if (digit === "0") {
      count++;
    } else {
      break;
    }
  }
  return count;
}
const SUBSCRIPTS = ["\u2080", "\u2081", "\u2082", "\u2083", "\u2084", "\u2085", "\u2086", "\u2087", "\u2088", "\u2089"];
function formatWithSubscript(input, zeroCount) {
  if (input === null) {
    return "0";
  }
  const [, decimals] = input.toString().split(".");
  const remainder = decimals.slice(zeroCount);
  const digits = zeroCount.toString().split("");
  const suscripts = digits.map((n) => SUBSCRIPTS[Number(n)]).join("");
  return `0.0${suscripts}${remainder}`;
}
var CoinFormat = /* @__PURE__ */ ((CoinFormat2) => {
  CoinFormat2["Rounded"] = "Rounded";
  CoinFormat2["Full"] = "Full";
  return CoinFormat2;
})(CoinFormat || {});
function formatBalance(balance, decimals, format = "Rounded" /* Rounded */, showSign = false) {
  const bn = new import_bignumber.default(balance.toString()).shiftedBy(-1 * decimals);
  let formattedBalance = formatAmount(bn);
  if (format === "Full" /* Full */) {
    formattedBalance = bn.toFormat();
  }
  if (showSign && !formattedBalance.startsWith("-")) {
    formattedBalance = `+${formattedBalance}`;
  }
  return formattedBalance;
}
//# sourceMappingURL=formatBalance.js.map

Выполнить команду


Для локальной разработки. Не используйте в интернете!