UnisKB/ui/src/utils/utils.ts

91 lines
2.5 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('.')
2024-04-10 09:29:09 +00:00
return suffix[suffix.length - 1]
2023-11-06 11:06:02 +00:00
}
2023-11-22 03:05:06 +00:00
/*
*/
const typeList: any = {
2024-08-28 11:04:50 +00:00
txt: ['txt', 'pdf', 'docx', 'csv', 'md', 'html', 'PDF'],
table: ['xlsx', 'xls', 'csv'],
QA: ['xlsx', 'csv', 'xls']
}
2023-11-06 11:06:02 +00:00
export function getImgUrl(name: string) {
const list = Object.values(typeList).flat()
const type = list.includes(fileType(name.toLowerCase())) ? fileType(name.toLowerCase()) : '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
}
2024-04-10 07:50:57 +00:00
// 是否是白名单后缀
export function isRightType(name: string, type: string) {
return typeList[type].includes(fileType(name))
2024-04-10 07:50:57 +00:00
}
2023-11-27 03:21:56 +00:00
/*
*/
2024-04-15 10:46:03 +00:00
export function relatedObject(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
// 排序
2024-07-01 01:45:59 +00:00
export function arraySort(list: Array<any>, property: any, desc?: boolean) {
2024-01-02 09:32:41 +00:00
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-04-15 06:42:57 +00:00
return array.reduce((total, item) => total + item, 0)
2024-03-29 07:45:04 +00:00
}