diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx index 384750d..f71968d 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx @@ -37,6 +37,7 @@ import { } from '../recipes/recipeApi'; import { useRecipeQuery } from '../recipes/useRecipeQuery'; import RecipeQueryStatus from '../recipes/RecipeQueryStatus'; +import { formatAmount, scaleAmount } from '../../lib/recipeScaling'; import './RecipeDetailPage.css'; const MIN_SERVINGS = 1; @@ -64,26 +65,13 @@ function formatTagLabel(tag: string) { .join(' '); } +// Scaling and rounding rules live in lib/recipeScaling.ts (unit-tested). function formatScaledAmount( amount: number, selectedServings: number, originalServings: number, ) { - const scaledAmount = amount * (selectedServings / originalServings); - - if (scaledAmount >= 200) { - return String(Math.round(scaledAmount / 5) * 5); - } - - if (scaledAmount >= 50) { - return String(Math.round(scaledAmount)); - } - - if (scaledAmount >= 3) { - return String(Math.round(scaledAmount * 10) / 10); - } - - return String(Math.round(scaledAmount * 100) / 100); + return formatAmount(scaleAmount(amount, selectedServings, originalServings)); } function getSourcePath(locationState: unknown) { diff --git a/apps/web/src/lib/recipeScaling.test.ts b/apps/web/src/lib/recipeScaling.test.ts new file mode 100644 index 0000000..9b66d82 --- /dev/null +++ b/apps/web/src/lib/recipeScaling.test.ts @@ -0,0 +1,39 @@ +import { describe, expect, it } from 'vitest'; +import { formatAmount, scaleAmount } from './recipeScaling'; + +const scaled = (amount: number, to: number, from: number) => + formatAmount(scaleAmount(amount, to, from)); + +describe('recipe scaling', () => { + it('never turns a small positive amount into 0', () => { + expect(scaled(0.125, 1, 4)).toBe('0.031'); // 1/8 tsp for one instead of four + expect(scaled(0.01, 1, 10)).toBe('0.001'); + expect(scaled(0.0001, 1, 1000)).toBe('0.0000001'); + }); + + it('keeps common fractions readable', () => { + expect(scaled(0.5, 3, 2)).toBe('0.75'); + expect(scaled(1, 1, 3)).toBe('0.33'); + expect(scaled(2.5, 3, 2)).toBe('3.8'); + expect(scaled(0.1, 3, 1)).toBe('0.3'); // not 0.30000000000000004 + }); + + it('rounds large quantities sensibly', () => { + expect(scaled(500, 3, 4)).toBe('375'); + expect(scaled(60, 3, 2)).toBe('90'); + expect(scaled(1234, 3, 1)).toBe('3700'); + expect(scaled(99999999.9999, 1000, 1)).toBe('100000000000'); + }); + + it('scales from the original amount, so repeated changes do not drift', () => { + // 4 -> 3 -> 7 -> 4 servings always starts from the stored 0.3. + for (const servings of [3, 7, 4]) scaled(0.3, servings, 4); + expect(scaled(0.3, 4, 4)).toBe('0.3'); + expect(scaled(1 / 3, 3, 1)).toBe('1'); + }); + + it('shows 0 only for zero or invalid amounts', () => { + expect(formatAmount(0)).toBe('0'); + expect(formatAmount(Number.NaN)).toBe('0'); + }); +}); diff --git a/apps/web/src/lib/recipeScaling.ts b/apps/web/src/lib/recipeScaling.ts new file mode 100644 index 0000000..880f45d --- /dev/null +++ b/apps/web/src/lib/recipeScaling.ts @@ -0,0 +1,35 @@ +/** + * Scales an ingredient amount from the recipe's own servings to the + * servings the reader chose. Always scales from the stored original amount, + * so changing servings back and forth never accumulates rounding errors. + */ +export function scaleAmount( + amount: number, + selectedServings: number, + originalServings: number, +) { + return amount * (selectedServings / originalServings); +} + +/** + * Rounds a scaled amount to something readable in a kitchen: + * - 200 and above: nearest 5 (e.g. 245 g) + * - 50–199: whole numbers + * - 3–49: one decimal + * - 0.1–3: two decimals (e.g. 1.25 tsp) + * - below 0.1: two significant figures (e.g. 0.042) + * A positive amount is never shown as 0. + */ +export function formatAmount(value: number) { + if (!Number.isFinite(value) || value <= 0) return '0'; + let rounded: number; + if (value >= 200) rounded = Math.round(value / 5) * 5; + else if (value >= 50) rounded = Math.round(value); + else if (value >= 3) rounded = Math.round(value * 10) / 10; + else if (value >= 0.1) rounded = Math.round(value * 100) / 100; + else rounded = Number(value.toPrecision(2)); + // Number(...toPrecision()) also removes float noise like 0.30000000000000004. + const text = String(Number(rounded.toPrecision(12))); + // Very small values would print as "1e-7"; show them as plain decimals. + return text.includes('e') ? rounded.toFixed(20).replace(/\.?0+$/, '') : text; +}