mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-09 03:04:54 -04:00
feat: Default To Fractions When Unit Is Empty (#3587)
Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
This commit is contained in:
parent
554b3fa749
commit
c82549ccb4
@ -34,6 +34,12 @@ describe(parseIngredientText.name, () => {
|
|||||||
expect(parseIngredientText(ingredient, false, 1, true)).contain("1 <sup>1</sup>").and.to.contain("<sub>2</sub>");
|
expect(parseIngredientText(ingredient, false, 1, true)).contain("1 <sup>1</sup>").and.to.contain("<sub>2</sub>");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("ingredient text with fraction when unit is null", () => {
|
||||||
|
const ingredient = createRecipeIngredient({ quantity: 1.5, unit: undefined });
|
||||||
|
|
||||||
|
expect(parseIngredientText(ingredient, false, 1, true)).contain("1 <sup>1</sup>").and.to.contain("<sub>2</sub>");
|
||||||
|
});
|
||||||
|
|
||||||
test("ingredient text with fraction no formatting", () => {
|
test("ingredient text with fraction no formatting", () => {
|
||||||
const ingredient = createRecipeIngredient({ quantity: 1.5, unit: { fraction: true, id: "1", name: "cup" } });
|
const ingredient = createRecipeIngredient({ quantity: 1.5, unit: { fraction: true, id: "1", name: "cup" } });
|
||||||
const result = parseIngredientText(ingredient, false, 1, false);
|
const result = parseIngredientText(ingredient, false, 1, false);
|
||||||
|
@ -53,7 +53,9 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo
|
|||||||
|
|
||||||
// casting to number is required as sometimes quantity is a string
|
// casting to number is required as sometimes quantity is a string
|
||||||
if (quantity && Number(quantity) !== 0) {
|
if (quantity && Number(quantity) !== 0) {
|
||||||
if (unit?.fraction) {
|
if (unit && !unit.fraction) {
|
||||||
|
returnQty = (quantity * scale).toString();
|
||||||
|
} else {
|
||||||
const fraction = frac(quantity * scale, 10, true);
|
const fraction = frac(quantity * scale, 10, true);
|
||||||
if (fraction[0] !== undefined && fraction[0] > 0) {
|
if (fraction[0] !== undefined && fraction[0] > 0) {
|
||||||
returnQty += fraction[0];
|
returnQty += fraction[0];
|
||||||
@ -64,8 +66,6 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo
|
|||||||
` <sup>${fraction[1]}</sup>⁄<sub>${fraction[2]}</sub>` :
|
` <sup>${fraction[1]}</sup>⁄<sub>${fraction[2]}</sub>` :
|
||||||
` ${fraction[1]}/${fraction[2]}`;
|
` ${fraction[1]}/${fraction[2]}`;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
returnQty = (quantity * scale).toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ class RecipeIngredientBase(MealieModel):
|
|||||||
qty: float | Fraction
|
qty: float | Fraction
|
||||||
|
|
||||||
# decimal
|
# decimal
|
||||||
if not self.unit or not self.unit.fraction:
|
if self.unit and not self.unit.fraction:
|
||||||
qty = round(self.quantity or 0, INGREDIENT_QTY_PRECISION)
|
qty = round(self.quantity or 0, INGREDIENT_QTY_PRECISION)
|
||||||
if qty.is_integer():
|
if qty.is_integer():
|
||||||
return str(int(qty))
|
return str(int(qty))
|
||||||
|
@ -2,8 +2,11 @@ from uuid import uuid4
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from mealie.schema.recipe.recipe_ingredient import IngredientFood, IngredientUnit, RecipeIngredient
|
from mealie.schema.recipe.recipe_ingredient import (
|
||||||
from tests.utils.factories import random_string
|
IngredientFood,
|
||||||
|
IngredientUnit,
|
||||||
|
RecipeIngredient,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
@ -19,6 +22,12 @@ from tests.utils.factories import random_string
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
["unit", "expect_display_fraction", "expected_unit_singular_string", "expected_unit_plural_string"],
|
["unit", "expect_display_fraction", "expected_unit_singular_string", "expected_unit_plural_string"],
|
||||||
[
|
[
|
||||||
|
[
|
||||||
|
None,
|
||||||
|
True,
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
],
|
||||||
[
|
[
|
||||||
IngredientUnit(
|
IngredientUnit(
|
||||||
id=uuid4(),
|
id=uuid4(),
|
||||||
@ -154,7 +163,7 @@ def test_ingredient_display(
|
|||||||
quantity: float | None,
|
quantity: float | None,
|
||||||
quantity_display_decimal: str,
|
quantity_display_decimal: str,
|
||||||
quantity_display_fraction: str,
|
quantity_display_fraction: str,
|
||||||
unit: IngredientUnit,
|
unit: IngredientUnit | None,
|
||||||
food: IngredientFood,
|
food: IngredientFood,
|
||||||
note: str,
|
note: str,
|
||||||
use_food: bool,
|
use_food: bool,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user