File size: 5,006 Bytes
e3eb984 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function formatDate(date: Date | string): string {
const d = new Date(date)
const now = new Date()
const diff = now.getTime() - d.getTime()
// Less than 1 minute
if (diff < 60000) {
return 'Just now'
}
// Less than 1 hour
if (diff < 3600000) {
const minutes = Math.floor(diff / 60000)
return `${minutes}m ago`
}
// Less than 24 hours
if (diff < 86400000) {
const hours = Math.floor(diff / 3600000)
return `${hours}h ago`
}
// Less than 7 days
if (diff < 604800000) {
const days = Math.floor(diff / 86400000)
return `${days}d ago`
}
// More than 7 days
return d.toLocaleDateString()
}
export function formatTime(date: Date | string): string {
const d = new Date(date)
return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
}
export function formatFileSize(bytes: number): string {
if (bytes === 0) return '0 Bytes'
const k = 1024
const sizes = ['Bytes', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
export function getFileIcon(mimeType: string): string {
if (mimeType.startsWith('image/')) return 'πΌοΈ'
if (mimeType.startsWith('video/')) return 'π₯'
if (mimeType.startsWith('audio/')) return 'π΅'
if (mimeType.includes('pdf')) return 'π'
if (mimeType.includes('word')) return 'π'
if (mimeType.includes('excel') || mimeType.includes('spreadsheet')) return 'π'
if (mimeType.includes('powerpoint') || mimeType.includes('presentation')) return 'π½οΈ'
if (mimeType.includes('zip') || mimeType.includes('rar') || mimeType.includes('archive')) return 'π¦'
return 'π'
}
export function generateAvatar(name: string): string {
const colors = [
'#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7',
'#DDA0DD', '#98D8C8', '#F7DC6F', '#BB8FCE', '#85C1E9'
]
const initials = name
.split(' ')
.map(word => word[0])
.join('')
.toUpperCase()
.slice(0, 2)
const colorIndex = name.charCodeAt(0) % colors.length
const color = colors[colorIndex]
return `data:image/svg+xml,${encodeURIComponent(`
<svg width="40" height="40" xmlns="http://www.w3.org/2000/svg">
<circle cx="20" cy="20" r="20" fill="${color}"/>
<text x="20" y="25" text-anchor="middle" fill="white" font-family="Arial" font-size="14" font-weight="bold">
${initials}
</text>
</svg>
`)}`
}
export function debounce<T extends (...args: any[]) => any>(
func: T,
wait: number
): (...args: Parameters<T>) => void {
let timeout: NodeJS.Timeout | null = null
return (...args: Parameters<T>) => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
func(...args)
}, wait)
}
}
export function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number
): (...args: Parameters<T>) => void {
let inThrottle: boolean
return (...args: Parameters<T>) => {
if (!inThrottle) {
func(...args)
inThrottle = true
setTimeout(() => inThrottle = false, limit)
}
}
}
export function isValidEmail(email: string): boolean {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
return emailRegex.test(email)
}
export function isValidUsername(username: string): boolean {
const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/
return usernameRegex.test(username)
}
export function truncateText(text: string, maxLength: number): string {
if (text.length <= maxLength) return text
return text.slice(0, maxLength) + '...'
}
export function copyToClipboard(text: string): Promise<void> {
if (navigator.clipboard) {
return navigator.clipboard.writeText(text)
} else {
// Fallback for older browsers
const textArea = document.createElement('textarea')
textArea.value = text
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
try {
document.execCommand('copy')
return Promise.resolve()
} catch (err) {
return Promise.reject(err)
} finally {
document.body.removeChild(textArea)
}
}
}
export function downloadFile(url: string, filename: string): void {
const link = document.createElement('a')
link.href = url
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
export function isImageFile(file: File): boolean {
return file.type.startsWith('image/')
}
export function isVideoFile(file: File): boolean {
return file.type.startsWith('video/')
}
export function isAudioFile(file: File): boolean {
return file.type.startsWith('audio/')
}
export function getInitials(name: string): string {
return name
.split(' ')
.map(word => word[0])
.join('')
.toUpperCase()
.slice(0, 2)
}
|