chore: initialize frontend miniapp repository
This commit is contained in:
90
src/utils/format.ts
Normal file
90
src/utils/format.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* 格式化工具函数
|
||||
*/
|
||||
|
||||
/**
|
||||
* 格式化时间
|
||||
* @param time 时间字符串
|
||||
* @returns 格式化后的时间
|
||||
*/
|
||||
export const formatTime = (time: string): string => {
|
||||
return time
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化距离
|
||||
* @param distance 距离(米)
|
||||
* @returns 格式化后的距离
|
||||
*/
|
||||
export const formatDistance = (distance: number): string => {
|
||||
if (distance < 1000) {
|
||||
return `${distance}米`
|
||||
}
|
||||
return `${(distance / 1000).toFixed(1)}公里`
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化时长
|
||||
* @param minutes 分钟数
|
||||
* @returns 格式化后的时长
|
||||
*/
|
||||
export const formatDuration = (minutes: number): string => {
|
||||
if (minutes < 60) {
|
||||
return `${minutes}分钟`
|
||||
}
|
||||
const hours = Math.floor(minutes / 60)
|
||||
const mins = minutes % 60
|
||||
return mins > 0 ? `${hours}小时${mins}分钟` : `${hours}小时`
|
||||
}
|
||||
|
||||
/**
|
||||
* 截断文本
|
||||
* @param text 文本
|
||||
* @param maxLength 最大长度
|
||||
* @returns 截断后的文本
|
||||
*/
|
||||
export const truncateText = (text: string, maxLength: number): string => {
|
||||
if (text.length <= maxLength) {
|
||||
return text
|
||||
}
|
||||
return text.substring(0, maxLength) + '...'
|
||||
}
|
||||
|
||||
/**
|
||||
* 防抖函数
|
||||
* @param fn 函数
|
||||
* @param delay 延迟时间(毫秒)
|
||||
* @returns 防抖后的函数
|
||||
*/
|
||||
export const debounce = <T extends (...args: any[]) => any>(
|
||||
fn: T,
|
||||
delay: number
|
||||
): ((...args: Parameters<T>) => void) => {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
return (...args: Parameters<T>) => {
|
||||
if (timer) clearTimeout(timer)
|
||||
timer = setTimeout(() => {
|
||||
fn(...args)
|
||||
}, delay)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 节流函数
|
||||
* @param fn 函数
|
||||
* @param delay 延迟时间(毫秒)
|
||||
* @returns 节流后的函数
|
||||
*/
|
||||
export const throttle = <T extends (...args: any[]) => any>(
|
||||
fn: T,
|
||||
delay: number
|
||||
): ((...args: Parameters<T>) => void) => {
|
||||
let lastTime = 0
|
||||
return (...args: Parameters<T>) => {
|
||||
const now = Date.now()
|
||||
if (now - lastTime >= delay) {
|
||||
fn(...args)
|
||||
lastTime = now
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user