mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-07-01 02:46:37 +02:00
44 lines
2 KiB
C#
44 lines
2 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace TINK.Model.Connector.Filter
|
|
{
|
|
public static class IntersectGroupFilterHelper
|
|
{
|
|
/// <summary>
|
|
/// Transforms a group (each element consists of an operator prefix and a numeric bike category) to bike category enumeration (numeric elements).
|
|
/// </summary>
|
|
/// <param name="group">Group to transform.</param>
|
|
/// <returns>Enumeration of numeric bike categories.</returns>
|
|
public static IEnumerable<string> ToBikeCategory(this IEnumerable<string> group)
|
|
=> group?.Select(x => x.GetBikeCategory())?.Where(x => !string.IsNullOrEmpty(x))
|
|
?? new List<string>();
|
|
|
|
/// <summary>
|
|
/// Extracts bike group category umber from a group string.
|
|
/// </summary>
|
|
/// <param name="group">Group to transform. Example KN_300101 (Stadtrad located in Konstanz), FR_300102 (Lastenrad located in Freiburg).</param>
|
|
/// <returns>Enumeration of numeric bike categories.</returns>
|
|
public static string GetBikeCategory(this string group)
|
|
=> Regex.Match(group, "[0-9]+")?.Value ?? string.Empty;
|
|
|
|
/// <summary>
|
|
/// Intersects two goups only taking into accout the numeric bike group category part.
|
|
/// </summary>
|
|
/// <param name="group">Group to filter.</param>
|
|
/// <param name="filter">Filter to apply</param>
|
|
public static IEnumerable<string> IntersectByGoupId(this IEnumerable<string> group, IEnumerable<string> filter)
|
|
=> group.Where(x => filter.ContainsGroupId(x));
|
|
|
|
/// <summary>
|
|
/// Gets if group contains a filter element.
|
|
/// </summary>
|
|
/// <param name="group"></param>
|
|
/// <param name="filterElement"></param>
|
|
/// <returns></returns>
|
|
public static bool ContainsGroupId(this IEnumerable<string> group, string filterElement)
|
|
=> group.ToBikeCategory().Contains(filterElement.GetBikeCategory());
|
|
}
|
|
}
|