Repository Public
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 vente : 489 000 → « 489 000 $ » ; valeurs aberrantes (0 $, 1 $) → « Prix sur demande » */13export const fmtPrice = (n: number | null | undefined) => (n == null || n < 1000 ? "Prix sur demande" : `${nf.format(Math.round(n))} $`);1415const nf2 = new Intl.NumberFormat("fr-CA", { maximumFractionDigits: 2 });16/** Prix de pastille carte : 489 000 → « 489 k », 1 250 000 → « 1,25 M » */17export const fmtShortPrice = (n: number | null | undefined) =>18 n == null || n < 1000 ? "•" : n < 1e6 ? `${Math.round(n / 1000)} k` : `${nf2.format(Math.round(n / 1e4) / 100)} M`;1920/** -0,167 → « −17 % » */21export function fmtPct(x: number | null | undefined, signed = true): string {22 if (x == null) return "—";23 const v = Math.round(x * 100);24 const sign = !signed || v === 0 ? "" : v > 0 ? "+" : "−";25 return `${sign}${Math.abs(v)} %`;26}2728/** 250 → « 250 m », 1240 → « 1,2 km » */29export const fmtDist = (m: number | null | undefined) =>30 m == null ? "—" : m < 1000 ? `${Math.round(m / 10) * 10} m` : `${nf1.format(m / 1000)} km`;3132/** Horodatage Unix (s) → « il y a 3 h » */33export function fmtAgo(ts: number | null | undefined): string {34 if (!ts) return "—";35 const s = Math.max(0, Date.now() / 1000 - ts);36 if (s < 90) return "à l'instant";37 if (s < 3600) return `il y a ${Math.round(s / 60)} min`;38 if (s < 86400) return `il y a ${Math.round(s / 3600)} h`;39 const d = Math.round(s / 86400);40 if (d < 31) return `il y a ${d} j`;41 if (d < 365) return `il y a ${Math.round(d / 30)} mois`;42 return `il y a ${Math.round(d / 365)} an${d >= 730 ? "s" : ""}`;43}4445/** Horodatage Unix (s) → « 25 novembre 2026 » */46export const fmtDate = (ts: number | null | undefined) =>47 ts ? new Date(ts * 1000).toLocaleDateString("fr-CA", { day: "numeric", month: "long", year: "numeric" }) : "—";4849/** "now" → « Maintenant », "2026-12-01" → « 1ᵉʳ décembre » */50export function fmtAvailability(iso: string | null | undefined, withYear = false): string | null {51 if (!iso) return null;52 if (iso === "now") return "Maintenant";53 const [y, m, d] = iso.split("-").map(Number);54 if (!y || !m || !d) return null;55 const date = new Date(y, m - 1, d);56 if (date.getTime() < Date.now()) return "Maintenant";57 return date58 .toLocaleDateString("fr-CA", { day: "numeric", month: "long", ...(withYear ? { year: "numeric" } : {}) })59 .replace(/^1 /, "1ᵉʳ ");60}6162/** « 4½ » → « 4½ · 2 chambres » quand le nombre de chambres est connu. */63export function unitLabel(unit: string | null | undefined, bedrooms?: number | null): string {64 const u = (unit || "").trim();65 if (!u) return bedrooms ? `${bedrooms} ch.` : "Propriété";66 return u;67}6869export const plural = (n: number, one: string, many: string) => `${fmtInt(n)} ${n > 1 ? many : one}`;7071/** Slug identique à celui du serveur (seo.slugify). */72export function slugify(text: string): string {73 return text74 .replace(/½/g, "-1-2")75 .replace(/\+/g, "-plus")76 .replace(/œ/g, "oe")77 .replace(/Œ/g, "Oe")78 .normalize("NFKD")79 .replace(/[̀-ͯ]/g, "")80 .toLowerCase()81 .replace(/[^a-z0-9]+/g, "-")82 .replace(/^-+|-+$/g, "");83}8485/** Nettoie les libellés de ville mal formés publiés par certaines sources. */86export const cleanCity = (c: string | null | undefined) => (c || "").replace(/[()]/g, "").trim();87