diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ff7772..96d9c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - X10D: Added `double.LinearToGamma([gamma])` and `float.LinearToGamma([gamma])`. (#60) - X10D: Added `double.GammaToLinear([gamma])` and `float.GammaToLinear([gamma])`. (#60) - X10D: Added `GreatestCommonFactor` for built-in integer types. +- X10D: Added `IEnumerable.ConcatOne(T)`. - X10D: Added `IEnumerable.CountWhereNot(Func)`. - X10D: Added `IEnumerable.FirstWhereNot(Func)`. - X10D: Added `IEnumerable.FirstWhereNotOrDefault(Func)`. diff --git a/X10D.Tests/src/Linq/EnumerableTests.cs b/X10D.Tests/src/Linq/EnumerableTests.cs index 838ec54..e211309 100644 --- a/X10D.Tests/src/Linq/EnumerableTests.cs +++ b/X10D.Tests/src/Linq/EnumerableTests.cs @@ -6,6 +6,37 @@ namespace X10D.Tests.Linq; [TestClass] public class EnumerableTests { + [TestMethod] + public void ConcatOne_ShouldReturnConcatenatedSequence_GivenValidSequenceAndValue() + { + IEnumerable source = new[] {"Hello"}; + string[] expected = {"Hello", "World"}; + + string[] actual = source.ConcatOne("World").ToArray(); + + Assert.AreEqual(2, actual.Length); + CollectionAssert.AreEqual(expected, actual); + } + + [TestMethod] + public void ConcatOne_ShouldReturnSingletonSequence_GivenEmptySequenceAndValidValue() + { + IEnumerable source = Enumerable.Empty(); + string[] expected = {"Foobar"}; + + string[] actual = source.ConcatOne("Foobar").ToArray(); + + Assert.AreEqual(1, actual.Length); + CollectionAssert.AreEqual(expected, actual); + } + + [TestMethod] + public void ConcatOne_ShouldThrowArgumentNullException_GivenNullSource() + { + IEnumerable? source = null; + Assert.ThrowsException(() => source!.ConcatOne("Foobar")); + } + [TestMethod] public void MinMax_ShouldReturnCorrectValues_UsingDefaultComparer() { diff --git a/X10D/src/Linq/EnumerableExtensions.cs b/X10D/src/Linq/EnumerableExtensions.cs index ed0115f..c94f10a 100644 --- a/X10D/src/Linq/EnumerableExtensions.cs +++ b/X10D/src/Linq/EnumerableExtensions.cs @@ -9,6 +9,31 @@ namespace X10D.Linq; /// public static class EnumerableExtensions { + /// + /// Concatenates a single value to the end of a sequence. + /// + /// The source sequence. + /// The value to concatenate to the end of the source sequence. + /// The type of the elements in . + /// + /// An that contains the concatenated elements of the input sequence, and the specified + /// value. + /// + /// is . + public static IEnumerable ConcatOne(this IEnumerable source, TSource value) + { +#if NET6_0_OR_GREATER + ArgumentNullException.ThrowIfNull(source); +#else + if (source is null) + { + throw new ArgumentNullException(nameof(source)); + } +#endif + + return source.Concat(new[] {value}); + } + /// /// Returns the minimum and maximum values in a sequence of values. ///