Skip to content

Commit

Permalink
Added filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Pouwels committed Feb 12, 2023
1 parent 43d0058 commit 2857a52
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 5 deletions.
27 changes: 27 additions & 0 deletions src/ZimLabs.CoreLib/ZimLabs.CoreLib/Common/FilterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace ZimLabs.CoreLib.Common;

/// <summary>
/// The different filter types
/// </summary>
public enum FilterType
{
/// <summary>
/// The value has to contain
/// </summary>
Contains,

/// <summary>
/// The value has to be equals
/// </summary>
Equals,

/// <summary>
/// The value has to starts with
/// </summary>
StartsWith,

/// <summary>
/// The value has to ends with
/// </summary>
EndsWith
}
20 changes: 20 additions & 0 deletions src/ZimLabs.CoreLib/ZimLabs.CoreLib/Core.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Reflection;
using ZimLabs.CoreLib.Common;
using ZimLabs.CoreLib.DataObjects;

namespace ZimLabs.CoreLib;

Expand All @@ -16,4 +18,22 @@ public static string GetBaseDirPath()
var assemblyPath = Assembly.GetExecutingAssembly().Location;
return Path.GetDirectoryName(assemblyPath) ?? "";
}

/// <summary>
/// Checks if the value matches the desired filter
/// </summary>
/// <param name="value">The value</param>
/// <param name="filter">The filter</param>
/// <returns><see langword="true"/> when the filter matches, otherwise <see langword="false"/></returns>
public static bool MatchFilter(string value, FilterEntry filter)
{
return filter.FilterType switch
{
FilterType.Contains => value.Contains(filter.Value, StringComparison.OrdinalIgnoreCase),
FilterType.Equals => value.Equals(filter.Value, StringComparison.OrdinalIgnoreCase),
FilterType.StartsWith => value.StartsWith(filter.Value, StringComparison.OrdinalIgnoreCase),
FilterType.EndsWith => value.EndsWith(filter.Value, StringComparison.OrdinalIgnoreCase),
_ => false
};
}
}
41 changes: 41 additions & 0 deletions src/ZimLabs.CoreLib/ZimLabs.CoreLib/DataObjects/FilterEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ZimLabs.CoreLib.Common;

namespace ZimLabs.CoreLib.DataObjects;

/// <summary>
/// Represents a filter entry
/// </summary>
public class FilterEntry
{
/// <summary>
/// Gets or sets the value to search for
/// </summary>
public string Value { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the desired filter
/// </summary>
public FilterType FilterType { get; set; } = FilterType.Equals;

/// <summary>
/// Gets or sets the original value (with wildcards)
/// </summary>
public string OriginalValue { get; set; } = string.Empty;

/// <summary>
/// Creates a new empty instance of the <see cref="FilterEntry"/>
/// </summary>
public FilterEntry() { }

/// <summary>
/// Creates a new instance of the <see cref="FilterEntry"/>
/// </summary>
/// <param name="value">The value to search for</param>
/// <param name="wildcard">The desired wildcard (optional)</param>
public FilterEntry(string value, string wildcard = "*")
{
Value = value.Replace(wildcard, ""); // Remove the wild card
FilterType = value.ToFilterType(wildcard);
OriginalValue = value;
}
}
48 changes: 47 additions & 1 deletion src/ZimLabs.CoreLib/ZimLabs.CoreLib/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace ZimLabs.CoreLib;
using ZimLabs.CoreLib.Common;
using ZimLabs.CoreLib.DataObjects;

namespace ZimLabs.CoreLib;

/// <summary>
/// Provides several extensions
Expand Down Expand Up @@ -44,6 +47,49 @@ public static int ToInt(this string value, int fallback = 0)
{
return int.TryParse(value, out var result) ? result : fallback;
}

/// <summary>
/// Gets the filter type of the specified value
/// </summary>
/// <param name="value">The value (with wildcards)</param>
/// <param name="wildcard">The desired wildcard (optional)</param>
/// <returns>The filter type</returns>
public static FilterType ToFilterType(this string value, string wildcard = "*")
{
var start = value.StartsWith(wildcard);
var end = value.EndsWith(wildcard);

return start switch
{
true when end => FilterType.Contains, // has a wildcard at the start and the end (*Value*)
true when !end => FilterType.EndsWith, // has a wildcard at the start (*Value)
false when end => FilterType.StartsWith, // has a wildcard at the end (Value*)
_ => FilterType.Equals // has no wildcard (Value)
};
}

/// <summary>
/// Converts the value into a filter entry
/// </summary>
/// <param name="value">The value (with wildcards)</param>
/// <param name="wildcard">The desired wildcard (optional)</param>
/// <returns>The filter entry</returns>
public static FilterEntry ToFilterEntry(this string value, string wildcard = "*")
{
return new FilterEntry(value, wildcard);
}

/// <summary>
/// Converts the list of value into a list of filter entries
/// </summary>
/// <param name="values">The list with the values (with wildcard)</param>
/// <param name="wildcard">The desired wildcard (optional)</param>
/// <returns>The list with the filter</returns>
public static List<FilterEntry> ToFilterList(this IEnumerable<string> values, string wildcard = "*")
{
var tmpList = values.ToList();
return tmpList.Any() ? tmpList.Select(s => s.ToFilterEntry(wildcard)).ToList() : new List<FilterEntry>();
}
#endregion

#region Numbers
Expand Down
8 changes: 4 additions & 4 deletions src/ZimLabs.CoreLib/ZimLabs.CoreLib/ZimLabs.CoreLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<RepositoryUrl>https://github.com/InvaderZim85/ZimLabs.CoreLib</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>helper, extensions</PackageTags>
<PackageReleaseNotes>Added extension to extract a custom attribute</PackageReleaseNotes>
<PackageReleaseNotes>Added filter functions</PackageReleaseNotes>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>2.1.0</AssemblyVersion>
<FileVersion>2.1.0</FileVersion>
<Version>2.1.0</Version>
<AssemblyVersion>2.2.0</AssemblyVersion>
<FileVersion>2.2.0</FileVersion>
<Version>2.2.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
Expand Down

0 comments on commit 2857a52

Please sign in to comment.