forked from liteflow-labs/starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
99 lines (86 loc) · 2.77 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { getAddress, isAddress } from '@ethersproject/address'
import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration'
import isBetween from 'dayjs/plugin/isBetween'
import relativeTime from 'dayjs/plugin/relativeTime'
import utc from 'dayjs/plugin/utc'
import { BaseError } from 'viem'
dayjs.extend(utc)
dayjs.extend(relativeTime)
dayjs.extend(duration)
dayjs.extend(isBetween)
export const isSameAddress = (addressA: string, addressB: string): boolean => {
return addressA.toLowerCase() === addressB.toLowerCase()
}
export const formatAddress = (
address: string | undefined,
length = 10,
): string => {
if (!address) return ''
if (!isAddress(address)) return ''
const parsed = getAddress(address)
const chars = (length - 2) / 2
return `${parsed.substring(0, chars + 2)}...${parsed.substring(42 - chars)}`
}
export const dateFromNow = (date: Date | string): string => {
return dayjs(date).fromNow()
}
export const formatDate = (date: Date | string): string => {
return dayjs(date).utc().format('DD/MM/YYYY, HH[:]mm UTC')
}
/**
Valid format for inputs min and max
Input doesn't accept other formats for datetime-local
**/
export const formatDateDatetime = (date: Date | string): string => {
return dayjs(date).utc(true).format('YYYY-MM-DD[T]HH[:]mm')
}
export const getHumanizedDate = (second: number): string => {
return dayjs.duration(second, 'seconds').humanize()
}
export const dateIsBefore = (now: Date, startDate: Date): boolean => {
return dayjs(now).isBefore(dayjs(startDate))
}
export const dateIsBetween = (
now: Date,
startDate: Date,
endDate: Date,
): boolean => {
return dayjs(now).isBetween(dayjs(startDate), dayjs(endDate))
}
export function formatError(error: unknown): string | undefined {
if (!error) return
if (typeof error === 'string') return error
if (
typeof error === 'object' &&
'code' in error &&
error.code === 'ACTION_REJECTED'
)
return 'Transaction was cancelled'
if (error instanceof BaseError) return error.shortMessage
if (typeof error === 'object' && 'error' in error)
return formatError(error.error)
if (typeof error === 'object' && 'data' in error)
return formatError(error.data)
if (typeof error === 'object' && 'message' in error)
return formatError(error.message)
if ((error as any).response?.errors?.length >= 0)
return formatError((error as any).response.errors[0])
if (error instanceof Error) return error.message
return 'An unknown error occurred'
}
export function removeEmptyFromObject<T = any>(obj: {
[key: string]: undefined | null | T
}): {
[key: string]: T
} {
return Object.keys(obj).reduce((prev, key) => {
if (obj[key] === undefined || obj[key] === null) {
return prev
}
return {
...prev,
[key]: obj[key],
}
}, {})
}