Put src/lib/utils.ts
Browse files- src/lib/utils.ts +87 -0
src/lib/utils.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { clsx, type ClassValue } from "clsx";
|
| 2 |
+
import { twMerge } from "tailwind-merge";
|
| 3 |
+
import { formatDistanceToNowStrict } from "date-fns";
|
| 4 |
+
|
| 5 |
+
export function cn(...inputs: ClassValue[]) {
|
| 6 |
+
return twMerge(clsx(inputs));
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
export function timeAgo(date: Date | string | number | null | undefined) {
|
| 10 |
+
if (!date) return "";
|
| 11 |
+
const d = new Date(date);
|
| 12 |
+
const diff = Date.now() - d.getTime();
|
| 13 |
+
if (diff < 60_000) return "just now";
|
| 14 |
+
return formatDistanceToNowStrict(d, { addSuffix: false })
|
| 15 |
+
.replace(" seconds", "s")
|
| 16 |
+
.replace(" second", "s")
|
| 17 |
+
.replace(" minutes", "m")
|
| 18 |
+
.replace(" minute", "m")
|
| 19 |
+
.replace(" hours", "h")
|
| 20 |
+
.replace(" hour", "h")
|
| 21 |
+
.replace(" days", "d")
|
| 22 |
+
.replace(" day", "d")
|
| 23 |
+
.replace(" months", "mo")
|
| 24 |
+
.replace(" month", "mo")
|
| 25 |
+
.replace(" years", "y")
|
| 26 |
+
.replace(" year", "y");
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
export function formatSalary(min?: number | null, max?: number | null, text?: string | null) {
|
| 30 |
+
const k = (n: number) => (n >= 1000 ? `$${Math.round(n / 1000)}k` : `$${n}`);
|
| 31 |
+
if (min && max && max >= min) return `${k(min)} – ${k(max)}`;
|
| 32 |
+
if (min) return `${k(min)}+`;
|
| 33 |
+
if (max) return `Up to ${k(max)}`;
|
| 34 |
+
return text?.trim() || "";
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
export function stripHtml(html: string) {
|
| 38 |
+
return html
|
| 39 |
+
.replace(/<\s*(br|\/p|\/div|\/li|\/h[1-6])\s*\/?>/gi, "\n")
|
| 40 |
+
.replace(/<li[^>]*>/gi, "• ")
|
| 41 |
+
.replace(/<[^>]+>/g, " ")
|
| 42 |
+
.replace(/ /g, " ")
|
| 43 |
+
.replace(/&/g, "&")
|
| 44 |
+
.replace(/</g, "<")
|
| 45 |
+
.replace(/>/g, ">")
|
| 46 |
+
.replace(/"/g, '"')
|
| 47 |
+
.replace(/'|’|'/g, "'")
|
| 48 |
+
.replace(/[ \t]+/g, " ")
|
| 49 |
+
.replace(/\n\s*\n\s*\n+/g, "\n\n")
|
| 50 |
+
.trim();
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
export function initials(name: string) {
|
| 54 |
+
return name
|
| 55 |
+
.split(/\s+/)
|
| 56 |
+
.filter(Boolean)
|
| 57 |
+
.slice(0, 2)
|
| 58 |
+
.map((p) => p[0]?.toUpperCase())
|
| 59 |
+
.join("");
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
const PALETTE = [
|
| 63 |
+
["#ffe4e0", "#d9482b"],
|
| 64 |
+
["#e0ecff", "#2f6df0"],
|
| 65 |
+
["#e3f6e8", "#1f8a4c"],
|
| 66 |
+
["#fff1d6", "#b9770e"],
|
| 67 |
+
["#efe3ff", "#7a3fd1"],
|
| 68 |
+
["#dff5f5", "#1a8a8a"],
|
| 69 |
+
["#ffe3f1", "#c93a84"],
|
| 70 |
+
];
|
| 71 |
+
|
| 72 |
+
export function colorFor(str: string) {
|
| 73 |
+
let h = 0;
|
| 74 |
+
for (let i = 0; i < str.length; i++) h = (h * 31 + str.charCodeAt(i)) >>> 0;
|
| 75 |
+
return PALETTE[h % PALETTE.length];
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
export const STATUSES = [
|
| 79 |
+
{ key: "saved", label: "Saved", dot: "#a3a3a3" },
|
| 80 |
+
{ key: "queued", label: "Ready to apply", dot: "#8b5cf6" },
|
| 81 |
+
{ key: "applied", label: "Applied", dot: "#3b82f6" },
|
| 82 |
+
{ key: "interview", label: "Interview", dot: "#f59e0b" },
|
| 83 |
+
{ key: "offer", label: "Offer", dot: "#22c55e" },
|
| 84 |
+
{ key: "rejected", label: "Rejected", dot: "#ef4444" },
|
| 85 |
+
] as const;
|
| 86 |
+
|
| 87 |
+
export type StatusKey = (typeof STATUSES)[number]["key"];
|