1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.formatBalance = void 0;
|
4 | const toBn_js_1 = require("../bn/toBn.js");
|
5 | const boolean_js_1 = require("../is/boolean.js");
|
6 | const formatDecimal_js_1 = require("./formatDecimal.js");
|
7 | const getSeparator_js_1 = require("./getSeparator.js");
|
8 | const si_js_1 = require("./si.js");
|
9 | const DEFAULT_DECIMALS = 0;
|
10 | const DEFAULT_UNIT = si_js_1.SI[si_js_1.SI_MID].text;
|
11 | let defaultDecimals = DEFAULT_DECIMALS;
|
12 | let defaultUnit = DEFAULT_UNIT;
|
13 | function _formatBalance(input, { decimals = defaultDecimals, forceUnit, locale = 'en', withAll = false, withSi = true, withSiFull = false, withUnit = true, withZero = true } = {}) {
|
14 |
|
15 |
|
16 | let text = (0, toBn_js_1.bnToBn)(input).toString();
|
17 | if (text.length === 0 || text === '0') {
|
18 | return '0';
|
19 | }
|
20 |
|
21 |
|
22 | let sign = '';
|
23 | if (text[0].startsWith('-')) {
|
24 | sign = '-';
|
25 | text = text.substring(1);
|
26 | }
|
27 |
|
28 |
|
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 |
|
33 |
|
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 |
|
40 | if (!withZero) {
|
41 | let end = post.length - 1;
|
42 |
|
43 |
|
44 | do {
|
45 | if (post[end] === '0') {
|
46 | end--;
|
47 | }
|
48 | } while (post[end] === '0');
|
49 | post = post.substring(0, end + 1);
|
50 | }
|
51 |
|
52 | const unit = (0, boolean_js_1.isBoolean)(withUnit)
|
53 | ? si_js_1.SI[si_js_1.SI_MID].text
|
54 | : withUnit;
|
55 |
|
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 | }
|
66 | exports.formatBalance = _formatBalance;
|
67 | exports.formatBalance.calcSi = (text, decimals = defaultDecimals) => (0, si_js_1.calcSi)(text, decimals);
|
68 | exports.formatBalance.findSi = si_js_1.findSi;
|
69 | exports.formatBalance.getDefaults = () => {
|
70 | return {
|
71 | decimals: defaultDecimals,
|
72 | unit: defaultUnit
|
73 | };
|
74 | };
|
75 | exports.formatBalance.getOptions = (decimals = defaultDecimals) => {
|
76 | return si_js_1.SI.filter(({ power }) => power < 0
|
77 | ? (decimals + power) >= 0
|
78 | : true);
|
79 | };
|
80 | exports.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 | };
|