UnisKB/ui/src/utils/utils.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-11-07 11:04:59 +00:00
export function toThousands(num: any) {
2023-11-10 11:05:52 +00:00
return num?.toString().replace(/\d+/, function (n: any) {
2023-11-03 08:12:57 +00:00
return n.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
})
}
export function numberFormat(num: number) {
return num < 1000 ? toThousands(num) : toThousands((num / 1000).toFixed(1)) + 'k'
}
2023-11-06 11:06:02 +00:00
export function filesize(size: number) {
if (!size) return ''
2023-11-22 03:05:06 +00:00
/* byte */
const num = 1024.0
2023-11-06 11:06:02 +00:00
if (size < num) return size + 'B'
if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + 'K' //kb
if (size < Math.pow(num, 3)) return (size / Math.pow(num, 2)).toFixed(2) + 'M' //M
if (size < Math.pow(num, 4)) return (size / Math.pow(num, 3)).toFixed(2) + 'G' //G
return (size / Math.pow(num, 4)).toFixed(2) + 'T' //T
}
2023-11-28 09:39:35 +00:00
/*
id
*/
export const randomId = function () {
return Math.floor(Math.random() * 10000) + ''
}
2023-11-22 03:05:06 +00:00
/*
*/
2023-11-06 11:06:02 +00:00
export function fileType(name: string) {
const suffix = name.split('.')
return suffix[suffix.length - 1]
}
2023-11-22 03:05:06 +00:00
/*
*/
2023-11-06 11:06:02 +00:00
export function getImgUrl(name: string) {
const type = fileType(name) || 'txt'
return `/src/assets/${type}-icon.svg`
}
2023-11-27 03:21:56 +00:00
/*
*/
export function realatedObject(list: any, val: string | number, attr: string) {
const filterData: any = list.filter((item: any) => item[attr] === val)?.[0]
return filterData || null
}