Repository Public
1//2// Fabri-Ka iOS v23// File: FabriKa/Core/Util/Format.swift4// Description: écriture des prix, des nombres et des dates en français du Québec5// (« 10,00 $ », « de 7 $ à 740 $ », « 333 918 produits », « 26 novembre 2026 », « 24 nov. »).6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import Foundation1213enum Fmt {14 /// espace insécable (entre le nombre et « $ » ou « % »)15 static let nbsp = "\u{00A0}"16 /// espace fine insécable (milliers)17 static let thin = "\u{202F}"18 /// vrai signe moins19 static let minus = "\u{2212}"2021 private static let locale = Locale(identifier: "fr_CA")22 private static let zone = TimeZone(identifier: "America/Toronto") ?? .current2324 private static func decimal(_ v: Double, _ digits: Int) -> String {25 let neg = v < 026 let a = abs(v)27 let p = pow(10.0, Double(digits))28 let r = (a * p).rounded() / p29 let whole = Int(r.rounded(.down))30 var s = group(whole)31 if digits > 0 {32 let frac = Int(((r - Double(whole)) * p).rounded())33 let f = String(frac)34 s += "," + String(repeating: "0", count: max(0, digits - f.count)) + f35 }36 return (neg && r != 0 ? minus : "") + s37 }3839 /// Milliers séparés par une espace fine insécable : « 1 386 », « 333 918 ».40 static func group(_ n: Int) -> String {41 let s = String(abs(n))42 guard s.count > 3 else { return (n < 0 ? minus : "") + s }43 var out = ""44 for (i, c) in s.reversed().enumerated() {45 if i > 0, i % 3 == 0 { out.append(thin) }46 out.append(c)47 }48 return (n < 0 ? minus : "") + String(out.reversed())49 }5051 static func count(_ n: Int?) -> String { n.map(group) ?? "—" }5253 /// « 10,00 $ » ; « Prix sur demande » quand la boutique ne publie pas de prix.54 static func price(_ v: Double?) -> String {55 guard let v else { return "Prix sur demande" }56 return decimal(v, 2) + nbsp + "$"57 }5859 /// Prix sans décimales inutiles : « 7 $ », « 12,50 $ » (fourchettes, statistiques).60 static func shortPrice(_ v: Double?) -> String {61 guard let v else { return "—" }62 let cents = (v * 100).rounded()63 return decimal(v, cents.truncatingRemainder(dividingBy: 100) == 0 ? 0 : 2) + nbsp + "$"64 }6566 /// Prix d'une carte ou d'une fiche : « 10,00 $ », ou « de 39,50 $ à 192 $ » quand les variantes diffèrent.67 static func priceRange(_ low: Double?, _ high: Double?) -> String {68 guard let low else { return price(nil) }69 guard let high, high > low + 0.004 else { return price(low) }70 return "de " + shortPrice(low) + " à " + shortPrice(high)71 }7273 /// Fourchette d'un catalogue : « de 7 $ à 740 $ ».74 static func span(_ low: Double?, _ high: Double?) -> String? {75 guard let low, let high else { return nil }76 if abs(high - low) < 0.005 { return shortPrice(low) }77 return "de " + shortPrice(low) + " à " + shortPrice(high)78 }7980 /// Pourcentage : « 22 % », « 46,9 % » avec `digits`.81 static func pct(_ v: Double?, digits: Int = 0) -> String {82 guard let v else { return "—" }83 return decimal(v, digits) + nbsp + "%"84 }8586 /// Nombre à décimales fixes : « 3,5 ».87 static func number(_ v: Double?, digits: Int = 1) -> String {88 guard let v else { return "—" }89 return decimal(v, digits)90 }9192 /// « 1 produit », « 64 produits », « 333 918 produits ».93 static func plural(_ n: Int, _ one: String, _ many: String) -> String {94 group(n) + " " + (n < 2 ? one : many)95 }9697 static func products(_ n: Int) -> String { plural(n, "produit", "produits") }98 static func stores(_ n: Int) -> String { plural(n, "boutique", "boutiques") }99100 /// Distance : « à 6 km », « à moins de 1 km ».101 static func distance(_ km: Double?) -> String? {102 guard let km else { return nil }103 if km < 1 { return "à moins de 1 km" }104 return "à " + group(Int(km.rounded())) + " km"105 }106107 // --- Dates -------------------------------------------------------------------------------108109 private static func formatter(_ template: String) -> DateFormatter {110 let f = DateFormatter()111 f.locale = locale112 f.timeZone = zone113 f.dateFormat = template114 return f115 }116117 private nonisolated(unsafe) static let longF = formatter("d MMMM yyyy")118 private nonisolated(unsafe) static let shortF = formatter("d MMM")119 private nonisolated(unsafe) static let monthF = formatter("MMMM yyyy")120 private static let lock = NSLock()121122 private static func fix(_ s: String) -> String {123 // « 1 novembre » → « 1er novembre »124 s.hasPrefix("1 ") ? "1er " + s.dropFirst(2) : s125 }126127 /// « 26 novembre 2026 »128 static func date(_ t: Double?) -> String {129 guard let t else { return "—" }130 lock.lock(); defer { lock.unlock() }131 return fix(longF.string(from: Date(timeIntervalSince1970: t)))132 }133134 /// « 24 nov. »135 static func shortDate(_ t: Double?) -> String {136 guard let t else { return "—" }137 lock.lock(); defer { lock.unlock() }138 return fix(shortF.string(from: Date(timeIntervalSince1970: t)))139 }140141 /// « août 2026 »142 static func month(_ t: Double?) -> String {143 guard let t else { return "—" }144 lock.lock(); defer { lock.unlock() }145 return monthF.string(from: Date(timeIntervalSince1970: t))146 }147148 /// Ancienneté d'un relevé : « à l'instant », « il y a 12 min », « il y a 3 h », « il y a 2 jours ».149 static func ago(_ t: Double?, now: Date = Date()) -> String? {150 guard let t else { return nil }151 let s = max(0, now.timeIntervalSince1970 - t)152 if s < 90 { return "à l'instant" }153 if s < 3600 { return "il y a \(Int(s / 60)) min" }154 if s < 36 * 3600 { return "il y a \(Int((s / 3600).rounded())) h" }155 return "il y a \(Int((s / 86400).rounded())) jours"156 }157158 /// Nom d'hôte lisible d'une adresse : « tranchedebois.com ».159 static func host(_ url: String?) -> String? {160 guard let url, let h = URL(string: url)?.host() else { return nil }161 return h.hasPrefix("www.") ? String(h.dropFirst(4)) : h162 }163}164