mango-v4-ui/utils/formatting.ts

25 lines
591 B
TypeScript
Raw Normal View History

2022-07-14 20:38:02 -07:00
import { PublicKey } from '@solana/web3.js'
import { formatNumericValue, numberCompacter } from './numbers'
2022-07-14 20:38:02 -07:00
export function abbreviateAddress(address: PublicKey, size = 5) {
const base58 = address.toBase58()
return base58.slice(0, size) + '…' + base58.slice(-size)
}
2023-01-19 02:42:39 -08:00
export const formatYAxis = (value: number) => {
return value === 0
? '0'
: Math.abs(value) > 1
2023-01-19 02:42:39 -08:00
? numberCompacter.format(value)
: formatNumericValue(value, 4)
2023-01-19 02:42:39 -08:00
}
export const tryParse = (val: string) => {
try {
const json = JSON.parse(val)
return json
} catch (e) {
return val
}
}