feat: Add optional note field to ingredient parsing and update related components

This commit is contained in:
Nils-Johan Gynther
2026-04-12 10:30:05 +02:00
parent 03727ee3c5
commit 9ca7fcce96
7 changed files with 158 additions and 34 deletions
+88 -29
View File
@@ -9,6 +9,7 @@ export interface ParsedRecipe {
quantity: number;
unit: string;
name: string;
note?: string;
}>;
instructions?: string;
}
@@ -29,61 +30,119 @@ export abstract class RecipeParser {
* Hanterar format som:
* - "3 ägg"
* - "150 g lax"
* - "1/2 citron"
* - "1 msk senap"
* - "salt och peppar"
* - "1 förp handskalade räkor i lake (à 570 g)"
*/
protected parseIngredientLine(line: string): {
quantity: number;
unit: string;
name: string;
note?: string;
} | null {
const cleaned = line.replace(/<[^>]+>/g, '').trim();
let cleaned = line.replace(/<[^>]+>/g, '').trim();
if (!cleaned) return null;
// Kända enheter
const knownUnits = [
'g', 'kg', 'hg', 'ml', 'dl', 'l',
'g', 'kg', 'hg', 'mg', 'ml', 'dl', 'l', 'tl',
'st', 'tsk', 'msk', 'krm', 'matsked', 'tesked',
'pris', 'portion', 'burk', 'förp', 'paket',
];
// Försök extrahera: [quantity] [unit?] [productName]
// Regex: början med valfri mängd, sedan valfritt ord (potentiell enhet), sedan resten
const match = cleaned.match(/^([\d.,]+)?\s*([a-zåäö]*)\s*(.*)$/i);
// Extrahera parentetisk info
let parentheticalText = '';
const parentheteMatch = cleaned.match(/\s*\(([^)]*)\)/);
if (parentheteMatch) {
parentheticalText = parentheteMatch[1].trim();
cleaned = cleaned.replace(/\s*\([^)]*\)/, '').trim();
}
if (!match) {
// Hantera bråkdelar: "1/2" eller "1 / 2"
const fractionMatch = cleaned.match(/^([\d.]+)\s*\/\s*([\d.]+)/);
let quantity = 0;
let remainingText = cleaned;
if (fractionMatch) {
const numerator = parseFloat(fractionMatch[1]);
const denominator = parseFloat(fractionMatch[2]);
quantity = numerator / denominator;
remainingText = cleaned.substring(fractionMatch[0].length).trim();
} else {
const numberMatch = remainingText.match(/^([\d.,]+)/);
if (numberMatch) {
quantity = parseFloat(numberMatch[1].replace(',', '.'));
remainingText = remainingText.substring(numberMatch[0].length).trim();
}
}
// Extrahera potentiell enhet
let potentialUnit = '';
let productName = remainingText;
if (remainingText) {
const unitMatch = remainingText.match(/^([a-zåäö]+)\b/i);
if (unitMatch) {
const candidateUnit = unitMatch[1].toLowerCase();
if (knownUnits.includes(candidateUnit)) {
potentialUnit = candidateUnit;
productName = remainingText.substring(candidateUnit.length).trim();
}
}
}
// Analysera parenthetical text för måttenhet
let parenthHasUnit = false;
if (parentheticalText) {
for (const unit of knownUnits) {
if (parentheticalText.toLowerCase().includes(unit)) {
parenthHasUnit = true;
break;
}
}
}
let note: string | undefined = undefined;
// Om vi hade quantity i huvuddelen och parenthetical innehåller unit
// → spara parenthetical som note
if (quantity > 0 && parenthHasUnit) {
note = parentheticalText;
}
// Om ingen mängd i huvuddelen men parenthetical hade både mängd och unit
// → parse parenthetical som quantity + unit
if (quantity === 0 && parentheticalText) {
const parenthMatch = parentheticalText.match(/^[\D]*?([\d.,]+)?\s*([a-zåäö]*)?\s*(.*)$/i);
if (parenthMatch) {
let pQuantity = parenthMatch[1] ? parseFloat(parenthMatch[1].replace(',', '.')) : 0;
let pUnit = parenthMatch[2]?.toLowerCase() || '';
let pRest = parenthMatch[3]?.trim() || '';
if (knownUnits.includes(pUnit) && pQuantity > 0) {
quantity = pQuantity;
potentialUnit = pUnit;
note = parentheticalText;
}
}
}
// Om ingen mängd och enhet, bara returna produktnamnet
if (quantity === 0) {
return {
quantity: 0,
unit: 'st',
unit: '',
name: cleaned,
note: parentheticalText || undefined,
};
}
let quantity = match[1] ? parseFloat(match[1].replace(',', '.')) : 0;
let potentialUnit = match[2]?.toLowerCase().trim() || '';
let productName = match[3]?.trim() || '';
// Om potentialUnit är inte en känd enhet, lägg det tillbaka i produktnamnet
if (potentialUnit && !knownUnits.includes(potentialUnit)) {
productName = potentialUnit + (productName ? ' ' + productName : '');
potentialUnit = '';
}
// Om inget produktnamn men vi hade potentialUnit, denna är faktiskt produktnamnet
if (!productName && potentialUnit) {
productName = potentialUnit;
potentialUnit = '';
}
// Fallback: om vi har mängd men inget annat, vara produktnamn
if (quantity > 0 && !potentialUnit && !productName) {
return null; // Ogiltigt
}
return {
quantity,
unit: potentialUnit || 'st',
unit: potentialUnit,
name: productName,
note: note,
};
}
}