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

loresoft / MediatR.CommandQuery / 13575541599

27 Feb 2025 08:45PM UTC coverage: 58.86% (-1.2%) from 60.086%
13575541599

push

github

pwelter34
add UrlBuilder

480 of 955 branches covered (50.26%)

Branch coverage included in aggregate %.

160 of 282 new or added lines in 3 files covered. (56.74%)

1440 of 2307 relevant lines covered (62.42%)

22.25 hits per line

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

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

5
using MediatR.CommandQuery.Services;
6

7
namespace MediatR.CommandQuery.Extensions;
8

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

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

32
        if (text!.Length <= keep)
×
33
            return text;
×
34

35
        ellipsis ??= string.Empty;
×
36

37
        if (text.Length <= keep + ellipsis.Length || keep < ellipsis.Length)
×
38
            return text[..keep];
×
39

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

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

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

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

72
        return true;
×
73
    }
74

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

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

101
        if (string.IsNullOrEmpty(second))
8!
102
            return first;
×
103

104
        var firstEndsWith = first[^1] == separator;
8✔
105
        var secondStartsWith = second[0] == separator;
8✔
106

107
        if (firstEndsWith && !secondStartsWith)
8✔
108
            return string.Concat(first, second);
2✔
109

110
        if (!firstEndsWith && secondStartsWith)
6✔
111
            return string.Concat(first, second);
2✔
112

113
        if (firstEndsWith && secondStartsWith)
4✔
114
            return string.Concat(first, second[1..]);
2✔
115

116
        return $"{first}{separator}{second}";
2✔
117
    }
118

119
    /// <summary>
120
    /// Converts a NameIdentifier and spaces it out into words "Name Identifier".
121
    /// </summary>
122
    /// <param name="text">The text value to convert.</param>
123
    /// <returns>The text converted</returns>
124
    [return: NotNullIfNotNull(nameof(text))]
125
    public static string? ToTitle(this string? text)
126
    {
127
        if (text.IsNullOrEmpty() || text.Length < 2)
×
128
            return text;
×
129

130
        var words = WordsExpression().Matches(text);
×
131

NEW
132
        var builder = StringBuilderCache.Acquire(text.Length + 10);
×
133
        foreach (Match word in words)
×
134
        {
135
            if (builder.Length > 0)
×
136
                builder.Append(' ');
×
137

138
            builder.Append(word.Value);
×
139
        }
140

NEW
141
        return StringBuilderCache.ToString(builder);
×
142
    }
143
}
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