SPB Git

Repository Public

format.ts 3.3 KB 83 lines typescript

1// Formats d'affichage (français du Québec).23const nf = new Intl.NumberFormat("fr-CA", { maximumFractionDigits: 0 });4const nf1 = new Intl.NumberFormat("fr-CA", { maximumFractionDigits: 1 });56/** 1 234 */7export const fmtInt = (n: number | null | undefined) => (n == null ? "—" : nf.format(n));89/** 1 234 $ */10export const fmtMoney = (n: number | null | undefined) => (n == null ? "—" : `${nf.format(n)} $`);1112/** Prix de pastille carte : 950 → « 950 », 1 695 → « 1,7 k » */13export const fmtShortPrice = (n: number | null | undefined) =>14  n == null ? "•" : n < 1000 ? String(Math.round(n)) : `${nf1.format(n / 1000)} k`;1516/** -0,167 → « −17 % » */17export function fmtPct(x: number | null | undefined, signed = true): string {18  if (x == null) return "—";19  const v = Math.round(x * 100);20  const sign = !signed || v === 0 ? "" : v > 0 ? "+" : "−";21  return `${sign}${Math.abs(v)} %`;22}2324/** 250 → « 250 m », 1240 → « 1,2 km » */25export const fmtDist = (m: number | null | undefined) =>26  m == null ? "—" : m < 1000 ? `${Math.round(m / 10) * 10} m` : `${nf1.format(m / 1000)} km`;2728/** Horodatage Unix (s) → « il y a 3 h » */29export function fmtAgo(ts: number | null | undefined): string {30  if (!ts) return "—";31  const s = Math.max(0, Date.now() / 1000 - ts);32  if (s < 90) return "à l'instant";33  if (s < 3600) return `il y a ${Math.round(s / 60)} min`;34  if (s < 86400) return `il y a ${Math.round(s / 3600)} h`;35  const d = Math.round(s / 86400);36  if (d < 31) return `il y a ${d} j`;37  if (d < 365) return `il y a ${Math.round(d / 30)} mois`;38  return `il y a ${Math.round(d / 365)} an${d >= 730 ? "s" : ""}`;39}4041/** Horodatage Unix (s) → « 25 novembre 2026 » */42export const fmtDate = (ts: number | null | undefined) =>43  ts ? new Date(ts * 1000).toLocaleDateString("fr-CA", { day: "numeric", month: "long", year: "numeric" }) : "—";4445/** "now" → « Maintenant », "2026-12-01" → « 1ᵉʳ décembre » */46export function fmtAvailability(iso: string | null | undefined, withYear = false): string | null {47  if (!iso) return null;48  if (iso === "now") return "Maintenant";49  const [y, m, d] = iso.split("-").map(Number);50  if (!y || !m || !d) return null;51  const date = new Date(y, m - 1, d);52  if (date.getTime() < Date.now()) return "Maintenant";53  return date54    .toLocaleDateString("fr-CA", { day: "numeric", month: "long", ...(withYear ? { year: "numeric" } : {}) })55    .replace(/^1 /, "1ᵉʳ ");56}5758/** « 4½ » → « 4½ · 2 chambres » quand le nombre de chambres est connu. */59export function unitLabel(unit: string | null | undefined, bedrooms?: number | null): string {60  const u = (unit || "").trim();61  if (!u) return bedrooms ? `${bedrooms} ch.` : "Logement";62  return u;63}6465export const plural = (n: number, one: string, many: string) => `${fmtInt(n)} ${n > 1 ? many : one}`;6667/** Slug identique à celui du serveur (seo.slugify). */68export function slugify(text: string): string {69  return text70    .replace(/½/g, "-1-2")71    .replace(/\+/g, "-plus")72    .replace(/œ/g, "oe")73    .replace(/Œ/g, "Oe")74    .normalize("NFKD")75    .replace(/[̀-ͯ]/g, "")76    .toLowerCase()77    .replace(/[^a-z0-9]+/g, "-")78    .replace(/^-+|-+$/g, "");79}8081/** Nettoie les libellés de ville mal formés publiés par certaines sources. */82export const cleanCity = (c: string | null | undefined) => (c || "").replace(/[()]/g, "").trim();83
Files main
Menu

Theme