| | 1 | | using Fluorite.Extensions; |
| | 2 | | using Fluorite.Strainer.Models.Sorting; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Strainer.Services.Sorting; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Provides sorting way formatter based on a suffix indicator. |
| | 8 | | /// </summary> |
| | 9 | | public class SuffixSortingWayFormatter : ISortingWayFormatter |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The prefix used to mark by this formatter. |
| | 13 | | /// <para/> |
| | 14 | | /// This field is readonly. |
| | 15 | | /// </summary> |
| | 16 | | public const string AscendingSuffix = "_asc"; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// The suffix used to mark by this formatter. |
| | 20 | | /// <para/> |
| | 21 | | /// This field is readonly. |
| | 22 | | /// </summary> |
| | 23 | | public const string DescendingSuffix = "_desc"; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="SuffixSortingWayFormatter"/> |
| | 27 | | /// class. |
| | 28 | | /// </summary> |
| 21 | 29 | | public SuffixSortingWayFormatter() |
| | 30 | | { |
| | 31 | |
|
| 21 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <inheritdoc/> |
| | 35 | | /// <exception cref="ArgumentNullException"> |
| | 36 | | /// <paramref name="input"/> is <see langword="null"/>. |
| | 37 | | /// </exception> |
| | 38 | | public string Format(string input, SortingWay sortingWay) |
| | 39 | | { |
| 8 | 40 | | Guard.Against.Null(input); |
| | 41 | |
|
| 7 | 42 | | if (string.IsNullOrWhiteSpace(input)) |
| | 43 | | { |
| 4 | 44 | | return input; |
| | 45 | | } |
| | 46 | |
|
| 3 | 47 | | return input + GetSuffix(sortingWay); |
| | 48 | |
|
| | 49 | | static string GetSuffix(SortingWay sortingWay) |
| | 50 | | { |
| 3 | 51 | | return sortingWay switch |
| 3 | 52 | | { |
| 1 | 53 | | SortingWay.Descending => DescendingSuffix, |
| 1 | 54 | | SortingWay.Ascending => AscendingSuffix, |
| 1 | 55 | | _ => throw new ArgumentException($"{nameof(sortingWay)} with value '{sortingWay}' is not supported."), |
| 3 | 56 | | }; |
| | 57 | | } |
| | 58 | | } |
| | 59 | |
|
| | 60 | | /// <inheritdoc/> |
| | 61 | | /// <exception cref="ArgumentNullException"> |
| | 62 | | /// <paramref name="input"/> is <see langword="null"/>. |
| | 63 | | /// </exception> |
| | 64 | | public SortingWay? GetSortingWay(string input) |
| | 65 | | { |
| 5 | 66 | | Guard.Against.Null(input); |
| | 67 | |
|
| 4 | 68 | | if (input.EndsWith(DescendingSuffix)) |
| | 69 | | { |
| 1 | 70 | | return SortingWay.Descending; |
| | 71 | | } |
| | 72 | |
|
| 3 | 73 | | if (input.EndsWith(AscendingSuffix)) |
| | 74 | | { |
| 1 | 75 | | return SortingWay.Ascending; |
| | 76 | | } |
| | 77 | |
|
| 2 | 78 | | return null; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | /// <inheritdoc/> |
| | 82 | | /// <exception cref="ArgumentNullException"> |
| | 83 | | /// <paramref name="input"/> is <see langword="null"/>. |
| | 84 | | /// </exception> |
| | 85 | | /// <exception cref="ArgumentException"> |
| | 86 | | /// <paramref name="sortingWay"/> is <see cref="SortingWay.Unknown"/>. |
| | 87 | | /// </exception> |
| | 88 | | public string Unformat(string input, SortingWay sortingWay) |
| | 89 | | { |
| 8 | 90 | | Guard.Against.Null(input); |
| | 91 | |
|
| 7 | 92 | | if (string.IsNullOrEmpty(input)) |
| | 93 | | { |
| 1 | 94 | | return input; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | switch (sortingWay) |
| | 98 | | { |
| | 99 | | case SortingWay.Ascending: |
| 4 | 100 | | input = input.TrimEndOnce(AscendingSuffix); |
| 4 | 101 | | break; |
| | 102 | | case SortingWay.Descending: |
| 2 | 103 | | input = input.TrimEndOnce(DescendingSuffix); |
| | 104 | | break; |
| | 105 | | } |
| | 106 | |
|
| 6 | 107 | | return input; |
| | 108 | | } |
| | 109 | | } |