initial
This commit is contained in:
6
layers/shared/utils/copy.ts
Normal file
6
layers/shared/utils/copy.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export async function $copy(value: string | number) {
|
||||
try {
|
||||
await navigator.clipboard.writeText(value.toString())
|
||||
}
|
||||
catch {}
|
||||
}
|
||||
26
layers/shared/utils/download-file.ts
Normal file
26
layers/shared/utils/download-file.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export default (filename: string, content: string | File | Blob) => {
|
||||
let blob: File | Blob
|
||||
|
||||
if (typeof content === 'string') {
|
||||
blob = new File([content], filename, {
|
||||
type: 'text/plain',
|
||||
})
|
||||
}
|
||||
else {
|
||||
blob = content
|
||||
}
|
||||
|
||||
const url = window.URL.createObjectURL(blob)
|
||||
const link = document.createElement('a')
|
||||
link.href = url
|
||||
link.setAttribute(
|
||||
'download',
|
||||
filename,
|
||||
)
|
||||
document.body.appendChild(link)
|
||||
|
||||
link.click()
|
||||
|
||||
window.URL.revokeObjectURL(url)
|
||||
document.body.removeChild(link)
|
||||
}
|
||||
8
layers/shared/utils/hide-email.ts
Normal file
8
layers/shared/utils/hide-email.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export default function hideEmail(email: string) {
|
||||
const atIndex = email.indexOf('@')
|
||||
const username = email.substring(0, atIndex)
|
||||
const domain = email.substring(atIndex + 1)
|
||||
const hiddenUsername = `${username.substring(0, 2)}**`
|
||||
|
||||
return `${hiddenUsername}@${domain}`
|
||||
}
|
||||
89
layers/shared/utils/money.ts
Normal file
89
layers/shared/utils/money.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import Decimal from 'decimal.js'
|
||||
|
||||
const CURRENCY_SYMBOL: Record<string, string> = {
|
||||
RUB: '₽',
|
||||
USD: '$',
|
||||
EUR: '€',
|
||||
}
|
||||
|
||||
const DEFAULT_FORMAT: string = '{amount} {currency}'
|
||||
const CURRENCY_FORMAT: Record<string, string> = {
|
||||
USD: '{currency}{amount}',
|
||||
RUB: '{amount}{currency}',
|
||||
EUR: '{currency}{amount}',
|
||||
}
|
||||
|
||||
const DEFAULT_EXPONENT: number = 2
|
||||
const CURRENCY_EXPONENT: Record<string, number> = {
|
||||
BTC: 6,
|
||||
USDT: 2,
|
||||
}
|
||||
|
||||
function getSymbol(currency: string): string {
|
||||
return CURRENCY_SYMBOL[currency] ?? currency
|
||||
}
|
||||
|
||||
function getFormat(currency: string): string {
|
||||
return CURRENCY_FORMAT[currency] ?? DEFAULT_FORMAT
|
||||
}
|
||||
|
||||
function getExponent(currency: string): number {
|
||||
return CURRENCY_EXPONENT[currency] ?? DEFAULT_EXPONENT
|
||||
}
|
||||
|
||||
function format(value: Decimal.Value, currency: string): string {
|
||||
const exponent = getExponent(currency)
|
||||
|
||||
try {
|
||||
return new Decimal(value).toFixed(exponent)
|
||||
}
|
||||
catch {
|
||||
return new Decimal(0).toFixed(exponent)
|
||||
}
|
||||
}
|
||||
|
||||
function fullFormat(value: Decimal.Value, currency: string, showCurrency = true, approximately = false): string {
|
||||
let result = ''
|
||||
let decimalValue: Decimal.Instance
|
||||
const exponent = getExponent(currency)
|
||||
|
||||
try {
|
||||
decimalValue = new Decimal(value)
|
||||
}
|
||||
catch {
|
||||
decimalValue = new Decimal(0)
|
||||
}
|
||||
|
||||
value = decimalValue.toFixed(exponent)
|
||||
|
||||
if (approximately && !decimalValue.isZero())
|
||||
result = '~'
|
||||
|
||||
if (showCurrency) {
|
||||
const symbol = getSymbol(currency)
|
||||
const format = getFormat(currency)
|
||||
|
||||
result += format.replace('{amount}', value).replace('{currency}', symbol)
|
||||
}
|
||||
else {
|
||||
result += value
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
function convert(amount: Decimal.Value, rate: Decimal.Value, isTurnRate = true) {
|
||||
if (isTurnRate)
|
||||
return Decimal.mul(amount, rate)
|
||||
else
|
||||
return Decimal.div(amount, rate)
|
||||
}
|
||||
|
||||
export const $money = {
|
||||
getSymbol,
|
||||
getFormat,
|
||||
getExponent,
|
||||
format,
|
||||
fullFormat,
|
||||
convert,
|
||||
}
|
||||
Reference in New Issue
Block a user