Added InsertAfter extension method.

pull/877/head
Halil ibrahim Kalkan 6 years ago
parent 0853ad4790
commit fb656134d8

@ -32,6 +32,18 @@ namespace System.Collections.Generic
source.Insert(source.Count, item);
}
public static void InsertAfter<T>(this IList<T> source, T existingItem, T item)
{
var index = source.IndexOf(existingItem);
if (index < 0)
{
source.AddFirst(item);
return;
}
source.Insert(index + 1, item);
}
public static void InsertAfter<T>(this IList<T> source, Predicate<T> selector, T item)
{
var index = source.FindIndex(selector);
@ -44,6 +56,17 @@ namespace System.Collections.Generic
source.Insert(index + 1, item);
}
public static void InsertBefore<T>(this IList<T> source, T existingItem, T item)
{
var index = source.IndexOf(existingItem);
if (index < 0)
{
source.AddLast(item);
return;
}
source.Insert(index, item);
}
public static void InsertBefore<T>(this IList<T> source, Predicate<T> selector, T item)
{

@ -11,6 +11,29 @@ namespace System.Collections.Generic
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertAfter(2, 42);
list.Count.ShouldBe(4);
list[0].ShouldBe(1);
list[1].ShouldBe(2);
list[2].ShouldBe(42);
list[3].ShouldBe(3);
list.InsertAfter(3, 43);
list.Count.ShouldBe(5);
list[0].ShouldBe(1);
list[1].ShouldBe(2);
list[2].ShouldBe(42);
list[3].ShouldBe(3);
list[4].ShouldBe(43);
}
[Fact]
public void InsertAfter_With_Predicate()
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertAfter(i => i == 2, 42);
list.Count.ShouldBe(4);
@ -30,7 +53,7 @@ namespace System.Collections.Generic
}
[Fact]
public void InsertAfter_Should_Insert_To_First_If_Not_Found()
public void InsertAfter_With_Predicate_Should_Insert_To_First_If_Not_Found()
{
var list = Enumerable.Range(1, 3).ToList();
@ -48,6 +71,29 @@ namespace System.Collections.Generic
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertBefore(2, 42);
list.Count.ShouldBe(4);
list[0].ShouldBe(1);
list[1].ShouldBe(42);
list[2].ShouldBe(2);
list[3].ShouldBe(3);
list.InsertBefore(1, 43);
list.Count.ShouldBe(5);
list[0].ShouldBe(43);
list[1].ShouldBe(1);
list[2].ShouldBe(42);
list[3].ShouldBe(2);
list[4].ShouldBe(3);
}
[Fact]
public void InsertBefore_With_Predicate()
{
var list = Enumerable.Range(1, 3).ToList();
list.InsertBefore(i => i == 2, 42);
list.Count.ShouldBe(4);

Loading…
Cancel
Save