• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 13442064295

20 Feb 2025 06:23PM UTC coverage: 59.525% (-1.4%) from 60.884%
13442064295

push

github

pwelter34
add extension methods

384 of 755 branches covered (50.86%)

Branch coverage included in aggregate %.

0 of 32 new or added lines in 3 files covered. (0.0%)

1269 of 2022 relevant lines covered (62.76%)

19.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/MediatR.CommandQuery/Extensions/StringExtensions.cs
1
using System.Diagnostics.CodeAnalysis;
2
using System.Text;
3
using System.Text.RegularExpressions;
4

5
namespace MediatR.CommandQuery.Extensions;
6

7
/// <summary>
8
/// <see cref="String"/> type extension methods
9
/// </summary>
10
public static partial class StringExtensions
11
{
12
    [GeneratedRegex("([A-Z][a-z]*)|([0-9]+)", RegexOptions.ExplicitCapture, matchTimeoutMilliseconds: 1000)]
13
    private static partial Regex WordsExpression();
14

15
    /// <summary>
16
    /// Truncates the specified text.
17
    /// </summary>
18
    /// <param name="text">The text to truncate.</param>
19
    /// <param name="keep">The number of characters to keep.</param>
20
    /// <param name="ellipsis">The ellipsis string to use when truncating. (Default ...)</param>
21
    /// <returns>
22
    /// A truncate string.
23
    /// </returns>
24
    [return: NotNullIfNotNull(nameof(text))]
25
    public static string? Truncate(this string? text, int keep, string ellipsis = "...")
26
    {
NEW
27
        if (string.IsNullOrEmpty(text))
×
NEW
28
            return text;
×
29

NEW
30
        if (text!.Length <= keep)
×
NEW
31
            return text;
×
32

NEW
33
        ellipsis ??= string.Empty;
×
34

NEW
35
        if (text.Length <= keep + ellipsis.Length || keep < ellipsis.Length)
×
NEW
36
            return text[..keep];
×
37

NEW
38
        int prefix = keep - ellipsis.Length;
×
NEW
39
        return string.Concat(text[..prefix], ellipsis);
×
40
    }
41

42
    /// <summary>
43
    /// Indicates whether the specified String object is null or an empty string
44
    /// </summary>
45
    /// <param name="item">A String reference</param>
46
    /// <returns>
47
    ///     <c>true</c> if is null or empty; otherwise, <c>false</c>.
48
    /// </returns>
49
    public static bool IsNullOrEmpty([NotNullWhen(false)] this string? item)
50
    {
NEW
51
        return string.IsNullOrEmpty(item);
×
52
    }
53

54
    /// <summary>
55
    /// Indicates whether a specified string is null, empty, or consists only of white-space characters
56
    /// </summary>
57
    /// <param name="item">A String reference</param>
58
    /// <returns>
59
    ///      <c>true</c> if is null or empty; otherwise, <c>false</c>.
60
    /// </returns>
61
    public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? item)
62
    {
NEW
63
        if (item == null)
×
NEW
64
            return true;
×
65

NEW
66
        for (int i = 0; i < item.Length; i++)
×
NEW
67
            if (!char.IsWhiteSpace(item[i]))
×
NEW
68
                return false;
×
69

NEW
70
        return true;
×
71
    }
72

73
    /// <summary>
74
    /// Determines whether the specified string is not <see cref="IsNullOrEmpty"/>.
75
    /// </summary>
76
    /// <param name="value">The value to check.</param>
77
    /// <returns>
78
    ///   <c>true</c> if the specified <paramref name="value"/> is not <see cref="IsNullOrEmpty"/>; otherwise, <c>false</c>.
79
    /// </returns>
80
    public static bool HasValue([NotNullWhen(true)] this string? value)
81
    {
NEW
82
        return !string.IsNullOrEmpty(value);
×
83
    }
84

85
    /// <summary>
86
    /// Combines two strings with the specified separator.
87
    /// </summary>
88
    /// <param name="first">The first string.</param>
89
    /// <param name="second">The second string.</param>
90
    /// <param name="separator">The separator string.</param>
91
    /// <returns>A string combining the <paramref name="first"/> and <paramref name="second"/> parameters with the <paramref name="separator"/> between them</returns>
92
    [return: NotNullIfNotNull(nameof(first))]
93
    [return: NotNullIfNotNull(nameof(second))]
94
    public static string? Combine(this string? first, string? second, char separator = '/')
95
    {
96
        if (string.IsNullOrEmpty(first))
×
97
            return second;
×
98

99
        if (string.IsNullOrEmpty(second))
×
100
            return first;
×
101

102
        bool hasSeparator = first[^1] == separator || second[0] == separator;
×
103

104
        return hasSeparator
×
105
            ? string.Concat(first, second)
×
106
            : $"{first}{separator}{second}";
×
107
    }
108

109
    /// <summary>
110
    /// Converts a NameIdentifier and spaces it out into words "Name Identifier".
111
    /// </summary>
112
    /// <param name="text">The text value to convert.</param>
113
    /// <returns>The text converted</returns>
114
    [return: NotNullIfNotNull(nameof(text))]
115
    public static string? ToTitle(this string? text)
116
    {
NEW
117
        if (text.IsNullOrEmpty() || text.Length < 2)
×
NEW
118
            return text;
×
119

NEW
120
        var words = WordsExpression().Matches(text);
×
121

NEW
122
        var builder = new StringBuilder();
×
NEW
123
        foreach (Match word in words)
×
124
        {
NEW
125
            if (builder.Length > 0)
×
NEW
126
                builder.Append(' ');
×
127

NEW
128
            builder.Append(word.Value);
×
129
        }
130

NEW
131
        return builder.ToString();
×
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc