export function formatDate(date, options = {}) {
const defaultOptions = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
return new Intl.DateTimeFormat('en-US', { ...defaultOptions, ...options }).format(
new Date(date)
);
}
export function formatRelativeTime(date) {
const now = new Date();
const then = new Date(date);
const diffInSeconds = Math.floor((now - then) / 1000);
const intervals = [
{ label: 'year', seconds: 31536000 },
{ label: 'month', seconds: 2592000 },
{ label: 'week', seconds: 604800 },
{ label: 'day', seconds: 86400 },
{ label: 'hour', seconds: 3600 },
{ label: 'minute', seconds: 60 },
];
for (const interval of intervals) {
const count = Math.floor(diffInSeconds / interval.seconds);
if (count >= 1) {
return `${count} ${interval.label}${count !== 1 ? 's' : ''} ago`;
}
}
return 'just now';
}
export function truncate(text, maxLength = 100) {
if (!text || text.length <= maxLength) {
return text;
}
return text.slice(0, maxLength).trim() + '...';
}
export function slugify(text) {
return text
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}
export function parseSearchParams(searchParams) {
const params = {};
for (const [key, value] of searchParams.entries()) {
if (params[key]) {
if (Array.isArray(params[key])) {
params[key].push(value);
} else {
params[key] = [params[key], value];
}
} else {
params[key] = value;
}
}
return params;
}