Skip to content

Commit

Permalink
Merge pull request #1 from Jhakiz/dev
Browse files Browse the repository at this point in the history
Adding WherefIf support
  • Loading branch information
Jhakiz authored May 8, 2022
2 parents a5d5452 + 95ad5eb commit 19155f4
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 1 deletion.
106 changes: 106 additions & 0 deletions Lincolle4net.Tests/CollectionsUnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Collections.Generic;
using System.Linq;

namespace Lincolle4net.Tests
{
[TestClass]
public class CollectionsUnitTest
{
[TestMethod]
public void ValuedEnumerableFilter()
{
var filter = "DRC";
var countries = (new List<string>() { "USA", "DRC", "Uganda" })
.AsEnumerable()
.WhereIf(!string.IsNullOrEmpty(filter), c => c == filter);

Assert.IsNotNull(filter);
Assert.AreEqual(countries.Count(), 1);
}

[TestMethod]
public void NullOrEmptyEnumerableFilter()
{
var filter = string.Empty;
var countries = (new List<string>() { "DRC", "Uganda", "USA" })
.AsEnumerable()
.WhereIf(!string.IsNullOrEmpty(filter), c => c == filter);

Assert.IsTrue(string.IsNullOrEmpty(filter));
Assert.AreEqual(countries.Count(), 3);
}

[TestMethod]
public void NullOrEmptyListFilter()
{
var filter = string.Empty;
var countries = (new List<string>() { "DRC", "Uganda", "USA" })
.WhereIf(!string.IsNullOrEmpty(filter), c => c == filter);

Assert.IsTrue(string.IsNullOrEmpty(filter));
Assert.AreEqual(countries.Count(), 3);
}

[TestMethod]
public void ValuedCollectionFilter()
{
var filter = "da";
var data = new List<string>() { "Canada", "DRC", "Uganda", "USA" };

var countries = ((ICollection<string>)(data))
.WhereIf(!string.IsNullOrEmpty(filter), c => c.Contains(filter));

Assert.IsFalse(string.IsNullOrEmpty(filter));
Assert.AreEqual(countries.Count(), 2);
}

[TestMethod]
public void ValuedDictionaryFilter()
{
var dic = new Dictionary<string, string>
{
{ "test", "val" },
{ "test2", "val2" }
};
var filter = "da";

var data = dic
.WhereIf(!string.IsNullOrEmpty(filter), c => c.Value == filter);

Assert.IsFalse(string.IsNullOrEmpty(filter));
Assert.AreEqual(data.Count(), 0);

filter = "val";
data = dic
.WhereIf(!string.IsNullOrEmpty(filter), c => c.Value == filter);

Assert.AreEqual(data.Count(), 1);
Assert.AreNotEqual(data.Count(), 2);
}

[TestMethod]
public void ValuedStackFilter()
{
Stack<int> stk = new Stack<int>();
stk.Push(1);
stk.Push(2);
stk.Push(3);
//
var filter = 2;

var data = stk
.WhereIf(filter > 0, c => c == filter);

Assert.AreEqual(data.Count(), 1);

filter = 3;
data = stk
.WhereIf(filter > 0, c => c < filter);

Assert.AreEqual(data.Count(), 2);
}

}
}
21 changes: 21 additions & 0 deletions Lincolle4net.Tests/Lincolle4net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Lincolle4net\Lincolle4net.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions Lincolle4net.Tests/LinqsUnitTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Collections.Generic;
using System.Linq;

