@instadapp/avocado-base 0.2.2 → 0.2.4

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.
@@ -1,94 +1,88 @@
1
1
  export function formatPercent(
2
2
  value?: number | string,
3
3
  fractionDigits = 2,
4
- maxValue = null
4
+ maxValue = null,
5
5
  ) {
6
- if (!value || isZero(value)) return "0.00%";
6
+ if (!value || isZero(value))
7
+ return '0.00%'
7
8
 
8
- const valueAsNumber = toBN(value).toNumber();
9
+ const valueAsNumber = toBN(value).toNumber()
9
10
 
10
- if (maxValue && gt(times(valueAsNumber, "100"), maxValue))
11
- return `>${maxValue}%`;
11
+ if (maxValue && gt(times(valueAsNumber, '100'), maxValue))
12
+ return `>${maxValue}%`
12
13
 
13
- const formatter = new Intl.NumberFormat("en-US", {
14
- style: "percent",
14
+ const formatter = new Intl.NumberFormat('en-US', {
15
+ style: 'percent',
15
16
  minimumFractionDigits: fractionDigits,
16
17
  maximumFractionDigits: fractionDigits,
17
- });
18
+ })
18
19
 
19
- return formatter.format(valueAsNumber);
20
+ return formatter.format(valueAsNumber)
20
21
  }
21
22
 
22
23
  export function shortenHash(hash: string | `0x${string}`, length: number = 4) {
23
- if (!hash) return;
24
- if (hash.length < 12) return hash;
25
- const beginningChars = hash.startsWith("0x") ? length + 2 : length;
26
- const shortened =
27
- hash.substr(0, beginningChars) + "..." + hash.substr(-length);
28
- return shortened;
24
+ if (!hash)
25
+ return
26
+ if (hash.length < 12)
27
+ return hash
28
+ const beginningChars = hash.startsWith('0x') ? length + 2 : length
29
+ const shortened
30
+ = `${hash.substr(0, beginningChars)}...${hash.substr(-length)}`
31
+ return shortened
29
32
  }
30
33
 
31
34
  export function formatUsd(value: any, fractionDigits = 2) {
32
- const formatter = new Intl.NumberFormat("en-US", {
33
- style: "currency",
34
- currency: "USD",
35
+ const formatter = new Intl.NumberFormat('en-US', {
36
+ style: 'currency',
37
+ currency: 'USD',
35
38
  minimumFractionDigits: fractionDigits,
36
39
  maximumFractionDigits: fractionDigits,
37
- });
40
+ })
38
41
 
39
- return formatter.format(value);
42
+ return formatter.format(value)
40
43
  }
41
44
 
42
45
  export function signedNumber(numb: string | number) {
43
- return new Intl.NumberFormat("en-US", {
44
- signDisplay: "exceptZero",
45
- }).format(toBN(numb).toNumber());
46
- }
47
-
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
- }
46
+ return new Intl.NumberFormat('en-US', {
47
+ signDisplay: 'exceptZero',
48
+ }).format(toBN(numb).toNumber())
62
49
  }
63
50
 
