30 lines
1009 B
TypeScript
30 lines
1009 B
TypeScript
import { clsx, type ClassValue } from "clsx";
|
||
import { twMerge } from "tailwind-merge";
|
||
|
||
/** 合并 className:clsx 处理条件类,tailwind-merge 消解冲突的 Tailwind 类 */
|
||
export function cn(...inputs: ClassValue[]): string {
|
||
return twMerge(clsx(inputs));
|
||
}
|
||
|
||
/** 将日期格式化为中文可读形式,如 2025 年 3 月 8 日 */
|
||
export function formatDate(date: string, locale = "zh-CN"): string {
|
||
return new Intl.DateTimeFormat(locale, {
|
||
year: "numeric",
|
||
month: "long",
|
||
day: "numeric",
|
||
timeZone: "UTC",
|
||
}).format(new Date(`${date}T00:00:00Z`));
|
||
}
|
||
|
||
/** 机器可读日期,用于 <time dateTime> */
|
||
export function toDateTimeAttribute(date: string): string {
|
||
return new Date(`${date}T00:00:00Z`).toISOString();
|
||
}
|
||
|
||
/** 拼接绝对 URL,用于 canonical / OG / sitemap */
|
||
export function absoluteUrl(base: string, pathname: string): string {
|
||
const base$ = base.replace(/\/+$/, "");
|
||
const path$ = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
||
return `${base$}${path$}`;
|
||
}
|