Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/llms-txt-hub / apps/web/lib/utils.ts |
| Date | Not specified in source |
| Repository | Janady13/llms-txt-hub |
| Revision | e0f4bf978d4df168369c7601d9c5f5ea89e84377 |
| Branch | main |
| Path or archive member | apps/web/lib/utils.ts |
| Original SHA-256 | 8c88e591e1047199fb5d07857c5917fa1b6871103465815a1dcd51aaa8f52b07 |
| Git blob SHA | 5657ceab41ef1943e9d68f3daf93101e84c66bb3 |
| Reading-copy SHA-256 | 8c88e591e1047199fb5d07857c5917fa1b6871103465815a1dcd51aaa8f52b07 |
Recovered source transcript
Download complete source text ↓68 lines · 2.1 KB
import { type ClassValue, clsx } from 'clsx'
import { format, formatDistanceToNow } from 'date-fns'
import { twMerge } from 'tailwind-merge'
/**
* Combines class names using clsx and tailwind-merge
*/
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
/**
* Format a date string to a human-readable format
*/
export function formatDate(dateString: string): string {
const date = new Date(dateString)
if (Number.isNaN(date.getTime())) {
return 'Invalid date'
}
return format(date, 'MMMM d, yyyy')
}
/**
* Formats a date string to human-readable relative time using formatDistanceToNow.
* Returns 'Invalid date' for invalid input, preserving the current behavior.
*
* @param dateString - String representation of a date
* @returns string - Human-readable relative time (e.g., "2 days ago") or 'Invalid date' for invalid input
*/
export function formatRelativeDate(dateString: string): string {
const date = new Date(dateString)
if (Number.isNaN(date.getTime())) {
return 'Invalid date'
}
return formatDistanceToNow(date, { addSuffix: true })
}
/**
* Extracts the hostname from a URL string, removing the 'www.' prefix if present.
* Returns an empty string for invalid URLs or parse failures.
*
* @param url - The input URL or string to parse
* @returns string - The extracted domain (without 'www.') or empty string on parse failure
*/
export function extractDomain(url: string): string {
try {
const urlObj = new URL(url)
return urlObj.hostname.replace('www.', '')
} catch {
return ''
}
}
/**
* Strips HTML tags from a string to create safe plain text
* Useful for sanitizing descriptions that may contain raw HTML
*
* @param html - The HTML string to sanitize
* @returns Plain text with HTML tags removed
*/
export function stripHtmlTags(html: string | null | undefined): string {
if (!html) return ''
return html.replace(/<[^>]*>/g, '').trim()
}
// Server-only utilities have been moved to lib/server-utils.ts
// This file should only contain isomorphic utilities that work on both client and server