using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace TINK.Model.Connector.Filter { public static class IntersectGroupFilterHelper { /// /// Transforms a group (each element consists of an operator prefix and a numeric bike category) to bike category enumeration (numeric elements). /// /// Group to transform. /// Enumeration of numeric bike categories. public static IEnumerable ToBikeCategory(this IEnumerable group) => group?.Select(x => x.GetBikeCategory())?.Where(x => !string.IsNullOrEmpty(x)) ?? new List(); /// /// Extracts bike group category umber from a group string. /// /// Group to transform. Example KN_300101 (Stadtrad located in Konstanz), FR_300102 (Lastenrad located in Freiburg). /// Enumeration of numeric bike categories. public static string GetBikeCategory(this string group) => Regex.Match(group, "[0-9]+")?.Value ?? string.Empty; /// /// Intersects two goups only taking into accout the numeric bike group category part. /// /// Group to filter. /// Filter to apply public static IEnumerable IntersectByGoupId(this IEnumerable group, IEnumerable filter) => group.Where(x => filter.ContainsGroupId(x)); /// /// Gets if group contains a filter element. /// /// /// /// public static bool ContainsGroupId(this IEnumerable group, string filterElement) => group.ToBikeCategory().Contains(filterElement.GetBikeCategory()); } }