UNPKG

3.5 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { bnToBn } from "../bn/toBn.js";
4import { isBoolean } from "../is/boolean.js";
5import { isUndefined } from "../is/undefined.js";
6import { formatDecimal } from "./formatDecimal.js";
7import { calcSi, findSi, SI, SI_MID } from "./si.js";
8const DEFAULT_DECIMALS = 0;
9const DEFAULT_UNIT = SI[SI_MID].text;
10let defaultDecimals = DEFAULT_DECIMALS;
11let defaultUnit = DEFAULT_UNIT;
12
13function getUnits(si, withSi, withSiFull, withUnit) {
14 const unit = isBoolean(withUnit) ? SI[SI_MID].text : withUnit;
15 return withSi || withSiFull ? si.value === '-' ? withUnit ? ` ${unit}` : '' : ` ${withSiFull ? `${si.text}${withUnit ? ' ' : ''}` : si.value}${withUnit ? unit : ''}` : '';
16}
17
18function getPrePost(text, decimals, forceUnit) {
19 // NOTE We start at midpoint (8) minus 1 - this means that values display as
20 // 123.456 instead of 0.123k (so always 6 relevant). Additionally we use ceil
21 // so there are at most 3 decimal before the decimal separator
22 const si = calcSi(text, decimals, forceUnit);
23 const mid = text.length - (decimals + si.power);
24 const prefix = text.substring(0, mid);
25 const padding = mid < 0 ? 0 - mid : 0;
26 const postfix = `${`${new Array(padding + 1).join('0')}${text}`.substring(mid < 0 ? 0 : mid)}0000`.substring(0, 4);
27 return [si, prefix || '0', postfix];
28} // Formats a string/number with <prefix>.<postfix><type> notation
29
30
31function _formatBalance(input, options = true, optDecimals = defaultDecimals) {
32 let text = bnToBn(input).toString();
33
34 if (text.length === 0 || text === '0') {
35 return '0';
36 } // extract options - the boolean case is for backwards-compat
37
38
39 const {
40 decimals = optDecimals,
41 forceUnit = undefined,
42 withSi = true,
43 withSiFull = false,
44 withUnit = true
45 } = isBoolean(options) ? {
46 withSi: options
47 } : options; // strip the negative sign so we can work with clean groupings, re-add this in the
48 // end when we return the result (from here on we work with positive numbers)
49
50 let sign = '';
51
52 if (text[0].startsWith('-')) {
53 sign = '-';
54 text = text.substring(1);
55 }
56
57 const [si, prefix, postfix] = getPrePost(text, decimals, forceUnit);
58 const units = getUnits(si, withSi, withSiFull, withUnit);
59 return `${sign}${formatDecimal(prefix)}.${postfix}${units}`;
60}
61
62export const formatBalance = _formatBalance; // eslint-disable-next-line @typescript-eslint/unbound-method
63
64formatBalance.calcSi = (text, decimals = defaultDecimals) => calcSi(text, decimals); // eslint-disable-next-line @typescript-eslint/unbound-method
65
66
67formatBalance.findSi = findSi; // eslint-disable-next-line @typescript-eslint/unbound-method
68
69formatBalance.getDefaults = () => {
70 return {
71 decimals: defaultDecimals,
72 unit: defaultUnit
73 };
74}; // get allowable options to display in a dropdown
75// eslint-disable-next-line @typescript-eslint/unbound-method
76
77
78formatBalance.getOptions = (decimals = defaultDecimals) => {
79 return SI.filter(({
80 power
81 }) => power < 0 ? decimals + power >= 0 : true);
82}; // Sets the default decimals to use for formatting (ui-wide)
83// eslint-disable-next-line @typescript-eslint/unbound-method
84
85
86formatBalance.setDefaults = ({
87 decimals,
88 unit
89}) => {
90 defaultDecimals = isUndefined(decimals) ? defaultDecimals : Array.isArray(decimals) ? decimals[0] : decimals;
91 defaultUnit = isUndefined(unit) ? defaultUnit : Array.isArray(unit) ? unit[0] : unit;
92 SI[SI_MID].text = defaultUnit;
93};
\No newline at end of file