64
51
  export function formatDecimal(value: string | number, fractionDigits?: number) {
65
52
  if (!value) {
66
- value = "0";
53
+ value = '0'
54
+ }
55
+ if (lt(value, '0.0001') && gt(value, '0')) {
56
+ return '< 0.0001'
67
57
  }
68
- if (lt(value, "0.0001") && gt(value, "0")) {
69
- return "< 0.0001";
70
- } else {
71
- const num = toBN(value);
72
- let decimals;
58
+ else {
59
+ const num = toBN(value)
60
+ let decimals
73
61
 
74
62
  if (num.lt(1)) {
75
- decimals = 4;
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;
63
+ decimals = 4
64
+ }
65
+ else if (num.lt(10)) {
66
+ decimals = 6
67
+ }
68
+ else if (num.lt(100)) {
69
+ decimals = 4
70
+ }
71
+ else if (num.lt(1000)) {
72
+ decimals = 3
73
+ }
74
+ else if (num.lt(10000)) {
75
+ decimals = 2
76
+ }
77
+ else if (num.lt(100000)) {
78
+ decimals = 1
79
+ }
80
+ else {
81
+ decimals = 0
88
82
  }
89
83
 
90
- const formattedNumber = num.toFixed(fractionDigits || decimals);
84
+ const formattedNumber = num.toFixed(fractionDigits || decimals)
91
85
 
92
- return toBN(formattedNumber).toFormat();
86
+ return toBN(formattedNumber).toFormat()
93
87
  }
94
88
  }
package/utils/helper.ts CHANGED
@@ -1,62 +1,65 @@
1
- export const indexSorter = (aIndex: number, bIndex: number) => {
1
+ export function indexSorter(aIndex: number, bIndex: number) {
2
2
  if (aIndex === -1 && bIndex === -1) {
3
- return 0; // fallback to other sorting criteria
4
- } else if (aIndex === -1) {
5
- return 1; // b comes first if a is not in the priority list
6
- } else if (bIndex === -1) {
7
- return -1; // a comes first if b is not in the priority list
8
- } else {
9
- return aIndex - bIndex; // sort by the index in the priority list
3
+ return 0 // fallback to other sorting criteria
10
4
  }
11
- };
5
+ else if (aIndex === -1) {
6
+ return 1 // b comes first if a is not in the priority list
7
+ }
8
+ else if (bIndex === -1) {
9
+ return -1 // a comes first if b is not in the priority list
10
+ }
11
+ else {
12
+ return aIndex - bIndex // sort by the index in the priority list
13
+ }
14
+ }
12
15
 
13
16
  export function sortByMany<T>(
14
17
  items: T[],
15
- callback: ((a: T, b: T) => number)[]
18
+ callback: ((a: T, b: T) => number)[],
16
19
  ) {
17
- return items.sort(function (a, b) {
18
- var result = 0;
19
- for (var i = 0; i < callback.length; i++) {
20
- var func = callback[i];
21
- result = func(a, b);
20
+ return items.sort((a, b) => {
21
+ let result = 0
22
+ for (let i = 0; i < callback.length; i++) {
23
+ const func = callback[i]
24
+ result = func(a, b)
22
25
  if (result !== 0) {
23
- break;
26
+ break
24
27
  }
25
28
  }
26
- return result;
27
- });
29
+ return result
30
+ })
28
31
  }
29
32
 
30
33
  export function cloneDeep<T>(value: T): T {
31
- return JSON.parse(JSON.stringify(value));
34
+ return JSON.parse(JSON.stringify(value))
32
35
  }
33
36
 
34
37
  export function filterArray(array: any, filters: any) {
35
- const filterKeys = Object.keys(filters);
38
+ const filterKeys = Object.keys(filters)
36
39
  return array.filter((item: any) => {
37
40
  // validates all filter criteria
38
41
  return filterKeys.every((key) => {
39
42
  // ignores non-function predicates
40
- if (typeof filters[key] !== "function") return true;
41
- return filters[key](item[key], item);
42
- });
43
- });
43
+ if (typeof filters[key] !== 'function')
44
+ return true
45
+ return filters[key](item[key], item)
46
+ })
47
+ })
44
48
  }
45
49
 
46
50
  export function onImageError(this: HTMLImageElement) {
47
- const parentElement = this.parentElement;
48
- this.onerror = null;
49
- this.remove();
51
+ const parentElement = this.parentElement
52
+ this.onerror = null
53
+ this.remove()
50
54
 
51
55
  if (parentElement) {
52
- parentElement.classList.add("bg-gray-300");
56
+ parentElement.classList.add('bg-gray-300')
53
57
  }
54
58
  }
55
59
 
56
60
  export function formatMultipleAddresses(addresses: string[], shorten = true) {
57
61
  const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' })
58
62
  const formattedString = formatter.format(addresses.map(i => shorten ? shortenHash(i) || '' : i))
59
-
63
+
60
64
  return formattedString
61
65
  }
62
-