Forráskód Böngészése

IEnumerable增加AppendIf扩展

懒得勤快 10 hónapja
szülő
commit
658b6da3e3

+ 1 - 1
Directory.Build.props

@@ -1,6 +1,6 @@
 <Project>
  <PropertyGroup>
-   <Version>2024.7</Version>
+   <Version>2024.7.1</Version>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
 </Project>

+ 58 - 0
Masuit.Tools.Abstractions/Extensions/BaseType/IEnumerableExtensions.cs

@@ -375,6 +375,64 @@ public static class IEnumerableExtensions
         }
     }
 
+    /// <summary>
+    /// 符合条件则添加一个元素
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="this"></param>
+    /// <param name="predicate"></param>
+    /// <param name="value"></param>
+    public static ICollection<T> AppendIf<T>(this ICollection<T> @this, Func<bool> predicate, T value)
+    {
+        if (predicate())
+        {
+            @this.Add(value);
+        }
+
+        return @this;
+    }
+
+    /// <summary>
+    /// 符合条件则添加一个元素
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="this"></param>
+    /// <param name="predicate"></param>
+    /// <param name="value"></param>
+    public static ICollection<T> AppendIf<T>(this ICollection<T> @this, bool predicate, T value)
+    {
+        if (predicate)
+        {
+            @this.Add(value);
+        }
+
+        return @this;
+    }
+
+    /// <summary>
+    /// 符合条件则添加一个元素
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="this"></param>
+    /// <param name="predicate"></param>
+    /// <param name="value"></param>
+    public static IEnumerable<T> AppendIf<T>(this IEnumerable<T> @this, Func<bool> predicate, T value)
+    {
+        return predicate() ? @this.Append(value) : @this;
+    }
+
+    /// <summary>
+    /// 符合条件则添加一个元素
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="this"></param>
+    /// <param name="predicate"></param>
+    /// <param name="value"></param>
+    public static IEnumerable<T> AppendIf<T>(this IEnumerable<T> @this, bool predicate, T value)
+    {
+        return predicate ? @this.Append(value) : @this;
+    }
+
     /// <summary>
     /// 移除符合条件的元素
     /// </summary>

+ 35 - 0
Test/Masuit.Tools.Abstractions.Test/Extensions/BaseType/IEnumerableTest.cs

@@ -0,0 +1,35 @@
+using System.Collections.Generic;
+using System.Linq;
+using Xunit;
+
+namespace Masuit.Tools.Abstractions.Test.Extensions.BaseType;
+
+public class IEnumerableTest
+{
+    [Fact]
+    public void Can_ICollection_AppendIf()
+    {
+        // arrange
+        var list = new List<string>();
+
+        // act
+        list.AppendIf(true, "1");
+        list.AppendIf(() => true, "2");
+
+        // assert
+        Assert.True(list.Count == 2);
+    }
+
+    [Fact]
+    public void Can_Enumerable_AppendIf()
+    {
+        // arrange
+        var enumerable = Enumerable.Range(1, 1);
+
+        // act
+        enumerable = enumerable.AppendIf(true, 2).AppendIf(() => true, 3);
+
+        // assert
+        Assert.Equal(enumerable.Count(), 3);
+    }
+}