@instadapp/avocado-base 0.0.14 → 0.0.18

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/formatter.ts +49 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instadapp/avocado-base",
3
- "version": "0.0.14",
3
+ "version": "0.0.18",
4
4
  "type": "module",
5
5
  "main": "./nuxt.config.ts",
6
6
  "types": "global.d.ts",
@@ -1,11 +1,14 @@
1
1
  export function formatPercent(
2
- value: number,
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
- if (maxValue && gt(times(value, "100"), maxValue)) return `>${maxValue}%`;
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(value);
19
+ return formatter.format(valueAsNumber);
17
20
  }
18
21
 
19
22
  export function shortenHash(hash: string, length: number = 4) {
@@ -42,10 +45,50 @@ export function signedNumber(numb: string | number) {
42
45
  }).format(toBN(numb).toNumber());
43
46
  }
44
47
 
45
- export function formatDecimal(value: string | number, decimalPlaces = 5) {
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(value: string | number, fractionDigits?: number) {
46
65
  if (!value) {
47
66
  value = "0";
48
67
  }
68
+ if (lt(value, "0.0001") && gt(value, "0")) {
69
+ return "< 0.0001";
70
+ } else {
71
+ const num = toBN(value);
72
+ let decimals;
73
+
74
+ if (num.lt(1)) {
75
+ decimals = 8;
76
+ } else if (num.lt(10)) {
77
+ decimals = 6;
78
+ } else if (num.lt(100)) {
79
+ decimals = 4;
80
+ } else if (num.lt(1000)) {
81
+ decimals = 3;
82
+ } else if (num.lt(10000)) {
83
+ decimals = 2;
84
+ } else if (num.lt(100000)) {
85
+ decimals = 1;
86
+ } else {
87
+ decimals = 0;
88
+ }
49
89
 
50
- return toBN(value).decimalPlaces(decimalPlaces).toFormat();
90
+ const formattedNumber = num.toFixed(fractionDigits || decimals);
91
+
92
+ return toBN(formattedNumber).toFormat();
93
+ }
51
94
  }