forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
31 lines (28 loc) · 1.27 KB
/
Copy pathformat.ts
File metadata and controls
31 lines (28 loc) · 1.27 KB
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
/** Format a byte count → "1.2 GB", "512 MB", "128 KB" */
export function formatBytes(bytes: number): string {
if (bytes >= 1e9) return `${(bytes / 1e9).toFixed(1)} GB`
if (bytes >= 1e6) return `${Math.round(bytes / 1e6)} MB`
return `${Math.round(bytes / 1e3)} KB`
}
/** Format a polygon/vertex count → "1.5M", "10.5k", "500" */
export function formatPoly(count: number): string {
if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`
if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k`
return String(count)
}
/** Format a timestamp → "14:32" */
export function formatTime(ts: number): string {
return new Date(ts).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })
}
/** Format a timestamp → "Today", "Yesterday", "Mar 8", "Mar 8, 2025" */
export function formatDate(ts: number): string {
const d = new Date(ts)
const today = new Date()
const yesterday = new Date(today)
yesterday.setDate(today.getDate() - 1)
if (d.toDateString() === today.toDateString()) return 'Today'
if (d.toDateString() === yesterday.toDateString()) return 'Yesterday'
const opts: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric' }
if (d.getFullYear() !== today.getFullYear()) opts.year = 'numeric'
return d.toLocaleDateString([], opts)
}