@instadapp/avocado-base 0.0.14 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/utils/formatter.ts +40 -6
package/package.json
CHANGED
package/utils/formatter.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export function formatPercent(
|
|
2
|
-
value
|
|
2
|
+
value?: number | string,
|
|
3
3
|
fractionDigits = 2,
|
|
4
4
|
maxValue = null
|
|
5
5
|
) {
|
|
6
|
-
if (isZero(value)) return "0.00%";
|
|
6
|
+
if (!value || isZero(value)) return "0.00%";
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const valueAsNumber = toBN(value).toNumber();
|
|
9
|
+
|
|
10
|
+
if (maxValue && gt(times(valueAsNumber, "100"), maxValue))
|
|
11
|
+
return `>${maxValue}%`;
|
|
9
12
|
|
|
10
13
|
const formatter = new Intl.NumberFormat("en-US", {
|
|
11
14
|
style: "percent",
|
|
@@ -13,7 +16,7 @@ export function formatPercent(
|
|
|
13
16
|
maximumFractionDigits: fractionDigits,
|
|
14
17
|
});
|
|
15
18
|
|
|
16
|
-
return formatter.format(
|
|
19
|
+
return formatter.format(valueAsNumber);
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export function shortenHash(hash: string, length: number = 4) {
|
|
@@ -42,10 +45,41 @@ export function signedNumber(numb: string | number) {
|
|
|
42
45
|
}).format(toBN(numb).toNumber());
|
|
43
46
|
}
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
function getFractionDigits(value: string | number) {
|
|
49
|
+
const absoluteValue = toBN(value).abs();
|
|
50
|
+
|
|
51
|
+
if (isZero(absoluteValue)) {
|
|
52
|
+
return 2;
|
|
53
|
+
} else if (lt(absoluteValue, 0.01)) {
|
|
54
|
+
return 6;
|
|
55
|
+
} else if (lt(absoluteValue, 1)) {
|
|
56
|
+
return 4;
|
|
57
|
+
} else if (lt(absoluteValue, 10000)) {
|
|
58
|
+
return 2;
|
|
59
|
+
} else {
|
|
60
|
+
return 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function formatDecimal(
|
|
65
|
+
value: string | number,
|
|
66
|
+
fractionDigits = getFractionDigits(value)
|
|
67
|
+
) {
|
|
46
68
|
if (!value) {
|
|
47
69
|
value = "0";
|
|
48
70
|
}
|
|
71
|
+
let formatter;
|
|
72
|
+
if (lt(value, "0.0001") && gt(value, "0")) {
|
|
73
|
+
return "< 0.0001";
|
|
74
|
+
} else {
|
|
75
|
+
formatter = new Intl.NumberFormat("en-US", {
|
|
76
|
+
style: "decimal",
|
|
77
|
+
minimumFractionDigits: fractionDigits,
|
|
78
|
+
maximumFractionDigits: fractionDigits,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const valueAsNumber = toBN(value).toNumber();
|
|
49
83
|
|
|
50
|
-
return
|
|
84
|
+
return formatter.format(valueAsNumber);
|
|
51
85
|
}
|