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 {