66 行
1.9 KiB
TypeScript
66 行
1.9 KiB
TypeScript
export function formatDisplayDate(value: string | null | undefined) {
|
|
if (!value) return "-";
|
|
|
|
const match = /^(\d{4})-(\d{2})-(\d{2})/.exec(value);
|
|
if (match) {
|
|
const [, year, month, day] = match;
|
|
return `${Number(year)}年${Number(month)}月${Number(day)}日`;
|
|
}
|
|
|
|
const timestamp = new Date(value).getTime();
|
|
if (Number.isNaN(timestamp)) return "-";
|
|
return new Intl.DateTimeFormat("zh-CN", {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
}).format(timestamp);
|
|
}
|
|
|
|
export function formatDisplayDateTime(value: string | null | undefined) {
|
|
if (!value) return "-";
|
|
|
|
const timestamp = new Date(value).getTime();
|
|
if (Number.isNaN(timestamp)) return formatDisplayDate(value);
|
|
|
|
return new Intl.DateTimeFormat("zh-CN", {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
}).format(timestamp);
|
|
}
|
|
|
|
export function formatCompactIdentifier(
|
|
value: string | number | null | undefined,
|
|
options: { head?: number; tail?: number; minLength?: number } = {},
|
|
) {
|
|
const text = String(value ?? "").trim();
|
|
if (!text) return "-";
|
|
|
|
const head = options.head ?? 10;
|
|
const tail = options.tail ?? 4;
|
|
const minLength = options.minLength ?? 18;
|
|
|
|
if (text.length <= minLength || text.length <= head + tail + 1) {
|
|
return text;
|
|
}
|
|
|
|
return `${text.slice(0, head)}…${text.slice(-tail)}`;
|
|
}
|
|
|
|
export function isCompactedIdentifier(value: string | number | null | undefined, displayValue: string) {
|
|
const text = String(value ?? "").trim();
|
|
return Boolean(text) && text !== displayValue;
|
|
}
|
|
|
|
export function formatTextWithCompactLongNumbers(value: string | number | null | undefined) {
|
|
const text = String(value ?? "").trim();
|
|
if (!text) return "-";
|
|
|
|
return text.replace(/\b20[0-9]{12,}\b/g, (match) => {
|
|
if (match.length <= 12) return match;
|
|
return `${match.slice(0, 8)}…${match.slice(-4)}`;
|
|
});
|
|
}
|