Skip to content

Commit

Permalink
feat: Default To Fractions When Unit Is Empty (#3587)
Browse files Browse the repository at this point in the history
Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
  • Loading branch information
michael-genson and hay-kot committed May 12, 2024
1 parent 554b3fa commit c82549c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
6 changes: 6 additions & 0 deletions frontend/composables/recipes/use-recipe-ingredients.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe(parseIngredientText.name, () => {
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", () => {
const ingredient = createRecipeIngredient({ quantity: 1.5, unit: { fraction: true, id: "1", name: "cup" } });
const result = parseIngredientText(ingredient, false, 1, false);
Expand Down
6 changes: 3 additions & 3 deletions frontend/composables/recipes/use-recipe-ingredients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo

// casting to number is required as sometimes quantity is a string
if (quantity && Number(quantity) !== 0) {
if (unit?.fraction) {
if (unit && !unit.fraction) {
returnQty = (quantity * scale).toString();
} else {
const fraction = frac(quantity * scale, 10, true);
if (fraction[0] !== undefined && fraction[0] > 0) {
returnQty += fraction[0];
Expand All @@ -64,8 +66,6 @@ export function useParsedIngredientText(ingredient: RecipeIngredient, disableAmo
` <sup>${fraction[1]}</sup>&frasl;<sub>${fraction[2]}</sub>` :
` ${fraction[1]}/${fraction[2]}`;
}
} else {
returnQty = (quantity * scale).toString();
}
}

Expand Down
2 changes: 1 addition & 1 deletion mealie/schema/recipe/recipe_ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _format_quantity_for_display(self) -> str:
qty: float | Fraction

# 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)
if qty.is_integer():
return str(int(qty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import pytest

from mealie.schema.recipe.recipe_ingredient import IngredientFood, IngredientUnit, RecipeIngredient
from tests.utils.factories import random_string
from mealie.schema.recipe.recipe_ingredient import (
IngredientFood,
IngredientUnit,
RecipeIngredient,
)


@pytest.mark.parametrize(
Expand All @@ -19,6 +22,12 @@
@pytest.mark.parametrize(
["unit", "expect_display_fraction", "expected_unit_singular_string", "expected_unit_plural_string"],
[
[
None,
True,
"",
"",
],
[
IngredientUnit(
id=uuid4(),
Expand Down Expand Up @@ -154,7 +163,7 @@ def test_ingredient_display(
quantity: float | None,
quantity_display_decimal: str,
quantity_display_fraction: str,
unit: IngredientUnit,
unit: IngredientUnit | None,
food: IngredientFood,
note: str,
use_food: bool,
Expand Down

0 comments on commit c82549c

Please sign in to comment.