前端: 添加工具函数

This commit is contained in:
Frontend Developer 2026-03-10 09:20:35 +00:00
parent 892e5acd50
commit e61f2d061f

View File

@ -0,0 +1,25 @@
export const formatFileSize = (bytes) => {
if (!bytes) return '0 B';
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
};
export const formatDate = (date) => {
if (!date) return '';
return new Date(date).toLocaleDateString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
};
export const getFileExt = (filename) => {
return filename?.split('.').pop()?.toLowerCase() || '';
};
export const generateId = () => {
return Date.now().toString(36) + Math.random().toString(36).substr(2);
};