diff --git a/README.md b/README.md index 9330563..80579fb 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,8 @@ To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implme - Description (enum) - Description(Enum) - Parse -- Parse +- Parse + ## StringExtensions - IsNotNullOrEmpty @@ -72,7 +73,8 @@ To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implme ## ListExtensions - ToDelimitedList - ToUpper -- AddRange - `IList.AddRange(IList)` +- AddRange - `IList.AddRange(IList)` +- AddUnlessBlank IList.AddUnlessBlank(string) ## JsonExtensions For both of these extension methods I'm using the `Newtonsoft.Json` library. I'm planning on migrating to `System.Text.Json` as soon as it is viable. Right now, Newtonsoft is easeir to serialize enums to thier ToString() value rather than index, and to deserialize the same. For example, by default - an enum is serialized to the index of the value. So an enum with (High, Med, Low) values would otherwise be serialiezed to a 2, instead of to "Med". Serializing to "Med" is my prefered behavior. I will continue to evailuate `System.Text.Json` against these unit tests and most likely migrate at some point. @@ -94,3 +96,4 @@ Merges to `main` publish to nuget as a major release. - 1.3.1 - suppressed some test warnings and updated the GH workflows - 1.4.0 - added `CreatedDiretory` and `DeleteDirectory` to `IFileIO` - 1.5.0 - Added package logo and `ToSafeString` for `ObjectExtensions` +- 1.6.0 - Added AddUnlessBlank to list extensions diff --git a/src/ExtensionTests/ListExtensionTests.cs b/src/ExtensionTests/ListExtensionTests.cs index db570bb..781a1bc 100644 --- a/src/ExtensionTests/ListExtensionTests.cs +++ b/src/ExtensionTests/ListExtensionTests.cs @@ -30,5 +30,26 @@ public void ShouldCombineLists() l2.Count.Should().Be(2); } + + [Fact] + public void AddUnlessBlank_shouldNotAddBlankItems() + { + var myList = new List(); + + myList.AddUnlessBlank("one"); + myList.AddUnlessBlank("two"); + myList.AddUnlessBlank("three"); + + myList.Count.Should().Be(3); + + myList.AddUnlessBlank(string.Empty); + myList.Count.Should().Be(3); + + myList.AddUnlessBlank(""); + myList.Count.Should().Be(3); + + myList.AddUnlessBlank(null); + myList.Count.Should().Be(3); + } } diff --git a/src/Extensions/Extensions.csproj b/src/Extensions/Extensions.csproj index 89ba40e..4d26ced 100644 --- a/src/Extensions/Extensions.csproj +++ b/src/Extensions/Extensions.csproj @@ -10,7 +10,7 @@ Debug;Release;NET7 Calebs.Extensions Calebs.Extensions - 1.5.0 + 1.6.0 true Caleb Jenkins Caleb Jenkins diff --git a/src/Extensions/ListExtensions.cs b/src/Extensions/ListExtensions.cs index 6486b65..6dd64f1 100644 --- a/src/Extensions/ListExtensions.cs +++ b/src/Extensions/ListExtensions.cs @@ -18,6 +18,16 @@ public static void ToUpper(this IList list) } } + public static void AddUnlessBlank(this IList list, string value) + { + if(value.IsNullOrEmpty()) + { + return; + } + + list.Add(value); + } + public static void AddRange(this IList list, IEnumerable items) { if (list == null) throw new ArgumentNullException(nameof(list));