SPB Git

Repository Public

format.ts 1.6 KB 39 lines typescript

1// Auteur : Simon-Pierre Boucher — contact@spboucher.ai2// Panel /admin — petits utilitaires d'affichage partagés (server-safe) :3// heures relatives fr, initiales et teinte stable d'un correspondant.45/** « il y a 4 min », « hier 14:02 », « 12 août »… */6export function timeAgo(iso: string): string {7  const d = new Date(iso.replace(" ", "T") + (iso.includes("Z") ? "" : "Z"));8  const s = Math.max(0, (Date.now() - d.getTime()) / 1000);9  if (s < 60) return "à l'instant";10  if (s < 3600) return `il y a ${Math.floor(s / 60)} min`;11  if (s < 86400) return `il y a ${Math.floor(s / 3600)} h`;12  if (s < 172800) return `hier ${iso.slice(11, 16)}`;13  if (s < 30 * 86400) return `il y a ${Math.floor(s / 86400)} j`;14  return iso.slice(0, 10);15}1617export function fmtDateFull(iso: string): string {18  return iso.replace("T", " ").slice(0, 16);19}2021/** Initiale(s) d'un correspondant — « Jean Client » → JC, « a@b.co » → A. */22export function initialsOf(name: string | null, email: string): string {23  const src = (name ?? "").trim() || email;24  const words = src.split(/[\s.@_-]+/).filter(Boolean);25  const chars = words.slice(0, 2).map((w) => w[0]?.toUpperCase() ?? "");26  return chars.join("") || "?";27}2829/** Teinte pastel stable dérivée d'une chaîne (fond d'avatar). */30export function hueOf(key: string): number {31  let h = 0;32  for (let i = 0; i < key.length; i++) h = (h * 31 + key.charCodeAt(i)) % 360;33  return h;34}3536export function avatarStyle(key: string): { background: string; color: string } {37  return { background: `hsl(${hueOf(key)} 55% 86%)`, color: "var(--ink)" };38}39
Files main
Menu

Theme