From 6cac0a32bcc99b455209f80c1ad71c381b7c5af9 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 16 Mar 2026 12:15:32 +0100 Subject: [PATCH] feat(07-01): implement formatWeight with WeightUnit parameter - Export WeightUnit type ("g" | "oz" | "lb" | "kg") - Add conversion constants (GRAMS_PER_OZ, GRAMS_PER_LB, GRAMS_PER_KG) - Switch-based formatting: g=0dp, oz=1dp, lb=2dp, kg=2dp - Default parameter "g" preserves backward compatibility - formatPrice left untouched - All 21 tests pass Co-Authored-By: Claude Opus 4.6 --- src/client/lib/formatters.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/client/lib/formatters.ts b/src/client/lib/formatters.ts index 42961ce..bf75747 100644 --- a/src/client/lib/formatters.ts +++ b/src/client/lib/formatters.ts @@ -1,6 +1,24 @@ -export function formatWeight(grams: number | null | undefined): string { +export type WeightUnit = "g" | "oz" | "lb" | "kg"; + +const GRAMS_PER_OZ = 28.3495; +const GRAMS_PER_LB = 453.592; +const GRAMS_PER_KG = 1000; + +export function formatWeight( + grams: number | null | undefined, + unit: WeightUnit = "g", +): string { if (grams == null) return "--"; - return `${Math.round(grams)}g`; + switch (unit) { + case "g": + return `${Math.round(grams)}g`; + case "oz": + return `${(grams / GRAMS_PER_OZ).toFixed(1)} oz`; + case "lb": + return `${(grams / GRAMS_PER_LB).toFixed(2)} lb`; + case "kg": + return `${(grams / GRAMS_PER_KG).toFixed(2)} kg`; + } } export function formatPrice(cents: number | null | undefined): string {