1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
market/src/utils/index.ts

62 lines
1.5 KiB
TypeScript

import axios, { AxiosResponse } from 'axios'
export function updateQueryStringParameter(
uri: string,
key: string,
newValue: string
): string {
const regex = new RegExp('([?&])' + key + '=.*?(&|$)', 'i')
const separator = uri.indexOf('?') !== -1 ? '&' : '?'
if (uri.match(regex)) {
return uri.replace(regex, '$1' + key + '=' + newValue + '$2')
} else {
return uri + separator + key + '=' + newValue
}
}
export function prettySize(
bytes: number,
separator = ' ',
postFix = ''
): string {
if (bytes) {
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']
const i = Math.min(
Math.floor(Math.log(bytes) / Math.log(1024)),
sizes.length - 1
)
return `${(bytes / 1024 ** i).toFixed(i ? 1 : 0)}${separator}${
sizes[i]
}${postFix}`
}
return 'n/a'
}
// Boolean value that will be true if we are inside a browser, false otherwise
export const isBrowser = typeof window !== 'undefined'
export function toStringNoMS(date: Date): string {
return date.toISOString().replace(/\.[0-9]{3}Z/, 'Z')
}
export async function fetchData(url: string): Promise<AxiosResponse['data']> {
try {
const response = await axios(url)
if (response.status !== 200) {
return console.error('Non-200 response: ' + response.status)
}
return response.data
} catch (error) {
console.error('Error parsing json: ' + error.message)
}
}
export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}