-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathutils.ts
More file actions
24 lines (20 loc) · 925 Bytes
/
utils.ts
File metadata and controls
24 lines (20 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function calcReadTime(text: string | undefined | null): number {
if (!text) return 1;
const wordCount = text.trim().split(/\s+/).filter(Boolean).length;
return Math.max(1, Math.ceil(wordCount / 265));
}
export function formatRelativeDate(dateStr: string): string {
const date = new Date(dateStr);
const now = new Date();
const diffDays = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60 * 24));
if (diffDays < 0) return date.toLocaleDateString("en-US", { year: "numeric", month: "short", day: "numeric" });
if (diffDays === 0) return "today";
if (diffDays === 1) return "yesterday";
if (diffDays <= 30) return `${diffDays} days ago`;
return date.toLocaleDateString("en-US", { year: "numeric", month: "short" });
}