Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/components/avendre/fmt.ts9 * Module : components10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Vrai-Prix — aides de format pour l'onglet « À vendre » (client + serveur).43export const SQFT_M2 = 0.092903;4445export const money = (v: number | null | undefined, lang = "fr") =>46 v == null || !Number.isFinite(v)47 ? "—"48 : new Intl.NumberFormat(lang === "fr" ? "fr-CA" : "en-CA", {49 style: "currency",50 currency: "CAD",51 maximumFractionDigits: 0,52 }).format(v);5354export const signedMoney = (v: number, lang = "fr") =>55 `${v >= 0 ? "+" : "−"}${money(Math.abs(Math.round(v)), lang)}`;5657export const num = (v: number | null | undefined, lang = "fr", maxFrac = 0) =>58 v == null || !Number.isFinite(v)59 ? "—"60 : v.toLocaleString(lang === "fr" ? "fr-CA" : "en-CA", { maximumFractionDigits: maxFrac });6162export const pct = (v: number | null | undefined, lang = "fr", frac = 0, signed = true) => {63 if (v == null || !Number.isFinite(v)) return "—";64 const s = Math.abs(v).toLocaleString(lang === "fr" ? "fr-CA" : "en-CA", { maximumFractionDigits: frac });65 return `${signed ? (v >= 0 ? "+" : "−") : ""}${s} %`;66};6768export const dateFr = (iso: string | null | undefined, lang = "fr") => {69 if (!iso) return "—";70 const d = new Date(iso.length <= 10 ? `${iso}T12:00:00` : iso);71 if (Number.isNaN(d.getTime())) return iso;72 return d.toLocaleDateString(lang === "fr" ? "fr-CA" : "en-CA", { year: "numeric", month: "short", day: "numeric" });73};7475export const epochDate = (ts: number | null | undefined, lang = "fr") =>76 ts ? dateFr(new Date(ts * 1000).toISOString(), lang) : "—";7778/** pi² → m² arrondi (superficie habitable plausible seulement). */79export const sqftToM2 = (sqft: number | null | undefined): number | null =>80 sqft && sqft > 50 ? Math.round(sqft * SQFT_M2) : null;8182export const m2 = (v: number | null | undefined, lang = "fr") => (v == null ? "—" : `${num(v, lang)} m²`);8384/** Ratio estimation / prix demandé → libellé de lecture. */85export function ratioLabel(ratio: number | null | undefined, fr: boolean): { text: string; tone: "up" | "down" | "flat" | "none" } {86 if (ratio == null) return { text: fr ? "Non évaluée" : "Not evaluated", tone: "none" };87 const d = (ratio - 1) * 100;88 if (Math.abs(d) < 5) return { text: fr ? "Prix aligné sur l'estimation" : "Price in line with estimate", tone: "flat" };89 if (d > 0) return { text: fr ? `Estimation ${pct(d, fr ? "fr" : "en")} vs prix` : `Estimate ${pct(d, "en")} vs price`, tone: "up" };90 return { text: fr ? `Estimation ${pct(d, "fr")} vs prix` : `Estimate ${pct(d, "en")} vs price`, tone: "down" };91}9293export const median = (xs: number[]): number | null => {94 if (!xs.length) return null;95 const s = [...xs].sort((a, b) => a - b);96 const m = Math.floor(s.length / 2);97 return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;98};99100export const mean = (xs: number[]): number | null => (xs.length ? xs.reduce((a, b) => a + b, 0) / xs.length : null);101102export function weightedMean(values: number[], weights: number[]): number | null {103 const tw = weights.reduce((a, b) => a + b, 0);104 if (!tw) return null;105 return values.reduce((s, v, i) => s + v * weights[i], 0) / tw;106}107108export function weightedMedian(values: number[], weights: number[]): number | null {109 const order = values.map((v, i) => ({ v, w: weights[i] })).sort((a, b) => a.v - b.v);110 const total = order.reduce((s, o) => s + o.w, 0);111 if (total <= 0) return null;112 let acc = 0;113 for (const o of order) {114 acc += o.w;115 if (acc >= total / 2) return o.v;116 }117 return order[order.length - 1]?.v ?? null;118}119120export const round100 = (v: number) => Math.round(v / 100) * 100;121