Skip to content

Commit

Permalink
empty if null extension for readonly collections
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed May 22, 2023
1 parent 5a49fce commit 69177a3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/EnumerablePlus/LinqEnumerablePlus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public static void ForEach<T>(this IEnumerable<T> source, Action<T, int> action)
var rnd = random ?? Random.Shared;
return source switch
{
IReadOnlyCollection<T> { Count: 0 } => default,
IReadOnlyList<T> { Count: > 0 } list => list[rnd.Next(list.Count)],
IReadOnlyCollection<T> {Count: 0} => default,
IReadOnlyList<T> {Count: > 0} list => list[rnd.Next(list.Count)],
_ => source.Shuffle(rnd).FirstOrDefault(defaultValue),
};
}
Expand Down Expand Up @@ -207,4 +207,37 @@ enumerable switch
IEnumerable<T> second, Func<T, TKey> keySelector,
IEqualityComparer<TKey>? comparer = null) =>
first.IntersectBy(second.Select(keySelector), keySelector, comparer);


/// <summary>
/// Return empty if collection is null
/// </summary>
/// <param name="enumerable"></param>
/// <returns></returns>
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T>? enumerable) =>
enumerable ?? Enumerable.Empty<T>();

/// <summary>
/// Return empty if array is null
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static T[] EmptyIfNull<T>(this T[]? array) =>
array ?? Array.Empty<T>();

/// <summary>
/// Return empty if array is null
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static IReadOnlyCollection<T> EmptyIfNull<T>(this IReadOnlyCollection<T>? array) =>
array ?? Array.Empty<T>();

/// <summary>
/// Return empty if array is null
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
public static IReadOnlyList<T> EmptyIfNull<T>(this IReadOnlyList<T>? array) =>
array ?? Array.Empty<T>();
}
17 changes: 17 additions & 0 deletions tests/CSharpPlus.Tests/EnumerablePlus/LinqEnumerablePlusTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ReSharper disable PossibleMultipleEnumeration

using System.Collections.ObjectModel;
using FsCheck;

namespace CSharpPlus.Tests.EnumerablePlus;
Expand Down Expand Up @@ -228,4 +229,20 @@ public void ShouldIntersectBySelector()
both1, both2,
});
}

[Test]
public void ShouldBeEmptyIfNull() =>
(null as IEnumerable<int>).EmptyIfNull().Should().BeEmpty();

[Test]
public void ShouldBeEmptyArrayIfNull() =>
(null as int[]).EmptyIfNull().Should().BeEmpty();

[Test]
public void ShouldBeEmptyCollectionIfNull() =>
(null as ReadOnlyCollection<int>).EmptyIfNull().Should().BeEmpty();

[Test]
public void ShouldBeEmptyListIfNull() =>
(null as IReadOnlyList<int>).EmptyIfNull().Should().BeEmpty();
}

0 comments on commit 69177a3

Please sign in to comment.