namespace Lincolle4net.Tests
{
[TestClass]
public class LinqsUnitTest
{

[TestMethod]
public void ValuedQueryableFilter()
{
var filter = "DRC";
var countries = (new List<string>() { "USA", "DRC", "Uganda" })
.AsQueryable()
.WhereIf(!string.IsNullOrEmpty(filter), c => c == filter);

Assert.IsNotNull(filter);
Assert.AreEqual(countries.Count(), 1);
}

[TestMethod]
public void NullOrEmptyQueryableFilter()
{
var filter = string.Empty;
var countries = (new List<string>() { "DRC", "Uganda", "USA" })
.AsQueryable()
.WhereIf(!string.IsNullOrEmpty(filter), c => c == filter);

Assert.IsTrue(string.IsNullOrEmpty(filter));
Assert.AreEqual(countries.Count(), 3);
}
}
}
31 changes: 31 additions & 0 deletions Lincolle4net.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lincolle4net", "Lincolle4net\Lincolle4net.csproj", "{73DEE913-6891-48DA-A336-BCAD599BCF9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lincolle4net.Tests", "Lincolle4net.Tests\Lincolle4net.Tests.csproj", "{F338A189-9E49-4DEB-9F78-F655E7F371F9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{73DEE913-6891-48DA-A336-BCAD599BCF9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73DEE913-6891-48DA-A336-BCAD599BCF9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73DEE913-6891-48DA-A336-BCAD599BCF9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73DEE913-6891-48DA-A336-BCAD599BCF9C}.Release|Any CPU.Build.0 = Release|Any CPU
{F338A189-9E49-4DEB-9F78-F655E7F371F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F338A189-9E49-4DEB-9F78-F655E7F371F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F338A189-9E49-4DEB-9F78-F655E7F371F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F338A189-9E49-4DEB-9F78-F655E7F371F9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7D8FEE18-19FC-4F93-999E-C05013AFEDC8}
EndGlobalSection
EndGlobal
46 changes: 46 additions & 0 deletions Lincolle4net/Collections/WhereIfExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace System.Collections.Generic
{
public static class WhereIfExtension
{
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> input, bool condition, Func<T, bool> predicate)
{
return condition ?
input.Where(predicate) :
input;
}

public static IEnumerable<T> WhereIf<T>(this IList<T> input, bool condition, Func<T, bool> predicate)
{
return condition ?
input.Where(predicate) :
input;
}

public static IEnumerable<T> WhereIf<T>(this ICollection<T> input, bool condition, Func<T, bool> predicate)
{
return condition ?
input.Where(predicate) :
input;
}

public static IEnumerable<T> WhereIf<T>(this Stack<T> input, bool condition, Func<T, bool> predicate)
{
return condition ?
input.Where(predicate) :
input;
}

public static Dictionary<TKey, TValue> WhereIf<TKey, TValue>(this Dictionary<TKey, TValue> input, bool condition, Func<KeyValuePair<TKey, TValue>, bool> predicate)
{
if (input is null)
throw new ArgumentNullException(nameof(input));

return condition ?
Enumerable
.Where(input, predicate)
.ToDictionary(item => item.Key, item => item.Value) :
input;
}

}
}
9 changes: 9 additions & 0 deletions Lincolle4net/Lincolle4net.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
14 changes: 14 additions & 0 deletions Lincolle4net/Linqs/WhereIfExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace System.Linq
{
public static class WhereIfExtension
{
public static IEnumerable<T> WhereIf<T>(this IQueryable<T> input, bool condition, Func<T, bool> predicate)
where T : class
{
return condition ?
input.Where(predicate) :
input;
}

}
}
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
# Lincolle4net
# Lincolle4net

By importing this library in your project, you are going to access some methods as extensions which will help you while filtering Linq and Collections in C#.

For now the current version supports only `WhereIf` on **List**, **Enumerable**, **Queryable**, **Stack**, **Dictionary** and **Collection**.

```csharp
var filter = "DRC";
var countries = (new List<string>() { "USA", "Canada" ,"DRC", "Uganda", "China" })
.AsQueryable()
.WhereIf(!string.IsNullOrEmpty(filter), c => c == filter);
```

Note that, on each call of `WhereIf`, an inline `IF` condition is applied on specified generics. Without using this package, the above code could be written as below:

```csharp
var filter = "DRC";
var countries = (new List<string>() { "USA", "Canada" ,"DRC", "Uganda", "China" })
.AsQueryable();

if(!string.IsNullOrEmpty(filter))
{
countries.Where(c => c == filter);
}

```

Use this library while filtering Dictionaries.

```csharp
var dic = new Dictionary<string, string>
{
{ "test", "val" },
{ "test2", "val2" }
};
var filter = "va";

var data = dic.WhereIf(!string.IsNullOrEmpty(filter), c => c.Value.StartsWith(filter));
```

### Roadmap

- [x] **WhereIf**
- [ ] AddIf
- [ ] RemoveIf

### Contribution

The project is opened for community contribution. To do this, you've to fork this project, perform all operation, finaly push changes on your forked project.

> Create a pull request if you want your changes to be merged to the main branch.

0 comments on commit 19155f4

Please sign in to comment.