| 1 | /** biome-ignore-all lint/suspicious/noBitwiseOperators: Need it for color magic */ |
| 2 | import { type ClassValue, |
| 3 | import slugify from 'slugify |
| 4 | import { twMerge } |
| 5 | import { slugAlphabet } |
| 6 | |
| 7 | export function cn(... |
| 8 | return twMerge(clsx |
| 9 | } |
| 10 | |
| 11 | export function titleToSlug(title |
| 12 | const slugified = |
| 13 | const slugifiedWithSuffix = |
| 14 | return { |
| 15 | slugified, |
| 16 | slugifiedWithSuffix, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | export function seedToColor(seed |
| 21 | let hash = |
| 22 | for (let i |
| 23 | hash = seed. |
| 24 | } |
| 25 | |
| 26 | const h = |
| 27 | const s = |
| 28 | const l = |
| 29 | |
| 30 | // HSL to hex |
| 31 | const lNorm = |
| 32 | const sNorm = |
| 33 | const a = |
| 34 | const f = |
| 35 | const k = |
| 36 | const color = |
| 37 | return Math.round |
| 38 | .toString(16) |
| 39 | .padStart(2, ' |
| 40 | } |
| 41 | |
| 42 | return `#${f |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Format a genuine moment (a UTC instant) as a date in the viewer's timezone. |
| 47 | * Pass the viewer's IANA zone; falls back to the runtime zone when omitted. |
| 48 | */ |
| 49 | export function formatDate(date |
| 50 | return date.toLocaleDateString |
| 51 | month: 'short', |
| 52 | day: 'numeric', |
| 53 | year: 'numeric', |
| 54 | timeZone, |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | /** Today as a `'YYYY-MM-DD'` calendar-date string in the given IANA zone (UTC by default). */ |
| 59 | export function todayDateOnly(timeZone |
| 60 | return new Intl |
| 61 | timeZone, |
| 62 | year: 'numeric', |
| 63 | month: '2-digit', |
| 64 | day: '2-digit', |
| 65 | }).format(new |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Format a calendar date stored as a zoneless `'YYYY-MM-DD'` string. The same |
| 70 | * literal day for every viewer — never shifted by a timezone. |
| 71 | */ |
| 72 | export function formatDateOnly(value |
| 73 | const match = |
| 74 | if (!match) |
| 75 | return value |
| 76 | } |
| 77 | const y = |
| 78 | const m = |
| 79 | const d = |
| 80 | const date = |
| 81 | if ( |
| 82 | date.getFullYear() !== |
| 83 | date.getMonth() !== |
| 84 | date.getDate() !== |
| 85 | ) { |
| 86 | return value |
| 87 | } |
| 88 | return formatDate(date |
| 89 | } |
| 90 | |
| 91 | /** Native `<input type="date">` only accepts a `yyyy-MM-dd` value. */ |
| 92 | export function toDateInputValue(value |
| 93 | // Already a date-only string: return as-is so UTC parsing doesn't shift it. |
| 94 | if (/^\d{4}-\d |
| 95 | return value |
| 96 | } |
| 97 | const date = |
| 98 | if (Number.isNaN |
| 99 | return '' |
| 100 | } |
| 101 | const offset = |
| 102 | return new Date |
| 103 | } |
| 104 | |
| 105 | export function stripHtml(html |
| 106 | return html |
| 107 | .replace(/<[^ |
| 108 | .replace(/\s+/ |
| 109 | .trim() |
| 110 | } |