From ea050f098aa50c95b04fb098d07aecb666d577f8 Mon Sep 17 00:00:00 2001 From: Kyle Ratti Date: Fri, 23 Sep 2022 21:07:42 -0400 Subject: [PATCH] feat: add Enumerable.ConditionalConcat --- Base.Tests/Extensions/EnumerableExtensionTests.cs | 13 +++++++++++++ Base/Base.csproj | 2 +- Base/Extensions/EnumerableExtensions.cs | 7 +++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Base.Tests/Extensions/EnumerableExtensionTests.cs create mode 100644 Base/Extensions/EnumerableExtensions.cs diff --git a/Base.Tests/Extensions/EnumerableExtensionTests.cs b/Base.Tests/Extensions/EnumerableExtensionTests.cs new file mode 100644 index 0000000..1249f6f --- /dev/null +++ b/Base.Tests/Extensions/EnumerableExtensionTests.cs @@ -0,0 +1,13 @@ +using System.Linq; +using FruityFoundation.Base.Extensions; +using NUnit.Framework; + +namespace Base.Tests.Extensions; + +public class EnumerableExtensionTests +{ + [TestCase(new object[] { 0, 1, 2 }, true, new object[] { 85 }, ExpectedResult = new object[] { 0, 1, 2, 85 })] + [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(); +} \ No newline at end of file diff --git a/Base/Base.csproj b/Base/Base.csproj index 55cd423..b18def3 100644 --- a/Base/Base.csproj +++ b/Base/Base.csproj @@ -11,7 +11,7 @@ FruityFoundation.Base https://github.com/kyleratti/FruityFoundation - 1.1.0 + 1.1.1 diff --git a/Base/Extensions/EnumerableExtensions.cs b/Base/Extensions/EnumerableExtensions.cs new file mode 100644 index 0000000..5a2de5c --- /dev/null +++ b/Base/Extensions/EnumerableExtensions.cs @@ -0,0 +1,7 @@ +namespace FruityFoundation.Base.Extensions; + +public static class EnumerableExtensions +{ + public static IEnumerable ConditionalConcat(this IEnumerable enumerable, bool isConditionValid, IEnumerable second) => + !isConditionValid ? enumerable : enumerable.Concat(second); +} \ No newline at end of file