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 <noreply@anthropic.com>
This commit is contained in:
@@ -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 "--";
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user