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

loresoft / EntityFrameworkCore.Generator / 15072048022

16 May 2025 03:31PM UTC coverage: 55.392% (-1.4%) from 56.772%
15072048022

push

github

pwelter34
enable nullable support

616 of 1271 branches covered (48.47%)

Branch coverage included in aggregate %.

233 of 397 new or added lines in 61 files covered. (58.69%)

17 existing lines in 11 files now uncovered.

1824 of 3134 relevant lines covered (58.2%)

88.56 hits per line

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

32.54
/src/EntityFrameworkCore.Generator.Core/Extensions/StringExtensions.cs
1
using System;
2
using System.Diagnostics.CodeAnalysis;
3
using System.Linq;
4
using System.Runtime.CompilerServices;
5
using System.Text;
6
using System.Text.RegularExpressions;
7

8
namespace EntityFrameworkCore.Generator.Extensions;
9

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
    private static readonly Regex _splitNameRegex = new Regex(@"[\W_]+");
1✔
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
    {
NEW
29
        if (string.IsNullOrEmpty(text))
×
NEW
30
            return text;
×
31

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

NEW
35
        ellipsis ??= string.Empty;
×
36

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

NEW
40
        int prefix = keep - ellipsis.Length;
×
NEW
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
    ///     <see langword="true"/> if is null or empty; otherwise, <see langword="false"/>.
50
    /// </returns>
51
    public static bool IsNullOrEmpty([NotNullWhen(false)] this string? item)
52
    {
53
        return string.IsNullOrEmpty(item);
1,036✔
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
    ///      <see langword="true"/> if is null or empty; otherwise, <see langword="false"/>.
62
    /// </returns>
63
    public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? item)
64
    {
65
        if (item == null)
2,702!
66
            return true;
×
67

68
        for (int i = 0; i < item.Length; i++)
5,404✔
69
        {
70
            if (!char.IsWhiteSpace(item[i]))
2,701!
71
                return false;
2,701✔
72
        }
73

74
        return true;
1✔
75
    }
76

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

89
    /// <summary>
90
    /// Does string contain both uppercase and lowercase characters?
91
    /// </summary>
92
    /// <param name="s">The value.</param>
93
    /// <returns>True if contain mixed case.</returns>
94
    public static bool IsMixedCase(this string s)
95
    {
96
        if (s.IsNullOrEmpty())
567!
97
            return false;
×
98

99
        var containsUpper = s.Any(Char.IsUpper);
567✔
100
        var containsLower = s.Any(Char.IsLower);
567✔
101

102
        return containsLower && containsUpper;
567✔
103
    }
104

105
    /// <summary>
106
    /// Converts a string to use camelCase.
107
    /// </summary>
108
    /// <param name="value">The value.</param>
109
    /// <returns>The to camel case. </returns>
110
    public static string ToCamelCase(this string value)
111
    {
112
        if (string.IsNullOrEmpty(value))
×
113
            return value;
×
114

115
        string output = ToPascalCase(value);
×
116
        if (output.Length > 2)
×
NEW
117
            return char.ToLower(output[0]) + output[1..];
×
118

119
        return output.ToLower();
×
120
    }
121

122
    /// <summary>
123
    /// Converts a string to use PascalCase.
124
    /// </summary>
125
    /// <param name="value">Text to convert</param>
126
    /// <returns>The string</returns>
127
    public static string ToPascalCase(this string value)
128
    {
129
        return value.ToPascalCase(_splitNameRegex);
567✔
130
    }
131

132
    /// <summary>
133
    /// Converts a string to use PascalCase.
134
    /// </summary>
135
    /// <param name="value">Text to convert</param>
136
    /// <param name="splitRegex">Regular Expression to split words on.</param>
137
    /// <returns>The string</returns>
138
    public static string ToPascalCase(this string value, Regex splitRegex)
139
    {
140
        if (string.IsNullOrEmpty(value))
567!
141
            return value;
×
142

143
        var mixedCase = value.IsMixedCase();
567✔
144
        var names = splitRegex.Split(value);
567✔
145
        var output = new StringBuilder();
567✔
146

147
        if (names.Length > 1)
567✔
148
        {
149
            foreach (string name in names)
18✔
150
            {
151
                if (name.Length > 1)
6!
152
                {
153
                    output.Append(char.ToUpper(name[0]));
6✔
154
                    output.Append(mixedCase ? name[1..] : name[1..].ToLower());
6!
155
                }
156
                else
157
                {
158
                    output.Append(name.ToUpper());
×
159
                }
160
            }
161
        }
162
        else if (value.Length > 1)
564!
163
        {
164
            output.Append(char.ToUpper(value[0]));
564✔
165
            output.Append(mixedCase ? value[1..] : value[1..].ToLower());
564✔
166
        }
167
        else
168
        {
169
            output.Append(value.ToUpper());
×
170
        }
171

172
        return output.ToString();
567✔
173
    }
174

175
    /// <summary>
176
    /// Combines two strings with the specified separator.
177
    /// </summary>
178
    /// <param name="first">The first string.</param>
179
    /// <param name="second">The second string.</param>
180
    /// <param name="separator">The separator string.</param>
181
    /// <returns>A string combining the <paramref name="first"/> and <paramref name="second"/> parameters with the <paramref name="separator"/> between them</returns>
182
    [return: NotNullIfNotNull(nameof(first))]
183
    [return: NotNullIfNotNull(nameof(second))]
184
    public static string? Combine(this string? first, string? second, char separator = '/')
185
    {
NEW
186
        if (string.IsNullOrEmpty(first))
×
NEW
187
            return second;
×
188

NEW
189
        if (string.IsNullOrEmpty(second))
×
NEW
190
            return first;
×
191

NEW
192
        var firstEndsWith = first[^1] == separator;
×
NEW
193
        var secondStartsWith = second[0] == separator;
×
194

NEW
195
        if (firstEndsWith && !secondStartsWith)
×
NEW
196
            return string.Concat(first, second);
×
197

NEW
198
        if (!firstEndsWith && secondStartsWith)
×
NEW
199
            return string.Concat(first, second);
×
200

NEW
201
        if (firstEndsWith && secondStartsWith)
×
NEW
202
            return string.Concat(first, second[1..]);
×
203

NEW
204
        return $"{first}{separator}{second}";
×
205
    }
206

207
    /// <summary>
208
    /// Converts a NameIdentifier and spaces it out into words "Name Identifier".
209
    /// </summary>
210
    /// <param name="text">The text value to convert.</param>
211
    /// <returns>The text converted</returns>
212
    [return: NotNullIfNotNull(nameof(text))]
213
    public static string? ToTitle(this string? text)
214
    {
NEW
215
        if (text.IsNullOrEmpty() || text.Length < 2)
×
NEW
216
            return text;
×
217

NEW
218
        var words = WordsExpression().Matches(text);
×
219

NEW
220
        var wrote = false;
×
NEW
221
        var builder = new DefaultInterpolatedStringHandler(literalLength: text.Length + 5, formattedCount: 1);
×
NEW
222
        foreach (Match word in words)
×
223
        {
NEW
224
            if (wrote)
×
NEW
225
                builder.AppendLiteral(" ");
×
226

NEW
227
            builder.AppendLiteral(word.Value);
×
NEW
228
            wrote = true;
×
229
        }
230

NEW
231
        return builder.ToStringAndClear();
×
232
    }
233
}
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

© 2025 Coveralls, Inc