import dayjs from 'dayjs';
|
|
/**
|
* 格式化日期
|
*/
|
export function formatDate(date: string | Date, format = 'YYYY-MM-DD HH:mm:ss'): string {
|
if (!date) return '';
|
return dayjs(date).format(format);
|
}
|
|
/**
|
* 格式化日期(仅日期)
|
*/
|
export function formatDateOnly(date: string | Date): string {
|
return formatDate(date, 'YYYY-MM-DD');
|
}
|
|
/**
|
* 深拷贝
|
*/
|
export function deepClone<T>(obj: T): T {
|
return JSON.parse(JSON.stringify(obj));
|
}
|
|
/**
|
* 防抖
|
*/
|
export function debounce<T extends (...args: any[]) => any>(
|
func: T,
|
wait: number
|
): (...args: Parameters<T>) => void {
|
let timeout: NodeJS.Timeout | null = null;
|
return function (this: any, ...args: Parameters<T>) {
|
if (timeout) clearTimeout(timeout);
|
timeout = setTimeout(() => {
|
func.apply(this, args);
|
}, wait);
|
};
|
}
|
|
/**
|
* 节流
|
*/
|
export function throttle<T extends (...args: any[]) => any>(
|
func: T,
|
wait: number
|
): (...args: Parameters<T>) => void {
|
let timeout: NodeJS.Timeout | null = null;
|
return function (this: any, ...args: Parameters<T>) {
|
if (!timeout) {
|
timeout = setTimeout(() => {
|
timeout = null;
|
func.apply(this, args);
|
}, wait);
|
}
|
};
|
}
|