< Summary

Information
Class: Fluorite.Strainer.Services.Sorting.SuffixSortingWayFormatter
Assembly: Fluorite.Strainer
File(s): /builds/fluorite/strainer/src/Strainer/Services/Sorting/SuffixSortingWayFormatter.cs
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 109
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Format(...)100%22100%
GetSuffix()100%44100%
GetSortingWay(...)100%44100%
Unformat(...)100%66100%

File(s)

/builds/fluorite/strainer/src/Strainer/Services/Sorting/SuffixSortingWayFormatter.cs

#LineLine coverage
 1using Fluorite.Extensions;
 2using Fluorite.Strainer.Models.Sorting;
 3
 4namespace Fluorite.Strainer.Services.Sorting;
 5
 6/// <summary>
 7/// Provides sorting way formatter based on a suffix indicator.
 8/// </summary>
 9public 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>
 2129    public SuffixSortingWayFormatter()
 30    {
 31
 2132    }
 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    {
 840        Guard.Against.Null(input);
 41
 742        if (string.IsNullOrWhiteSpace(input))
 43        {
 444            return input;
 45        }
 46
 347        return input + GetSuffix(sortingWay);
 48
 49        static string GetSuffix(SortingWay sortingWay)
 50        {
 351            return sortingWay switch
 352            {
 153                SortingWay.Descending => DescendingSuffix,
 154                SortingWay.Ascending => AscendingSuffix,
 155                _ => throw new ArgumentException($"{nameof(sortingWay)} with value '{sortingWay}' is not supported."),
 356            };
 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    {
 566        Guard.Against.Null(input);
 67
 468        if (input.EndsWith(DescendingSuffix))
 69        {
 170            return SortingWay.Descending;
 71        }
 72
 373        if (input.EndsWith(AscendingSuffix))
 74        {
 175            return SortingWay.Ascending;
 76        }
 77
 278        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    {
 890        Guard.Against.Null(input);
 91
 792        if (string.IsNullOrEmpty(input))
 93        {
 194            return input;
 95        }
 96
 97        switch (sortingWay)
 98        {
 99            case SortingWay.Ascending:
 4100                input = input.TrimEndOnce(AscendingSuffix);
 4101                break;
 102            case SortingWay.Descending:
 2103                input = input.TrimEndOnce(DescendingSuffix);
 104                break;
 105        }
 106
 6107        return input;
 108    }
 109}