From f8b5160f2dcd30f564f0a5864c9b614d6d54eab6 Mon Sep 17 00:00:00 2001 From: AlexBocken Date: Fri, 14 Jul 2023 14:31:19 +0200 Subject: [PATCH] ranges in portions are handled correctly --- src/lib/components/IngredientsPage.svelte | 30 +++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/lib/components/IngredientsPage.svelte b/src/lib/components/IngredientsPage.svelte index a295119..29c1f45 100644 --- a/src/lib/components/IngredientsPage.svelte +++ b/src/lib/components/IngredientsPage.svelte @@ -58,18 +58,22 @@ function findGCD(a, b) { return findGCD(b, a % b); } -// For portions, not sure whether to keep or let it be -function multiplyFirstNumberInString(inputString, constant) { - const firstNumberRegex = /\d+(?:[\.,]\d+)?/; - const match = inputString.match(firstNumberRegex); - if (match) { - const number = match[0].includes(',') ? match[0].replace(/\./g, '').replace(',', '.') : match[0]; - const multiplied = (parseFloat(number) * constant).toString(); - const rounded = parseFloat(multiplied).toString(); - const result = match[0].includes(',') ? rounded.replace('.', ',') : rounded; - return inputString.replace(firstNumberRegex, result); - } - return inputString; +// "1-2 Kuchen (Durchmesser: 26cm", constant=2 -> "2-4 Kuchen (Durchmesser: 26cm)" +function multiplyFirstAndSecondNumbers(inputString, constant) { + const regex = /(\d+(?:[\.,]\d+)?)(\s*-\s*\d+(?:[\.,]\d+)?)?/; + return inputString.replace(regex, (match, firstNumber, secondNumber) => { + const numbersToMultiply = [firstNumber]; + if (secondNumber) { + numbersToMultiply.push(secondNumber.replace(/-\s*/, '')); + } + const multipliedNumbers = numbersToMultiply.map(number => { + const multiplied = (parseFloat(number) * constant).toString(); + const rounded = parseFloat(multiplied).toString(); + const result = number.includes(',') ? rounded.replace('.', ',') : rounded; + return result; + }); + return multipliedNumbers.join('-') + }); } function adjust_amount(string, multiplier){ @@ -130,7 +134,7 @@ button.selected{
{#if data.portions}

Portionen:

- {@html convertFloatingPointToFraction(multiplyFirstNumberInString(data.portions, multiplier))} + {@html convertFloatingPointToFraction(multiplyFirstAndSecondNumbers(data.portions, multiplier))} {/if}

Menge anpassen: