Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Releade 1.6.0 - AddUnlessBlank in List Extensions #24

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implme
- Description (enum)
- Description<ToDesc>(Enum)
- Parse<T>
- Parse <T, D>
- Parse <T, D>


## StringExtensions
- IsNotNullOrEmpty
Expand All @@ -72,7 +73,8 @@ To use this helper - register `IFileIO` is your `DI` with `FileIO` as the implme
## ListExtensions
- ToDelimitedList
- ToUpper
- AddRange - `IList<T>.AddRange(IList<T>)`
- AddRange - `IList<T>.AddRange(IList<T>)`
- AddUnlessBlank IList<string>.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.
Expand All @@ -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
21 changes: 21 additions & 0 deletions src/ExtensionTests/ListExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,26 @@ public void ShouldCombineLists()

l2.Count.Should().Be(2);
}

[Fact]
public void AddUnlessBlank_shouldNotAddBlankItems()
{
var myList = new List<string>();

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);
}
}

2 changes: 1 addition & 1 deletion src/Extensions/Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Configurations>Debug;Release;NET7</Configurations>
<RootNamespace>Calebs.Extensions</RootNamespace>
<AssemblyName>Calebs.Extensions</AssemblyName>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Caleb Jenkins</Authors>
<Company>Caleb Jenkins</Company>
Expand Down
10 changes: 10 additions & 0 deletions src/Extensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public static void ToUpper(this IList<string> list)
}
}

public static void AddUnlessBlank(this IList<string> list, string value)
{
if(value.IsNullOrEmpty())
{
return;
}

list.Add(value);
}

public static void AddRange<T>(this IList<T> list, IEnumerable<T> items)
{
if (list == null) throw new ArgumentNullException(nameof(list));
Expand Down
Loading