summaryrefslogtreecommitdiff
path: root/lib/utils.ts
diff options
context:
space:
mode:
authorjoonhoekim <26rote@gmail.com>2025-03-25 15:55:45 +0900
committerjoonhoekim <26rote@gmail.com>2025-03-25 15:55:45 +0900
commit1a2241c40e10193c5ff7008a7b7b36cc1d855d96 (patch)
tree8a5587f10ca55b162d7e3254cb088b323a34c41b /lib/utils.ts
initial commit
Diffstat (limited to 'lib/utils.ts')
-rw-r--r--lib/utils.ts75
1 files changed, 75 insertions, 0 deletions
diff --git a/lib/utils.ts b/lib/utils.ts
new file mode 100644
index 00000000..2eca9285
--- /dev/null
+++ b/lib/utils.ts
@@ -0,0 +1,75 @@
+import { clsx, type ClassValue } from "clsx"
+import { twMerge } from "tailwind-merge"
+
+export function cn(...inputs: ClassValue[]) {
+ return twMerge(clsx(inputs))
+}
+
+export function formatDate(
+ date: Date | string | number,
+ locale: string = "en-US",
+ opts: Intl.DateTimeFormatOptions = {},
+ includeTime: boolean = false
+) {
+ return new Intl.DateTimeFormat(locale, {
+ month: opts.month ?? "long",
+ day: opts.day ?? "numeric",
+ year: opts.year ?? "numeric",
+ // Add time options when includeTime is true
+ ...(includeTime && {
+ hour: opts.hour ?? "2-digit",
+ minute: opts.minute ?? "2-digit",
+ second: opts.second ?? "2-digit",
+ hour12: opts.hour12 ?? false, // Use 24-hour format by default
+ }),
+ ...opts, // This allows overriding any of the above defaults
+ }).format(new Date(date))
+}
+
+// Alternative: Create a separate function for date and time
+export function formatDateTime(
+ date: Date | string | number,
+ locale: string = "en-US",
+ opts: Intl.DateTimeFormatOptions = {}
+) {
+ return new Intl.DateTimeFormat(locale, {
+ month: opts.month ?? "long",
+ day: opts.day ?? "numeric",
+ year: opts.year ?? "numeric",
+ hour: opts.hour ?? "2-digit",
+ minute: opts.minute ?? "2-digit",
+ second: opts.second ?? "2-digit",
+ hour12: opts.hour12 ?? false,
+ ...opts,
+ }).format(new Date(date))
+}
+
+export function toSentenceCase(str: string) {
+ return str
+ .replace(/_/g, " ")
+ .replace(/([A-Z])/g, " $1")
+ .toLowerCase()
+ .replace(/^\w/, (c) => c.toUpperCase())
+ .replace(/\s+/g, " ")
+ .trim()
+}
+
+/**
+ * @see https://github.com/radix-ui/primitives/blob/main/packages/core/primitive/src/primitive.tsx
+ */
+export function composeEventHandlers<E>(
+ originalEventHandler?: (event: E) => void,
+ ourEventHandler?: (event: E) => void,
+ { checkForDefaultPrevented = true } = {}
+) {
+ return function handleEvent(event: E) {
+ originalEventHandler?.(event)
+
+ if (
+ checkForDefaultPrevented === false ||
+ !(event as unknown as Event).defaultPrevented
+ ) {
+ return ourEventHandler?.(event)
+ }
+ }
+}