From 5067e419545810455c376d391bce319eb3c46136 Mon Sep 17 00:00:00 2001 From: "Daniel S. Billing" Date: Thu, 2 Jan 2025 23:04:17 +0100 Subject: [PATCH] Merge utils into one --- frontend/src/lib/date.ts | 10 --------- frontend/src/lib/utils.ts | 41 ++++++++++++++++++++++++++++++++++ frontend/src/lib/utils/date.ts | 18 --------------- 3 files changed, 41 insertions(+), 28 deletions(-) delete mode 100644 frontend/src/lib/date.ts delete mode 100644 frontend/src/lib/utils/date.ts diff --git a/frontend/src/lib/date.ts b/frontend/src/lib/date.ts deleted file mode 100644 index e81659c..0000000 --- a/frontend/src/lib/date.ts +++ /dev/null @@ -1,10 +0,0 @@ -export function formatDate(dateString: string): string { - const date = new Date(dateString); - return new Intl.DateTimeFormat('en-US', { - year: 'numeric', - month: 'short', - day: 'numeric', - hour: '2-digit', - minute: '2-digit', - }).format(date); -} \ No newline at end of file diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index bd0c391..190ab04 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -1,6 +1,47 @@ +import i18n from '@/i18n'; +import { formatDistanceToNow } from 'date-fns'; +import { enUS, sv, nb } from 'date-fns/locale'; import { clsx, type ClassValue } from "clsx" import { twMerge } from "tailwind-merge" +// Class name utilities export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } + +// String utilities +export function slugify(text: string): string { + return text + .toLowerCase() + .replace(/[^\w\s-]/g, '') // Remove special characters + .replace(/\s+/g, '-') // Replace spaces with hyphens + .replace(/-+/g, '-') // Replace multiple hyphens with single hyphen + .trim(); // Trim whitespace +} + +// Date utilities +const getDateLocale = (language: string) => { + switch (language) { + case 'sv': return sv; + case 'no': return nb; + default: return enUS; + } +}; + +export const formatRelativeTime = (date: Date | string, language: string = i18n.language) => { + return formatDistanceToNow(new Date(date), { + addSuffix: true, + locale: getDateLocale(language) + }); +}; + +export function formatDate(dateString: string): string { + const date = new Date(dateString); + return new Intl.DateTimeFormat(i18n.language, { + year: 'numeric', + month: 'short', + day: 'numeric', + hour: '2-digit', + minute: '2-digit', + }).format(date); +} \ No newline at end of file diff --git a/frontend/src/lib/utils/date.ts b/frontend/src/lib/utils/date.ts deleted file mode 100644 index f4159ee..0000000 --- a/frontend/src/lib/utils/date.ts +++ /dev/null @@ -1,18 +0,0 @@ -import i18n from '@/i18n'; -import { formatDistanceToNow } from 'date-fns'; -import { enUS, sv, nb } from 'date-fns/locale'; - -const getDateLocale = (language: string) => { - switch (language) { - case 'sv': return sv; - case 'no': return nb; - default: return enUS; - } -}; - -export const formatRelativeTime = (date: Date | string, language: string = i18n.language) => { - return formatDistanceToNow(new Date(date), { - addSuffix: true, - locale: getDateLocale(language) - }); -}; \ No newline at end of file