// Mengen: gespeichert immer in Basiseinheiten pro Portion (g, ml, Stück — // CLAUDE.md, Regel 2). Hier und nur hier wird umgerechnet: hochrechnen, // lesbar anzeigen, Eingaben aus Küchenmaßen in Basiseinheiten bringen. export type BaseUnit = "g" | "ml" | "piece"; /** Gebinde, in denen man kauft: Dose, Glas, Packung, Becher, Bund (26.11.39, Becher 26.11.49). */ export const PACK_KINDS = ["can", "jar", "pack", "tub", "bunch"] as const; export type PackKind = (typeof PACK_KINDS)[number]; /** * Zählmaße mit einer Größe je Zutat (26.11.49, Kai: „überall“): die Gebinde und * Stück, Würfel, Zehe, Scheibe. „1 Packung Hackfleisch = 500 g“, „1 Zehe = 3 g“ — * die Größe kennt der Haushalt (household_pack_sizes) oder die Zutat. */ export const COUNT_UNITS = [...PACK_KINDS, "piece", "cube", "clove", "slice"] as const; export type CountUnit = (typeof COUNT_UNITS)[number]; /** Feste Küchenmaße für Gramm- und Flüssig-Zutaten: Löffel, Prise, Tasse, Schuss, Messerspitze. */ export const FIXED_UNITS = ["tbsp", "tsp", "pinch", "cup", "dash", "knifepoint"] as const; export type FixedUnit = (typeof FIXED_UNITS)[number]; /** Küchenmaße, die ein Rezept so anzeigt, wie sie eingegeben wurden (26.11.39). */ export const KITCHEN_UNITS = [...FIXED_UNITS, ...COUNT_UNITS] as const; export type KitchenUnit = (typeof KITCHEN_UNITS)[number]; export const isPackKind = (u: string): u is PackKind => (PACK_KINDS as readonly string[]).includes(u); export const isCountUnit = (u: string): u is CountUnit => (COUNT_UNITS as readonly string[]).includes(u); export const isFixedUnit = (u: string): u is FixedUnit => (FIXED_UNITS as readonly string[]).includes(u); export const isKitchenUnit = (u: string): u is KitchenUnit => (KITCHEN_UNITS as readonly string[]).includes(u); /** Gewicht und Volumen (26.11.50, Kai: „Milliliter und Liter“ überall, auch bei Stück-Zutaten). */ export const METRIC = ["g", "kg", "ml", "l"] as const; export type Metric = (typeof METRIC)[number]; export const isMetric = (u: string): u is Metric => (METRIC as readonly string[]).includes(u); const METRIC_FACTOR: Record = { g: 1, kg: 1000, ml: 1, l: 1000 }; /** Maß, in dem ein Rezept eine Menge zeigt, wie sie eingegeben wurde. */ export type DisplayUnit = KitchenUnit | Metric; /** Maße, die man tippt oder in Rezepten liest. */ export type InputUnit = BaseUnit | "kg" | "l" | "cl" | KitchenUnit; /** * Ein Maß, das in dieser Basiseinheit eine Größe je Zutat braucht: Zählmaße („Stück“ * bei Stück-Zutaten ist keins) und Gewicht oder Volumen bei Stück-Zutaten * („750 ml Brühe“ bei Brühwürfeln: 1 Stück = 500 ml). */ export const needsSize = (unit: string, base: BaseUnit): boolean => (isCountUnit(unit) && !(unit === "piece" && base === "piece")) || (base === "piece" && isMetric(unit)); /** Unter welchem Maß eine Größe steht: kg und l teilen sich die von g und ml (je Stück). */ export const sizeKey = (unit: string): string => (unit === "kg" ? "g" : unit === "l" ? "ml" : unit); /** Ohne Angabe: Würfel, Zehe, Scheibe einer Stück-Zutat sind je ein Stück. */ export function defaultSize(unit: string, base: BaseUnit): number | null { return base === "piece" && (unit === "cube" || unit === "clove" || unit === "slice") ? 1 : null; } /** Größe eines Zählmaßes für eine Zutat: Haushalt, sonst ihr Gebinde, sonst die Vorgabe oben. */ export function sizeOf( unit: string, base: BaseUnit, entry: { packKind?: string | null; packSize?: number | null; sizes?: Partial> | null }, ): number | null { return entry.sizes?.[sizeKey(unit)] ?? (entry.packKind === unit ? (entry.packSize ?? null) : null) ?? defaultSize(unit, base); } /** Hochrechnen: Menge pro Portion × Portionen. */ export function scale(qtyPerPortion: number, portions: number): number { return qtyPerPortion * portions; } // Küchenmaße in Basiseinheiten. Löffel und Tassen sind Volumen; für feste // Zutaten (Einheit g) gilt eine mittlere Dichte — genau genug für ein // Familienrezept, und die Planerin sieht und korrigiert die Menge. const VOLUME_ML: Partial> = { ml: 1, cl: 10, l: 1000, tbsp: 15, tsp: 5, cup: 240, pinch: 0.5, dash: 10, knifepoint: 0.5 }; const SOLID_G: Partial> = { g: 1, kg: 1000, tbsp: 12, tsp: 4, cup: 150, pinch: 0.5, dash: 10, knifepoint: 0.5 }; /** * Eingabe in die Basiseinheit der Zutat bringen. Gibt null zurück, wenn die * Maße nicht zusammenpassen (z. B. „200 g“ Eier) — dann fragt die Oberfläche nach. * Ein Gebinde braucht seine Größe in der Basiseinheit („1 Dose“ × 400 g). */ export function toBase(qty: number, unit: InputUnit, base: BaseUnit, packSize?: number | null): number | null { if (!Number.isFinite(qty) || qty < 0) return null; // Stück-Zutat in Gewicht oder Volumen: Größe ist ml bzw. g je Stück. if (base === "piece" && isMetric(unit)) return packSize && packSize > 0 ? (qty * METRIC_FACTOR[unit]) / packSize : null; if (needsSize(unit, base)) { const size = packSize ?? defaultSize(unit, base); return size && size > 0 ? qty * size : null; } if (base === "piece") return unit === "piece" ? qty : null; if (unit === "piece") return null; if (base === "ml") { const f = VOLUME_ML[unit] ?? (unit === "g" ? 1 : unit === "kg" ? 1000 : undefined); // g ≈ ml bei Flüssigem return f === undefined ? null : qty * f; } const f = SOLID_G[unit] ?? (unit === "ml" ? 1 : unit === "l" ? 1000 : unit === "cl" ? 10 : undefined); return f === undefined ? null : qty * f; } const FRACTIONS: [number, string][] = [ [0.25, "¼"], [1 / 3, "⅓"], [0.5, "½"], [2 / 3, "⅔"], [0.75, "¾"], ]; /** Stückzahlen in Küchenbrüchen: 0,5 → ½, 1,25 → 1¼; sonst auf eine Stelle. */ function pieces(qty: number, locale: string): string { const whole = Math.floor(qty + 1e-9); const rest = qty - whole; if (rest < 0.05) return String(whole || (qty > 0 ? "<1" : "0")); if (rest > 0.95) return String(whole + 1); for (const [value, glyph] of FRACTIONS) { if (Math.abs(rest - value) < 0.05) return whole ? `${whole}${glyph}` : glyph; } return new Intl.NumberFormat(locale, { maximumFractionDigits: 1 }).format(qty); } /** Auf eine Größe runden, die man in der Küche abwiegt. */ function roundMeasure(value: number): number { if (value < 5) return Math.round(value * 2) / 2; if (value < 50) return Math.round(value); if (value < 250) return Math.round(value / 5) * 5; return Math.round(value / 10) * 10; } export type Formatted = { amount: string; unit: string }; /** * Für die Anzeige: 1500 g → 1,5 kg, 250 ml → 250 ml, 0,5 Stück → ½. * `unit` ist ein kurzes Zeichen ("g", "kg", "ml", "l") oder leer bei Stück — * das Wort für Stück liefert die Oberfläche selbst (Plural je Sprache). */ export function formatQuantity(qty: number, unit: BaseUnit, locale = "de"): Formatted { const nf = (v: number, digits: number) => new Intl.NumberFormat(locale, { maximumFractionDigits: digits }).format(v); if (unit === "piece") return { amount: pieces(qty, locale), unit: "" }; const rounded = roundMeasure(qty); if (rounded >= 1000) return { amount: nf(Math.round(rounded / 100) / 10, 1), unit: unit === "g" ? "kg" : "l" }; return { amount: nf(rounded, 1), unit }; } /** Menge in Basiseinheit zurück in das Küchenmaß, in dem sie eingegeben wurde (fürs Bearbeiten). */ export function fromBase(qty: number, unit: DisplayUnit, base: BaseUnit, packSize?: number | null): number | null { if (base === "piece" && isMetric(unit)) return packSize && packSize > 0 ? (qty * packSize) / METRIC_FACTOR[unit] : null; if (isMetric(unit)) return qty / METRIC_FACTOR[unit]; // g ↔ ml wie beim Löffel: 1 zu 1 if (needsSize(unit, base)) { const size = packSize ?? defaultSize(unit, base); return size && size > 0 ? qty / size : null; } const f = base === "ml" ? VOLUME_ML[unit] : base === "g" ? SOLID_G[unit] : undefined; return f ? qty / f : null; } /** Löffel und Prisen in Küchenbrüchen; ein Löffel wird nicht genauer als ein halber. */ function spoons(qty: number, locale: string): string { return pieces(qty < 0.5 ? qty : Math.round(qty * 2) / 2, locale); } export type KitchenFormatted = { /** Vorn stehende Zahl mit Maß: „2 EL“, „400 g“, „6“. */ main: string; /** In Klammern dahinter: „30 ml“, „1 Dose“ — leer, wenn nichts zu sagen ist. */ extra: string; }; /** * Anzeige mit Küchenmaß (Kai, 26.11.39): Löffel vorn, Gramm in Klammern — * „2 EL (30 ml)“; Gebinde hinten, Gramm vorn — „400 g (1 Dose)“. Ohne Küchenmaß * oder ohne bekannte Gebindegröße wie bisher. `word` liefert das Wort in der * Sprache („Dose“/„Dosen“), `count` entscheidet über Einzahl oder Mehrzahl. */ export function formatKitchen( qty: number, base: BaseUnit, display: DisplayUnit | null | undefined, packSize: number | null | undefined, locale: string, word: (unit: KitchenUnit, count: number) => string, ): KitchenFormatted { const plain = formatQuantity(qty, base, locale); const plainText = `${plain.amount}${plain.unit ? ` ${plain.unit}` : ""}`; if (!display) return { main: plainText, extra: "" }; const n = fromBase(qty, display, base, packSize); if (n === null) return { main: plainText, extra: "" }; if (isMetric(display)) { // Im eingegebenen Maß („750 ml“, „1,5 l“); bei Stück-Zutaten die Stück dahinter (26.11.50). const family = display === "g" || display === "kg" ? "g" : "ml"; const f = formatQuantity(n * METRIC_FACTOR[display], family, locale); return { main: `${f.amount} ${f.unit}`, extra: base === "piece" ? `${pieces(qty, locale)} ${word("piece", qty)}` : "" }; } if (isPackKind(display)) { // Ein Achtel Packung Butter hilft niemandem — dann nur Gramm. if (n < 0.25 - 1e-9) return { main: plainText, extra: "" }; const count = pieces(n, locale); return { main: plainText, extra: `${count} ${word(display, n)}` }; } // Löffel auf halbe, Stück, Zehen, Würfel und Scheiben auf Küchenbrüche (26.11.49). const count = isCountUnit(display) ? pieces(n, locale) : spoons(n, locale); // Eine Prise wiegt nichts, was man nachwiegen würde — ohne Klammer. return { main: `${count} ${word(display, n)}`, extra: display === "pinch" || display === "knifepoint" || base === "piece" ? "" : plainText }; } /** * Küchenmaß für Mengen ohne eingegebenes Maß (ältere Gerichte, Vorlagen): Gebinde, * wenn es mindestens ein Viertel ist; Löffel, wenn es ½ bis 8 Löffel sind. Prisen * nie von selbst. Entschieden für die Menge des ganzen Rezepts, nicht je Portion. */ export function impliedKitchenUnit( qty: number, base: BaseUnit, kitchenUnit: KitchenUnit | null | undefined, packKind: PackKind | null | undefined, packSize: number | null | undefined, ): KitchenUnit | null { if (packKind && packSize) { const n = qty / packSize; if (n >= 0.25 - 1e-9) return packKind; } if (kitchenUnit === "tbsp" || kitchenUnit === "tsp") { const n = fromBase(qty, kitchenUnit, base); if (n !== null && n >= 0.5 - 1e-9 && n <= 8 + 1e-9) return kitchenUnit; } return null; } /** * Eine Menge fürs Formular, so wie man sie tippen würde (26.11.49): Zählmaße und * Löffel auf Viertel, ab 10 ganze Zahlen, sonst höchstens zwei Stellen. * Aus 333,333 g wird 333, aus 1,4999 Dosen 1,5. */ export function niceQty(qty: number, unit: string): number { if (isKitchenUnit(unit)) return Math.max(0.25, Math.round(qty * 4) / 4); if (qty >= 10) return Math.round(qty); return Math.round(qty * 100) / 100; } /** Für die Einkaufsliste: Stück immer aufrunden (½ Zwiebel kauft man nicht). */ export function shoppingQuantity(qty: number, unit: BaseUnit): number { return unit === "piece" ? Math.ceil(qty - 1e-9) : qty; } /** Deutsche und englische Zahlen lesen: „1,5“, „1.5“, „1 1/2“, „½“. */ export function parseNumber(text: string): number | null { const t = text.trim().replace(/\s+/g, " "); const glyph: Record = { "¼": 0.25, "½": 0.5, "¾": 0.75, "⅓": 1 / 3, "⅔": 2 / 3 }; const m = t.match(/^(\d+)?\s*([¼½¾⅓⅔])$/); if (m) return Number(m[1] ?? 0) + glyph[m[2]!]!; const mixed = t.match(/^(\d+) (\d+)\/(\d+)$/); if (mixed) return Number(mixed[1]) + Number(mixed[2]) / Number(mixed[3]); const frac = t.match(/^(\d+)\/(\d+)$/); if (frac) return Number(frac[1]) / Number(frac[2]); const n = Number(t.replace(",", ".")); return Number.isFinite(n) && t !== "" ? n : null; }