UNPKG

3.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.formatBalance = void 0;
4const toBn_js_1 = require("../bn/toBn.js");
5const boolean_js_1 = require("../is/boolean.js");
6const formatDecimal_js_1 = require("./formatDecimal.js");
7const getSeparator_js_1 = require("./getSeparator.js");
8const si_js_1 = require("./si.js");
9const DEFAULT_DECIMALS = 0;
10const DEFAULT_UNIT = si_js_1.SI[si_js_1.SI_MID].text;
11let defaultDecimals = DEFAULT_DECIMALS;
12let defaultUnit = DEFAULT_UNIT;
13function _formatBalance(input, { decimals = defaultDecimals, forceUnit, locale = 'en', withAll = false, withSi = true, withSiFull = false, withUnit = true, withZero = true } = {}) {
14 // we only work with string inputs here - convert anything
15 // into the string-only value
16 let text = (0, toBn_js_1.bnToBn)(input).toString();
17 if (text.length === 0 || text === '0') {
18 return '0';
19 }
20 // strip the negative sign so we can work with clean groupings, re-add this in the
21 // end when we return the result (from here on we work with positive numbers)
22 let sign = '';
23 if (text[0].startsWith('-')) {
24 sign = '-';
25 text = text.substring(1);
26 }
27 // We start at midpoint (8) minus 1 - this means that values display as
28 // 123.4567 instead of 0.1234 k (so we always have the most relevant).
29 const si = (0, si_js_1.calcSi)(text, decimals, forceUnit);
30 const mid = text.length - (decimals + si.power);
31 const pre = mid <= 0 ? '0' : text.substring(0, mid);
32 // get the post from the midpoint onward and then first add max decimals
33 // before trimming to the correct (calculated) amount of decimals again
34 let post = text
35 .padStart(mid < 0 ? decimals : 1, '0')
36 .substring(mid < 0 ? 0 : mid)
37 .padEnd(withAll ? Math.max(decimals, 4) : 4, '0')
38 .substring(0, withAll ? Math.max(4, decimals + si.power) : 4);
39 // remove all trailing 0's (if required via flag)
40 if (!withZero) {
41 let end = post.length - 1;
42 // This looks inefficient, however it is better to do the checks and
43 // only make one final slice than it is to do it in multiples
44 do {
45 if (post[end] === '0') {
46 end--;
47 }
48 } while (post[end] === '0');
49 post = post.substring(0, end + 1);
50 }
51 // the display unit
52 const unit = (0, boolean_js_1.isBoolean)(withUnit)
53 ? si_js_1.SI[si_js_1.SI_MID].text
54 : withUnit;
55 // format the units for display based on the flags
56 const units = withSi || withSiFull
57 ? si.value === '-'
58 ? withUnit
59 ? ` ${unit}`
60 : ''
61 : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}`
62 : '';
63 const { decimal, thousand } = (0, getSeparator_js_1.getSeparator)(locale);
64 return `${sign}${(0, formatDecimal_js_1.formatDecimal)(pre, thousand)}${post && `${decimal}${post}`}${units}`;
65}
66exports.formatBalance = _formatBalance;
67exports.formatBalance.calcSi = (text, decimals = defaultDecimals) => (0, si_js_1.calcSi)(text, decimals);
68exports.formatBalance.findSi = si_js_1.findSi;
69exports.formatBalance.getDefaults = () => {
70 return {
71 decimals: defaultDecimals,
72 unit: defaultUnit
73 };
74};
75exports.formatBalance.getOptions = (decimals = defaultDecimals) => {
76 return si_js_1.SI.filter(({ power }) => power < 0
77 ? (decimals + power) >= 0
78 : true);
79};
80exports.formatBalance.setDefaults = ({ decimals, unit }) => {
81 defaultDecimals = (Array.isArray(decimals)
82 ? decimals[0]
83 : decimals) ?? defaultDecimals;
84 defaultUnit = (Array.isArray(unit)
85 ? unit[0]
86 : unit) ?? defaultUnit;
87 si_js_1.SI[si_js_1.SI_MID].text = defaultUnit;
88};