33 lines
1.6 KiB
TypeScript
33 lines
1.6 KiB
TypeScript
// Wie eine Zutat bei einer Menge heißt. Bei Stück zählt die Zahl: „1 Zwiebel“,
|
|
// „½ Zitrone“, aber „2 Zwiebeln“, „1½ Zitronen“. Gewicht und Volumen behalten den
|
|
// Listennamen („500 g Kartoffeln“). Die Formen kommen aus dem Seed (ingredients.forms).
|
|
|
|
export type Forms = Partial<Record<string, { one: string; other: string }>>;
|
|
|
|
export type Nameable = { names: Record<string, string>; forms?: Forms | null; unit: "g" | "ml" | "piece" };
|
|
|
|
/** Einzahl bis einschließlich 1 — also auch für ½ und ¼. */
|
|
export const isOne = (qty: number) => qty > 0 && qty <= 1 + 1e-9;
|
|
|
|
// „Gehackte Tomaten (Dose)“ neben „400 g (1 Dose)“ wäre doppelt: Steht das Gebinde
|
|
// schon in der Menge (26.11.39), fällt der Hinweis am Namen weg.
|
|
const PACK_HINT = /\s*\((dose|glas|tin|can|jar|bocal|boîte|lata|tarro)\)$/i;
|
|
|
|
/** Für Anzeigen, die Name und Formen schon in der Sprache haben (Gerichtsseite, Kochmodus). */
|
|
export function labelFor(
|
|
i: { name: string; forms: { one: string; other: string } | null; unit: string; displayUnit?: string | null },
|
|
qty: number,
|
|
): string {
|
|
if (i.displayUnit && ["can", "jar", "pack"].includes(i.displayUnit)) return i.name.replace(PACK_HINT, "");
|
|
if (i.unit !== "piece" || !i.forms) return i.name;
|
|
return isOne(qty) ? i.forms.one : i.forms.other;
|
|
}
|
|
|
|
export function ingredientLabel(entry: Nameable, locale: string, qty: number | null): string {
|
|
const list = entry.names[locale] ?? entry.names.en ?? "";
|
|
if (entry.unit !== "piece" || qty === null) return list;
|
|
const f = entry.forms?.[locale] ?? entry.forms?.en;
|
|
if (!f) return list;
|
|
return isOne(qty) ? f.one : f.other;
|
|
}
|