diff --git a/Base.Tests/Extensions/EnumerableExtensionTests.cs b/Base.Tests/Extensions/EnumerableExtensionTests.cs index 1249f6f..73b7348 100644 --- a/Base.Tests/Extensions/EnumerableExtensionTests.cs +++ b/Base.Tests/Extensions/EnumerableExtensionTests.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using FruityFoundation.Base.Extensions; using NUnit.Framework; @@ -10,4 +10,9 @@ public class EnumerableExtensionTests [TestCase(new object[] { "hi" }, false, new object[] { "there" }, ExpectedResult = new object[] { "hi" })] public object[] TestConditionalConcat(object[] input, bool isConditionValid, object[] second) => input.ConditionalConcat(isConditionValid, second).ToArray(); + + [TestCase(new object[] { 0, 1, 2 }, true, 85, ExpectedResult = new object[0])] + [TestCase(new object[] { "hi", "there" }, false, "there", ExpectedResult = new object[] { "hi", "there" })] + public object[] TestConditionalWhere(object[] input, bool isConditionValid, object valueToKeep) => + input.ConditionalWhere(isConditionValid, x => x.Equals(valueToKeep)).ToArray(); } \ No newline at end of file diff --git a/Base/Extensions/EnumerableExtensions.cs b/Base/Extensions/EnumerableExtensions.cs index 5a2de5c..0f27ae7 100644 --- a/Base/Extensions/EnumerableExtensions.cs +++ b/Base/Extensions/EnumerableExtensions.cs @@ -1,7 +1,10 @@ -namespace FruityFoundation.Base.Extensions; +namespace FruityFoundation.Base.Extensions; public static class EnumerableExtensions { public static IEnumerable ConditionalConcat(this IEnumerable enumerable, bool isConditionValid, IEnumerable second) => !isConditionValid ? enumerable : enumerable.Concat(second); + + public static IEnumerable ConditionalWhere(this IEnumerable enumerable, bool isConditionValid, Func pred) => + !isConditionValid ? enumerable : enumerable.Where(pred); } \ No newline at end of file