diff --git a/Base.Tests/Structures/EnumerableExtensionTests.cs b/Base.Tests/Structures/EnumerableExtensionTests.cs index 8709c9d..7b8830e 100644 --- a/Base.Tests/Structures/EnumerableExtensionTests.cs +++ b/Base.Tests/Structures/EnumerableExtensionTests.cs @@ -20,4 +20,30 @@ public class EnumerableExtensionTests [TestCase(new object[] { "hi", "there" }, false, "there", ExpectedResult = new object[] { "hi", "there" })] public object[] TestConditionalWhere(object[] input, bool condition, object valueToKeep) => input.ConditionalWhere(condition, x => x.Equals(valueToKeep)).ToArray(); + + [Test] + public void TestConditionalAppendWithMaybe_HasAppendedItem_WhenMaybeHasValue() + { + var baseArray = new[] { 0, 1, 2 }; + + var result = baseArray + .ConditionalAppend(Maybe.Just(3)) + .ToArray(); + + Assert.That(result, Has.Length.EqualTo(4)); + Assert.That(result, Is.EquivalentTo(new[] { 0, 1, 2, 3 })); + } + + [Test] + public void TestConditionalAppendWithMaybe_DoesNotHaveAppendedItem_WhenMaybeIsEmpty() + { + var baseArray = new[] { 0, 1, 2 }; + + var result = baseArray + .ConditionalAppend(Maybe.Empty()) + .ToArray(); + + Assert.That(result, Has.Length.EqualTo(3)); + Assert.That(result, Is.EquivalentTo(new[] { 0, 1, 2 })); + } } diff --git a/Base/Structures/EnumerableExtensions.cs b/Base/Structures/EnumerableExtensions.cs index 1ef735c..36c9bb1 100644 --- a/Base/Structures/EnumerableExtensions.cs +++ b/Base/Structures/EnumerableExtensions.cs @@ -11,6 +11,9 @@ public static class EnumerableExtensions public static IEnumerable ConditionalAppend(this IEnumerable enumerable, bool condition, T second) => condition ? enumerable.Append(second) : enumerable; + + public static IEnumerable ConditionalAppend(this IEnumerable enumerable, Maybe item) => + item.HasValue ? enumerable.Append(item.Value) : enumerable; public static IEnumerable ConditionalWhere(this IEnumerable enumerable, bool condition, Func pred) => !condition ? enumerable : enumerable.Where(pred);