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('.')
|
2024-04-10 06:16:56 +00:00
|
|
|
|
|
|
|
|
return suffix[suffix.length - 1] === 'docx' ? 'doc' : suffix[suffix.length - 1]
|
2023-11-06 11:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-22 03:05:06 +00:00
|
|
|
/*
|
|
|
|
|
获得文件对应图片
|
|
|
|
|
*/
|
2023-11-06 11:06:02 +00:00
|
|
|
export function getImgUrl(name: string) {
|
2024-04-10 06:16:56 +00:00
|
|
|
const typeList = ['txt', 'pdf', 'doc', 'csv', 'md']
|
|
|
|
|
const type = typeList.includes(fileType(name)) ? fileType(name) : 'unknow'
|
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)
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-03-29 07:45:04 +00:00
|
|
|
|
|
|
|
|
// 数组对象中某一属性值的集合
|
2024-03-29 10:47:34 +00:00
|
|
|
export function getAttrsArray(array: Array<any>, attr: string) {
|
2024-03-29 07:45:04 +00:00
|
|
|
return array.map((item) => {
|
|
|
|
|
return item[attr]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 求和
|
2024-03-29 10:47:34 +00:00
|
|
|
export function getSum(array: Array<any>) {
|
2024-03-29 07:45:04 +00:00
|
|
|
return array.reduce((totol, item) => totol + item, 0)
|
|
|
|
|
}
|