diff options
| author | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-03-26 00:37:41 +0000 |
|---|---|---|
| committer | dujinkim <dujin.kim@dtsolution.co.kr> | 2025-03-26 00:37:41 +0000 |
| commit | e0dfb55c5457aec489fc084c4567e791b4c65eb1 (patch) | |
| tree | 68543a65d88f5afb3a0202925804103daa91bc6f /lib/utils.ts | |
3/25 까지의 대표님 작업사항
Diffstat (limited to 'lib/utils.ts')
| -rw-r--r-- | lib/utils.ts | 75 |
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) + } + } +} |
