Open History to see the last commit
1import he from "he";
2export const HN = "https://news.ycombinator.com";
3export const itemLink = (id: number) =>
4 `${HN}/item?id=${encodeURIComponent(id)}`;
5export const userLink = (author: string) =>
6 `${HN}/user?id=${encodeURIComponent(author)}`;
7export function articleLink(url: string | null, id: number): string {
8 try {
9 const parsed = new URL(url ?? "");
10 if (["https:", "http:"].includes(parsed.protocol)) return parsed.href;
11 } catch {}
12 return itemLink(id);
13}
14export function domain(url: string | null): string | null {
15 try {
16 return new URL(url ?? "").hostname.replace(/^www\./, "");
17 } catch {
18 return null;
19 }
20}
21export const decodeTitle = (text: string) => he.decode(text);
22export function age(created: number, now: number): string {
23 const minutes = Math.max(0, Math.floor((now / 1000 - created) / 60));
24 if (minutes < 60)
25 return `${minutes || 1} minute${minutes === 1 || minutes === 0 ? "" : "s"} ago`;
26 const hours = Math.floor(minutes / 60);
27 if (hours < 24) return `${hours} hour${hours === 1 ? "" : "s"} ago`;
28 const days = Math.floor(hours / 24);
29 return `${days} day${days === 1 ? "" : "s"} ago`;
30}