All files / src/lib format.ts

95.69% Statements 89/93
96.15% Branches 50/52
100% Functions 8/8
95.69% Lines 89/93

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 1011x 678x 678x 678x 3x 3x 678x 318x 318x 642x 48x 48x 309x 309x   1x 722x 722x 56x 56x 666x 722x 323x 323x 343x 343x 356x 339x 339x 339x 4x 4x 4x 722x 722x   1x 114x 114x   1x 443x 1x 1x 442x 443x     442x 442x 442x 442x 442x 442x 443x 443x 443x   1x 432x 3x 3x 429x 432x 1x 1x 428x 432x 12x 12x 416x 416x 432x 432x   1x 17x 15x 15x 2x 3x     2x 3x 1x 1x 1x 1x   1x 41x 1x 1x 40x 40x   1x 36x 36x  
export function pluralRu(count: number, forms: [string, string, string]): string {
  const absolute = Math.abs(count) % 100;
  const last = absolute % 10;
  if (absolute > 10 && absolute < 20) {
    return forms[2];
  }
  if (last > 1 && last < 5) {
    return forms[1];
  }
  if (last === 1) {
    return forms[0];
  }
  return forms[2];
}
 
export function formatDuration(totalSeconds: number): string {
  const seconds = Math.max(0, Math.round(totalSeconds));
  if (seconds < 60) {
    return `${seconds} с`;
  }
  const minutes = Math.floor(seconds / 60);
  if (minutes < 60) {
    return `${minutes} ${pluralRu(minutes, ["минута", "минуты", "минут"])}`;
  }
  const hours = Math.floor(minutes / 60);
  const restMinutes = minutes % 60;
  if (hours < 24) {
    const hourText = `${hours} ${pluralRu(hours, ["час", "часа", "часов"])}`;
    return restMinutes > 0 ? `${hourText} ${restMinutes} мин` : hourText;
  }
  const days = Math.floor(hours / 24);
  const restHours = hours % 24;
  const dayText = `${days} ${pluralRu(days, ["день", "дня", "дней"])}`;
  return restHours > 0 ? `${dayText} ${restHours} ч` : dayText;
}
 
export function formatNumber(value: number): string {
  return new Intl.NumberFormat("ru-RU").format(value);
}
 
export function formatDateTime(iso: string | null | undefined, timeZone?: string): string {
  if (!iso) {
    return "—";
  }
  const date = new Date(iso);
  if (Number.isNaN(date.getTime())) {
    return "—";
  }
  return new Intl.DateTimeFormat("ru-RU", {
    day: "2-digit",
    month: "2-digit",
    year: "numeric",
    hour: "2-digit",
    minute: "2-digit",
    ...(timeZone ? { timeZone } : {}),
  }).format(date);
}
 
export function formatRelative(iso: string | null | undefined, now: Date = new Date()): string {
  if (!iso) {
    return "—";
  }
  const date = new Date(iso);
  if (Number.isNaN(date.getTime())) {
    return "—";
  }
  const deltaSeconds = Math.round((now.getTime() - date.getTime()) / 1000);
  if (Math.abs(deltaSeconds) < 10) {
    return "только что";
  }
  const future = deltaSeconds < 0;
  const text = formatDuration(Math.abs(deltaSeconds));
  return future ? `через ${text}` : `${text} назад`;
}
 
export function formatLease(leaseExpiresAt: string | null, now: Date = new Date()): string {
  if (!leaseExpiresAt) {
    return "аренда не выдана";
  }
  const date = new Date(leaseExpiresAt);
  if (Number.isNaN(date.getTime())) {
    return "аренда не выдана";
  }
  const deltaSeconds = Math.round((date.getTime() - now.getTime()) / 1000);
  if (deltaSeconds <= 0) {
    return `просрочена на ${formatDuration(Math.abs(deltaSeconds))}`;
  }
  return `истекает через ${formatDuration(deltaSeconds)}`;
}
 
export function percent(part: number, total: number): number {
  if (total <= 0) {
    return 0;
  }
  return Math.round((part / total) * 1000) / 10;
}
 
export function formatPercent(part: number, total: number): string {
  return `${percent(part, total).toFixed(1)} %`;
}