Files
mtusi2026lab4/frontend/src/lib/format.ts
T
2026-10-28 12:40:39 +03:00

59 lines
1.5 KiB
TypeScript

const timeFormatter = new Intl.DateTimeFormat("ru-RU", {
hour: "2-digit",
minute: "2-digit",
});
const dayFormatter = new Intl.DateTimeFormat("ru-RU", {
day: "numeric",
month: "long",
});
const fullDateFormatter = new Intl.DateTimeFormat("ru-RU", {
day: "numeric",
month: "long",
year: "numeric",
});
export function formatMessageTime(value: string): string {
return timeFormatter.format(new Date(value));
}
export function formatDay(value: string): string {
const date = new Date(value);
const now = new Date();
const sameYear = date.getFullYear() === now.getFullYear();
return (sameYear ? dayFormatter : fullDateFormatter).format(date);
}
export function dayKey(value: string): string {
const date = new Date(value);
return [date.getFullYear(), date.getMonth(), date.getDate()].join("-");
}
export function formatMemberCount(count: number): string {
const mod10 = count % 10;
const mod100 = count % 100;
if (mod10 === 1 && mod100 !== 11) {
return `${count} участник`;
}
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) {
return `${count} участника`;
}
return `${count} участников`;
}
export function shortID(id: string): string {
return id.length > 12 ? `${id.slice(0, 8)}…` : id;
}
export function initials(value: string): string {
const parts = value.trim().split(/\s+/).filter(Boolean);
if (parts.length === 0) {
return "?";
}
return parts
.slice(0, 2)
.map((part) => Array.from(part)[0]?.toUpperCase() ?? "")
.join("");
}