UNPKG

6.23 kBSource Map (JSON)View Raw
1{"version":3,"file":"format.js","sources":["../src/utils/format.js"],"sourcesContent":["import JSBI from 'jsbi'\nimport { NO_BREAK_SPACE } from './characters'\nimport { divideRoundBigInt } from './math'\n\n/**\n * Formats an integer based on a limited range.\n *\n * Example:\n * formatIntegerRange(234, 0, 99, '+') === \"99+\"\n *\n * @param {number} value The number to format.\n * @param {number} min Range minimum.\n * @param {number} max Range maximum.\n * @param {number} maxSuffix Suffix to add if the value exceeds the max.\n */\nexport function formatIntegerRange(\n value = -1,\n min = 0,\n max = 99,\n maxSuffix = ''\n) {\n value = parseInt(value, 10)\n if (value <= min) {\n return `${parseInt(min, 10)}`\n }\n if (value > max) {\n return `${parseInt(max, 10)}${maxSuffix}`\n }\n return String(value)\n}\n\n/**\n * Formats a number for display purposes.\n *\n * This function is not using Intl.NumberFormat() to be compatible with big\n * integers expressed as string, or BigInt-like objects.\n *\n * @param {BigInt|string|number} number Number to convert\n * @returns {string}\n */\nexport function formatNumber(number) {\n const numAsString = String(number)\n const [integer, decimals] = numAsString.split('.')\n\n return [...integer].reverse().reduce(\n (result, digit, index) => {\n return digit + (index > 0 && index % 3 === 0 ? ',' : '') + result\n },\n decimals ? `.${decimals}` : ''\n )\n}\n\n/**\n * Formats a token amount for display purposes.\n *\n * @param {BigInt|string|number} amount Number to round\n * @param {BigInt|string|number} decimals Decimal placement for amount\n * @param {BigInt|string|number} digits Rounds the number to a given decimal place\n * @param {boolean} options.displaySign Decides if the sign should be displayed\n * @param {string} options.symbol Symbol for the token amount\n * @returns {string}\n */\nexport function formatTokenAmount(\n amount,\n decimals,\n { digits = 2, symbol = '', displaySign = false } = {}\n) {\n amount = JSBI.BigInt(String(amount))\n decimals = JSBI.BigInt(String(decimals))\n digits = JSBI.BigInt(String(digits))\n\n if (JSBI.lessThan(decimals, 0)) {\n throw new Error('formatTokenAmount(): decimals cannot be negative')\n }\n\n if (JSBI.lessThan(digits, 0)) {\n throw new Error('formatTokenAmount(): digits cannot be negative')\n }\n\n const _0 = JSBI.BigInt(0)\n const _10 = JSBI.BigInt(10)\n const negative = JSBI.lessThan(amount, _0)\n\n if (negative) {\n amount = JSBI.unaryMinus(amount)\n }\n\n const amountConverted = JSBI.BigInt(\n divideRoundBigInt(\n amount,\n JSBI.exponentiate(_10, JSBI.subtract(decimals, digits))\n )\n )\n\n const leftPart = formatNumber(\n JSBI.divide(amountConverted, JSBI.exponentiate(_10, digits))\n )\n\n const rightPart = String(\n JSBI.remainder(amountConverted, JSBI.exponentiate(_10, digits))\n )\n .padStart(digits, '0')\n .replace(/0+$/, '')\n\n return [\n displaySign ? (negative ? '-' : '+') : '',\n leftPart,\n rightPart ? `.${rightPart}` : '',\n symbol ? `${NO_BREAK_SPACE}${symbol}` : '',\n ].join('')\n}\n"],"names":["formatIntegerRange","value","min","max","maxSuffix","parseInt","String","formatNumber","number","numAsString","split","integer","decimals","_toConsumableArray","reverse","reduce","result","digit","index","formatTokenAmount","amount","digits","symbol","displaySign","JSBI","BigInt","lessThan","Error","_0","_10","negative","unaryMinus","amountConverted","divideRoundBigInt","exponentiate","subtract","leftPart","divide","rightPart","remainder","padStart","replace","NO_BREAK_SPACE","join"],"mappings":";;;;;;;;;;AAIA;;;;;;;;;;;;AAWO,SAASA,kBAAT,GAKL;AAAA,MAJAC,KAIA,uEAJQ,CAAC,CAIT;AAAA,MAHAC,GAGA,uEAHM,CAGN;AAAA,MAFAC,GAEA,uEAFM,EAEN;AAAA,MADAC,SACA,uEADY,EACZ;AACAH,EAAAA,KAAK,GAAGI,QAAQ,CAACJ,KAAD,EAAQ,EAAR,CAAhB;;AACA,MAAIA,KAAK,IAAIC,GAAb,EAAkB;AAChB,qBAAUG,QAAQ,CAACH,GAAD,EAAM,EAAN,CAAlB;AACD;;AACD,MAAID,KAAK,GAAGE,GAAZ,EAAiB;AACf,qBAAUE,QAAQ,CAACF,GAAD,EAAM,EAAN,CAAlB,SAA8BC,SAA9B;AACD;;AACD,SAAOE,MAAM,C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