feat: Enhance ingredient parsing to support mixed fractions and add description extraction in recipe parsers

This commit is contained in:
Nils-Johan Gynther
2026-04-12 10:50:59 +02:00
parent 9ca7fcce96
commit 3d4994f24d
5 changed files with 314 additions and 218 deletions
@@ -59,15 +59,25 @@ export abstract class RecipeParser {
cleaned = cleaned.replace(/\s*\([^)]*\)/, '').trim();
}
// Hantera bråkdelar: "1/2" eller "1 / 2"
const fractionMatch = cleaned.match(/^([\d.]+)\s*\/\s*([\d.]+)/);
// Hantera bråkdelar: "1/2" eller "1 1/2" eller "1 1 / 2"
// Regex: (optional whole)? numerator / denominator
const fractionMatch = cleaned.match(/^(\d+)?\s*(\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;
if (fractionMatch[1]) {
// Heltal + bråk: "1 1/2"
const whole = parseFloat(fractionMatch[1]);
const numerator = parseFloat(fractionMatch[2]);
const denominator = parseFloat(fractionMatch[3]);
quantity = whole + (numerator / denominator);
} else {
// Bara bråk: "1/2"
const numerator = parseFloat(fractionMatch[2]);
const denominator = parseFloat(fractionMatch[3]);
quantity = numerator / denominator;
}
remainingText = cleaned.substring(fractionMatch[0].length).trim();
} else {
const numberMatch = remainingText.match(/^([\d.,]+)/);