diff --git a/src/CollectionExtensions.cs b/src/CollectionExtensions.cs index 69fd4fc..2fe500f 100644 --- a/src/CollectionExtensions.cs +++ b/src/CollectionExtensions.cs @@ -10,6 +10,7 @@ public static class CollectionExtensions /// public static void AddRange(this ICollection self, IEnumerable items) { + ArgumentNullException.ThrowIfNull(self); ArgumentNullException.ThrowIfNull(items); switch (self) { @@ -27,4 +28,24 @@ public static void AddRange(this ICollection self, IEnumerable items) break; } } + + /// + /// Removes the list item at the specified index. + /// + public static void RemoveAt(this IList self, Index index) + { + ArgumentNullException.ThrowIfNull(self); + var offset = index.GetOffset(self.Count); + self.RemoveAt(offset); + } + + /// + /// Removes the list item at the specified range. + /// + public static void RemoveRange(this List self, Range range) + { + ArgumentNullException.ThrowIfNull(self); + var (offset, length) = range.GetOffsetAndLength(self.Count); + self.RemoveRange(offset, length); + } } diff --git a/src/EnumerablePlus/LinqEnumerablePlus.cs b/src/EnumerablePlus/LinqEnumerablePlus.cs index 4d4a3cd..60b95ea 100644 --- a/src/EnumerablePlus/LinqEnumerablePlus.cs +++ b/src/EnumerablePlus/LinqEnumerablePlus.cs @@ -253,25 +253,25 @@ public static IEnumerable Shuffle(this IEnumerable source, Random? rand switch (source) { case ICollection collection: - { - var copy = collection.ToArray(); - random.Shuffle(copy); - return copy; - } + { + var copy = collection.ToArray(); + random.Shuffle(copy); + return copy; + } case IReadOnlyCollection readOnly: - { - int count = readOnly.Count; - if (count == 0) - return Array.Empty(); - - var result = new T[count]; - var index = 0; - foreach (var item in readOnly) - result[index++] = item; - - random.Shuffle(result); - return result; - } + { + int count = readOnly.Count; + if (count == 0) + return Array.Empty(); + + var result = new T[count]; + var index = 0; + foreach (var item in readOnly) + result[index++] = item; + + random.Shuffle(result); + return result; + } default: return source.OrderBy(_ => random.Next()); } diff --git a/src/RangeExtension.cs b/src/RangeExtension.cs index 3cc676e..dd623e5 100644 --- a/src/RangeExtension.cs +++ b/src/RangeExtension.cs @@ -137,8 +137,8 @@ public static IEnumerable SelectMany( Func project) { foreach (var n1 in range) - foreach (var n2 in projection(n1)) - yield return project(n1, n2); + foreach (var n2 in projection(n1)) + yield return project(n1, n2); } /// diff --git a/tests/CSharpPlus.Tests/CollectionExtensionsTests.cs b/tests/CSharpPlus.Tests/CollectionExtensionsTests.cs new file mode 100644 index 0000000..8725340 --- /dev/null +++ b/tests/CSharpPlus.Tests/CollectionExtensionsTests.cs @@ -0,0 +1,60 @@ +namespace CSharpPlus.Tests; + +#pragma warning disable S6605 +#pragma warning disable S6602 + +public class CollectionExtensionsTests +{ + [Test] + public void ShouldAddRange() + { + List currentItems = [1, 2, 3]; + int[] newItems = [4, 5, 6]; + int[] expected = [1, 2, 3, 4, 5, 6]; + + currentItems.AddRange(newItems); + currentItems.Should().BeEquivalentTo(expected); + } + + [Test] + public void ShouldRemoveRange() + { + List currentItems = [1, 2, 3, 4, 5, 6]; + int[] expected = [1, 2, 3]; + var removeRange = 3..6; + + currentItems.RemoveRange(removeRange); + currentItems.Should().BeEquivalentTo(expected); + } + + [Test] + public void ShouldRemoveRangeMid() + { + List currentItems = [1, 2, 3, 4, 5, 6]; + int[] expected = [1, 6]; + var removeRange = 1..^1; + + currentItems.RemoveRange(removeRange); + currentItems.Should().BeEquivalentTo(expected); + } + + [Test] + public void ShouldRemoveLast() + { + List currentItems = [1, 2, 3, 4]; + int[] expected = [1, 2, 3]; + + currentItems.RemoveAt(^1); + currentItems.Should().BeEquivalentTo(expected); + } + + [Test] + public void ShouldRemoveBeforeLast() + { + List currentItems = [1, 2, 3, 4]; + int[] expected = [1, 2, 4]; + + currentItems.RemoveAt(^2); + currentItems.Should().BeEquivalentTo(expected); + } +}