UnisKB/ui/src/utils/utils.ts

67 lines
1.8 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'
2024-01-30 08:44:26 +00:00
return new URL(`../assets/${type}-icon.svg`, import.meta.url).href
2023-11-06 11:06:02 +00:00
}
2023-11-27 03:21:56 +00:00
/*
*/
2024-01-22 07:33:48 +00:00
export function realatedObject(list: any, val: any, attr: string) {
2023-11-27 03:21:56 +00:00
const filterData: any = list.filter((item: any) => item[attr] === val)?.[0]
return filterData || null
}
2023-11-30 10:50:42 +00:00
2024-01-02 09:32:41 +00:00
// 排序
export function arraySort(list: Array<string>, property: any, desc?: boolean) {
return list.sort((a: any, b: any) => {
return desc ? b[property] - a[property] : a[property] - b[property]
})
}
2024-01-08 09:53:50 +00:00
// 判断对象里所有属性全部为空
export function isAllPropertiesEmpty(obj: object) {
return Object.values(obj).every(
(value) =>
value === null || typeof value === 'undefined' || (typeof value === 'string' && !value)
)
}