forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
89 lines (78 loc) · 2.86 KB
/
Copy pathutils.ts
File metadata and controls
89 lines (78 loc) · 2.86 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
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
// ─── cn() — className merger (no external dep) ────────────────────────────────
export type ClassValue =
| string
| number
| boolean
| null
| undefined
| ClassValue[]
| Record<string, boolean | null | undefined>
export function cn(...inputs: ClassValue[]): string {
return inputs.flatMap(toClassArray).filter(Boolean).join(' ')
}
function toClassArray(val: ClassValue): string[] {
if (!val && val !== 0) return []
if (typeof val === 'string') return [val]
if (typeof val === 'number') return [String(val)]
if (Array.isArray(val)) return val.flatMap(toClassArray)
if (typeof val === 'object') {
return Object.entries(val)
.filter(([, v]) => Boolean(v))
.map(([k]) => k)
}
return []
}
// ─── Date helpers ─────────────────────────────────────────────────────────────
export function formatDate(iso: string, opts?: Intl.DateTimeFormatOptions): string {
try {
return new Date(iso).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
...opts,
})
} catch {
return iso
}
}
export function formatDateTime(iso: string): string {
try {
return new Date(iso).toLocaleString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
})
} catch {
return iso
}
}
export function timeAgo(iso: string): string {
const seconds = Math.floor((Date.now() - new Date(iso).getTime()) / 1000)
if (seconds < 60) return 'just now'
const minutes = Math.floor(seconds / 60)
if (minutes < 60) return `${minutes}m ago`
const hours = Math.floor(minutes / 60)
if (hours < 24) return `${hours}h ago`
const days = Math.floor(hours / 24)
if (days < 30) return `${days}d ago`
return formatDate(iso)
}
// ─── Clipboard ────────────────────────────────────────────────────────────────
export function copyToClipboard(text: string): Promise<void> {
if (navigator.clipboard) return navigator.clipboard.writeText(text)
const el = document.createElement('textarea')
el.value = text
el.style.position = 'fixed'
el.style.opacity = '0'
document.body.appendChild(el)
el.select()
document.execCommand('copy')
document.body.removeChild(el)
return Promise.resolve()
}
// ─── Misc ─────────────────────────────────────────────────────────────────────
export function sleep(ms: number): Promise<void> {
return new Promise((r) => setTimeout(r, ms))
}