-
Notifications
You must be signed in to change notification settings - Fork 188
/
helpers.ts
156 lines (128 loc) · 3.52 KB
/
helpers.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { CosmosBaseV1Beta1Coin } from '@injectivelabs/core-proto-ts'
import { Coin } from '@injectivelabs/ts-types'
export const isServerSide = () => typeof window === 'undefined'
export const isReactNative = () => {
return (
typeof document === 'undefined' &&
typeof navigator !== 'undefined' &&
navigator.product === 'ReactNative'
)
}
export const isNode = () => {
if (typeof window === 'undefined') {
return true
}
return (
typeof process !== 'undefined' &&
typeof process.versions !== 'undefined' &&
typeof process.versions.node !== 'undefined'
)
}
export const isBrowser = () => {
if (isReactNative()) {
return false
}
if (isNode()) {
return false
}
return typeof window !== 'undefined'
}
export const objectToJson = (
object: Record<string, any>,
params?:
| {
replacer?: any
indentation?: number
}
| undefined,
): string => {
const { replacer, indentation } = params || { replacer: null, indentation: 2 }
return JSON.stringify(object, replacer, indentation)
}
export const protoObjectToJson = (
object: any,
params?:
| {
replacer?: any
indentation?: number
}
| undefined,
): string => {
const { replacer, indentation } = params || { replacer: null, indentation: 2 }
if (object.toObject !== undefined) {
return JSON.stringify(object.toObject(), replacer, indentation)
}
return objectToJson(object, { replacer, indentation })
}
export const grpcCoinToUiCoin = (coin: CosmosBaseV1Beta1Coin.Coin): Coin => ({
amount: coin.amount,
denom: coin.denom,
})
export const uint8ArrayToString = (
string: string | Uint8Array | null | undefined,
): string => {
if (!string) {
return ''
}
if (string.constructor !== Uint8Array) {
return string as string
}
return new TextDecoder().decode(string)
}
export const toPascalCase = (str: string): string => {
return `${str}`
.toLowerCase()
.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w*)/, 'g'),
(_$1, $2, $3) => `${$2.toUpperCase() + $3}`,
)
.replace(new RegExp(/\w/), (s) => s.toUpperCase())
}
export const snakeToPascal = (str: string): string => {
return str
.split('/')
.map((snake) =>
snake
.split('_')
.map((substr) => substr.charAt(0).toUpperCase() + substr.slice(1))
.join(''),
)
.join('/')
}
export const sortObjectByKeysWithReduce = <T>(obj: T): T => {
if (typeof obj !== 'object' || obj === null) return obj
if (Array.isArray(obj)) {
return obj.map((e) => sortObjectByKeysWithReduce(e)).sort() as T
}
return Object.keys(obj)
.sort()
.reduce((sorted, k) => {
const key = k as keyof typeof obj
sorted[key] = sortObjectByKeysWithReduce(obj[key])
return sorted
}, {} as T)
}
export const sortObjectByKeys = <T>(obj: T): T => {
if (typeof obj !== 'object' || obj === null) {
return obj
}
if (Array.isArray(obj)) {
return obj.map(sortObjectByKeys) as T
}
const sortedKeys = Object.keys(obj).sort() as Array<keyof typeof obj>
const result = {} as Record<keyof typeof obj, any>
sortedKeys.forEach((key) => {
result[key] = sortObjectByKeys(obj[key])
})
return result as T
}
export const getErrorMessage = (error: any, endpoint: string): string => {
if (!error.response) {
return `The request to ${endpoint} has failed.`
}
return error.response.data
? error.response.data.message || error.response.data
: error.response.statusText
}