|
| 1 | +namespace DevTKSS.Extensions.Uno.Storage.Enumerable; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Provides <see cref="IEnumerable{TResult}"/> extension methods for working with <see cref="IStorage"/> |
| 5 | +/// </summary> |
| 6 | +public static class EnumerableExtensions |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Selects string typed items from the input collection based on the specified ranges. |
| 10 | + /// </summary> |
| 11 | + /// <param name="source">The collection of string typed items to select from.</param> |
| 12 | + /// <param name="ranges"> |
| 13 | + /// A collection of ranges, where each range is a tuple containing a start and end index. |
| 14 | + /// The start index specifies the first item to include, and the end index specifies the last item to include. |
| 15 | + /// </param> |
| 16 | + /// <param name="isNullBased"> |
| 17 | + /// Indicates whether the range indices are 0-based (<c>true</c>) or 1-based (<c>false</c>). |
| 18 | + /// If <c>true</c>, the start and end indices are treated as 0-based; otherwise, they are treated as 1-based. |
| 19 | + /// </param> |
| 20 | + /// <returns> |
| 21 | + /// An enumerable collection of strings, where each string represents the items within a specified range. |
| 22 | + /// If a range is invalid (e.g. start is greater than the last one), an empty string is returned for that range. |
| 23 | + /// </returns> |
| 24 | + public static IEnumerable<string> SelectItemsByRanges(this IEnumerable<string> source, IEnumerable<(int Start, int End)> ranges, bool isNullBased = true) |
| 25 | + { |
| 26 | + if (source is null || ranges is null) |
| 27 | + { |
| 28 | + yield return string.Empty; |
| 29 | + yield break; |
| 30 | + } |
| 31 | + |
| 32 | + if (!ranges.Any()) |
| 33 | + { |
| 34 | + yield return source.JoinBy(Environment.NewLine); |
| 35 | + yield break; |
| 36 | + } |
| 37 | + |
| 38 | + foreach (var range in ranges) |
| 39 | + { |
| 40 | + yield return source.GetItemsWithinRange(range, isNullBased); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Retrieves the concatenated items within the specified range as one single <see langword="string"/> joined by <see cref="Environment.NewLine"/> character. |
| 46 | + /// </summary> |
| 47 | + /// <param name="source">The <see cref="IEnumerable{TData}"/> to select from.</param> |
| 48 | + /// <param name="range"> |
| 49 | + /// A tuple containing the start and end indices of the range as <see langword="int"/>. |
| 50 | + /// The start index specifies the first item to include, and the end index specifies the last item to include. |
| 51 | + /// </param> |
| 52 | + /// <param name="isNullBased"> |
| 53 | + /// Indicates whether the range indices are 0-based (<c>true</c>) or 1-based (<c>false</c>). |
| 54 | + /// If <c>true</c>, the start and end indices are treated as 0-based; otherwise, they are treated as 1-based. |
| 55 | + /// </param> |
| 56 | + /// <returns> |
| 57 | + /// A string containing the string typed items of <paramref name="source"/> within the specified range, joined by the system's newline character. |
| 58 | + /// </returns> |
| 59 | + public static string GetItemsWithinRange(this IEnumerable<string> source, (int Start, int End) range, bool isNullBased = true) // TODO: Consider to limit int to min 0 value instead of implicit allowing negative. |
| 60 | + { |
| 61 | + if (source is null) |
| 62 | + { |
| 63 | + return string.Empty; |
| 64 | + } |
| 65 | + |
| 66 | + var list = source as IList<string> ?? [.. source]; |
| 67 | + if (list.Count == 0) |
| 68 | + { |
| 69 | + return string.Empty; |
| 70 | + } |
| 71 | + |
| 72 | + var startIndex = Math.Clamp( |
| 73 | + value: range.Start - (isNullBased ? 0 : 1), |
| 74 | + min: 0, |
| 75 | + max: list.Count); |
| 76 | + |
| 77 | + var endIndex = Math.Clamp( |
| 78 | + value: range.End - (isNullBased ? 0 : 1), |
| 79 | + min: startIndex, |
| 80 | + max: list.Count); // Ensure 'End' does not exceed available lines |
| 81 | + |
| 82 | + return list.Skip(startIndex) |
| 83 | + .Take(endIndex - startIndex) |
| 84 | + .JoinBy(Environment.NewLine); |
| 85 | + } |
| 86 | +} |
0 commit comments