From db779d891a2a4b3c300dd81e0a8f4fa0a04f335c Mon Sep 17 00:00:00 2001 From: Tony Troeff Date: Mon, 22 Jul 2024 00:16:23 +0300 Subject: [PATCH] Added collection extension methods for `IList` --- .../CollectionExtensionsTests.cs | 42 ++++++++++++------- .../CollectionExtensions.cs | 21 +++++++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/TryAtSoftware.Extensions.Collections.Tests/CollectionExtensionsTests.cs b/TryAtSoftware.Extensions.Collections.Tests/CollectionExtensionsTests.cs index 8370c2f..102b6a2 100644 --- a/TryAtSoftware.Extensions.Collections.Tests/CollectionExtensionsTests.cs +++ b/TryAtSoftware.Extensions.Collections.Tests/CollectionExtensionsTests.cs @@ -10,18 +10,25 @@ public class CollectionExtensionsTests [Fact] public void OrEmptyIfNullShouldReturnTheSameCollectionIfItIsNotNull() { - var collection = TestsHelper.GetStandardCollection(); - var result = collection.OrEmptyIfNull(); - Assert.NotNull(result); - Assert.Same(collection, result); + var collection = TestsHelper.GetStandardCollection().ToArray(); + var result1 = ((IEnumerable) collection).OrEmptyIfNull(); + var result2 = ((IList) collection).OrEmptyIfNull(); + + Assert.Same(collection, result1); + Assert.Same(collection, result2); } [Fact] public void OrEmptyIfNullShouldReturnEmptyCollectionIfNullIsPassed() { - var result = ((IEnumerable?)null).OrEmptyIfNull(); - Assert.NotNull(result); - Assert.Empty(result); + var result1 = ((IEnumerable?)null).OrEmptyIfNull(); + var result2 = ((IList?)null).OrEmptyIfNull(); + + Assert.NotNull(result1); + Assert.Empty(result1); + + Assert.NotNull(result2); + Assert.Empty(result2); } [Fact] @@ -123,7 +130,7 @@ public void SafeWhereShouldRespectTheProvidedCondition() } [Fact] - public void SetUnionShouldHandleNull() + public void UnionShouldHandleNull() { var result = ((IEnumerable>?)null).Union(); Assert.NotNull(result); @@ -131,7 +138,7 @@ public void SetUnionShouldHandleNull() } [Fact] - public void SetUnionShouldWorkCorrectly() + public void UnionShouldWorkCorrectly() { var standardCollection = TestsHelper.GetStandardCollection().ToArray(); @@ -146,18 +153,25 @@ public void SetUnionShouldWorkCorrectly() [Fact] public void AsReadOnlyCollectionShouldHandleNull() { - var result = ((IEnumerable?)null).AsReadOnlyCollection(); - Assert.NotNull(result); - Assert.Empty(result); + var result1 = ((IEnumerable?)null).AsReadOnlyCollection(); + var result2 = ((IList?)null).AsReadOnlyCollection(); + + Assert.NotNull(result1); + Assert.Empty(result1); + + Assert.NotNull(result2); + Assert.Empty(result2); } [Fact] public void AsReadOnlyCollectionShouldWorkCorrectly() { var standardCollection = TestsHelper.GetStandardCollection().ToArray(); - var readonlyCollection = standardCollection.AsReadOnlyCollection(); - Assert.Equal(standardCollection, readonlyCollection); + var readonlyCollection1 = ((IEnumerable) standardCollection).AsReadOnlyCollection(); + var readonlyCollection2 = ((IList) standardCollection).AsReadOnlyCollection(); + Assert.Equal(standardCollection, readonlyCollection1); + Assert.Equal(standardCollection, readonlyCollection2); } public static IEnumerable GetConcatenateWithTestData() diff --git a/TryAtSoftware.Extensions.Collections/CollectionExtensions.cs b/TryAtSoftware.Extensions.Collections/CollectionExtensions.cs index f6f2433..f0a768b 100644 --- a/TryAtSoftware.Extensions.Collections/CollectionExtensions.cs +++ b/TryAtSoftware.Extensions.Collections/CollectionExtensions.cs @@ -2,6 +2,7 @@ namespace TryAtSoftware.Extensions.Collections; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; /// @@ -31,7 +32,15 @@ public static IEnumerable SafeWhere(this IEnumerable collection, FuncThe type of elements in the collection. /// The extended instance. /// Returns the same collection if it was not null. Else, returns an empty enumerable. - public static IEnumerable OrEmptyIfNull(this IEnumerable? collection) => collection ?? Enumerable.Empty(); + public static IEnumerable OrEmptyIfNull(this IEnumerable? collection) => collection ?? []; + + /// + /// This method will give you an empty enumerable instance if the extended is null. + /// + /// The type of elements in the collection. + /// The extended instance. + /// Returns the same collection if it was not null. Else, returns an empty enumerable. + public static IList OrEmptyIfNull(this IList? collection) => collection ?? new List(capacity: 0); /// /// Use this method to filter out all null values from the extended . @@ -111,5 +120,13 @@ public static HashSet Union(this IEnumerable?>? collections /// The type of elements in the collection. /// The extended instance. /// Returns an containing all elements from the extended in the same order. - public static IReadOnlyCollection AsReadOnlyCollection(this IEnumerable? collection) => collection.OrEmptyIfNull().ToList().AsReadOnly(); + public static IReadOnlyCollection AsReadOnlyCollection(this IEnumerable? collection) => collection.OrEmptyIfNull().ToList().AsReadOnlyCollection(); + + /// + /// Use this method to construct an instance from the extended . + /// + /// The type of elements in the collection. + /// The extended instance. + /// Returns an containing all elements from the extended in the same order. + public static IReadOnlyCollection AsReadOnlyCollection(this IList? list) => new ReadOnlyCollection(list.OrEmptyIfNull()); } \ No newline at end of file