Commit: 14d3105
Parent: af7adb6

Parse ingredient names with multiple parts

Mårten Åsberg committed on 2026-07-24 at 08:42
src/JsonLdRecipeParser/Ingredient.cs +2 -1
diff --git a/src/JsonLdRecipeParser/Ingredient.cs b/src/JsonLdRecipeParser/Ingredient.cs
index 36b2548..58b143e 100644
@@ -50,11 +50,12 @@ public record Ingredient
return false;
}
text = text[start..];
var end = text.IndexOfAny(WhitespaceCharacters);
var end = text.LastIndexOfAnyExcept(WhitespaceCharacters);
if (end == -1)
{
end = text.Length;
}
end += 1;
name = text[..end].ToString();
text = text[end..];
src/JsonLdRecipeParser/JsonLdRecipeParser.csproj +1 -1
diff --git a/src/JsonLdRecipeParser/JsonLdRecipeParser.csproj b/src/JsonLdRecipeParser/JsonLdRecipeParser.csproj
index 5f22f8a..6bf7fa9 100644
@@ -5,7 +5,7 @@
</PropertyGroup>
<PropertyGroup>
<PackageId>JsonLdRecipeParser</PackageId>
<Version>0.0.1</Version>
<Version>0.0.2</Version>
<Authors>Mårten Åsberg</Authors>
<Company>MoreTec</Company>
<Description>Parses JSON-LD to a common recipe format.</Description>
test/JsonLdRecipeParser.Tests/IngredientTests/TryParseShould.cs +34 -0
diff --git a/test/JsonLdRecipeParser.Tests/IngredientTests/TryParseShould.cs b/test/JsonLdRecipeParser.Tests/IngredientTests/TryParseShould.cs
index 7fd7452..a1dd0a2 100644
@@ -84,4 +84,38 @@ public class TryParseShould
ingredient.Amount.Unit.ShouldBeNull();
ingredient.Name.ShouldBe("citron");
}
[TestMethod]
public void ReturnIngredientNameWithMultipleParts()
{
// Arrange
var text = "1 tsk färsk ingefära, riven";
// Act
var success = Ingredient.TryParse(text, out var ingredient);
// Assert
success.ShouldBeTrue();
ingredient.ShouldNotBeNull();
ingredient.Amount.ShouldNotBeNull();
ingredient.Amount.Value.ShouldBe(new ExactAmountValue(1.0d));
ingredient.Amount.Unit.ShouldBe("tsk");
ingredient.Name.ShouldBe("färsk ingefära, riven");
}
[TestMethod]
public void ReturnIngredientNameWithMultiplePartsWithoutAmount()
{
// Arrange
var text = "limefrukt, i klyftor";
// Act
var success = Ingredient.TryParse(text, out var ingredient);
// Assert
success.ShouldBeTrue();
ingredient.ShouldNotBeNull();
ingredient.Amount.ShouldBeNull();
ingredient.Name.ShouldBe("limefrukt, i klyftor");
}
}
test/JsonLdRecipeParser.Tests/RecipeTests/ParseShould.cs +2 -2
diff --git a/test/JsonLdRecipeParser.Tests/RecipeTests/ParseShould.cs b/test/JsonLdRecipeParser.Tests/RecipeTests/ParseShould.cs
index e175234..263fb05 100644
@@ -52,12 +52,12 @@ public class ParseShould
[
new Ingredient
{
Name = "of",
Name = "of pineapple juice",
Amount = new Amount { Value = new ExactAmountValue(400.0d), Unit = "ml" },
},
new Ingredient
{
Name = "cream",
Name = "cream of coconut",
Amount = new Amount { Value = new ExactAmountValue(100.0d), Unit = "ml" },
},
new Ingredient { Name = "ice" },