feat: IEnumerable<T>.ConditionalAppend(Maybe<T>)
This commit is contained in:
parent
b59b5655bf
commit
de8d2f393c
|
@ -20,4 +20,30 @@ public class EnumerableExtensionTests
|
||||||
[TestCase(new object[] { "hi", "there" }, false, "there", ExpectedResult = new object[] { "hi", "there" })]
|
[TestCase(new object[] { "hi", "there" }, false, "there", ExpectedResult = new object[] { "hi", "there" })]
|
||||||
public object[] TestConditionalWhere(object[] input, bool condition, object valueToKeep) =>
|
public object[] TestConditionalWhere(object[] input, bool condition, object valueToKeep) =>
|
||||||
input.ConditionalWhere(condition, x => x.Equals(valueToKeep)).ToArray();
|
input.ConditionalWhere(condition, x => x.Equals(valueToKeep)).ToArray();
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestConditionalAppendWithMaybe_HasAppendedItem_WhenMaybeHasValue()
|
||||||
|
{
|
||||||
|
var baseArray = new[] { 0, 1, 2 };
|
||||||
|
|
||||||
|
var result = baseArray
|
||||||
|
.ConditionalAppend(Maybe.Just(3))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
Assert.That(result, Has.Length.EqualTo(4));
|
||||||
|
Assert.That(result, Is.EquivalentTo(new[] { 0, 1, 2, 3 }));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestConditionalAppendWithMaybe_DoesNotHaveAppendedItem_WhenMaybeIsEmpty()
|
||||||
|
{
|
||||||
|
var baseArray = new[] { 0, 1, 2 };
|
||||||
|
|
||||||
|
var result = baseArray
|
||||||
|
.ConditionalAppend(Maybe.Empty<int>())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
Assert.That(result, Has.Length.EqualTo(3));
|
||||||
|
Assert.That(result, Is.EquivalentTo(new[] { 0, 1, 2 }));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,9 @@ public static class EnumerableExtensions
|
||||||
|
|
||||||
public static IEnumerable<T> ConditionalAppend<T>(this IEnumerable<T> enumerable, bool condition, T second) =>
|
public static IEnumerable<T> ConditionalAppend<T>(this IEnumerable<T> enumerable, bool condition, T second) =>
|
||||||
condition ? enumerable.Append(second) : enumerable;
|
condition ? enumerable.Append(second) : enumerable;
|
||||||
|
|
||||||
|
public static IEnumerable<T> ConditionalAppend<T>(this IEnumerable<T> enumerable, Maybe<T> item) =>
|
||||||
|
item.HasValue ? enumerable.Append(item.Value) : enumerable;
|
||||||
|
|
||||||
public static IEnumerable<T> ConditionalWhere<T>(this IEnumerable<T> enumerable, bool condition, Func<T, bool> pred) =>
|
public static IEnumerable<T> ConditionalWhere<T>(this IEnumerable<T> enumerable, bool condition, Func<T, bool> pred) =>
|
||||||
!condition ? enumerable : enumerable.Where(pred);
|
!condition ? enumerable : enumerable.Where(pred);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user