11using System . Diagnostics . CodeAnalysis ;
2+ using System . Text ;
3+ using System . Text . RegularExpressions ;
24
35namespace MediatR . CommandQuery . Extensions ;
46
57/// <summary>
68/// <see cref="String"/> type extension methods
79/// </summary>
8- public static class StringExtensions
10+ public static partial class StringExtensions
911{
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+ {
27+ if ( string . IsNullOrEmpty ( text ) )
28+ return text ;
29+
30+ if ( text ! . Length <= keep )
31+ return text ;
32+
33+ ellipsis ??= string . Empty ;
34+
35+ if ( text . Length <= keep + ellipsis . Length || keep < ellipsis . Length )
36+ return text [ ..keep ] ;
37+
38+ int prefix = keep - ellipsis . Length ;
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+ {
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+ {
63+ if ( item == null )
64+ return true ;
65+
66+ for ( int i = 0 ; i < item . Length ; i ++ )
67+ if ( ! char . IsWhiteSpace ( item [ i ] ) )
68+ return false ;
69+
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+ {
82+ return ! string . IsNullOrEmpty ( value ) ;
83+ }
84+
1085 /// <summary>
1186 /// Combines two strings with the specified separator.
1287 /// </summary>
@@ -30,4 +105,29 @@ public static class StringExtensions
30105 ? string . Concat ( first , second )
31106 : $ "{ first } { separator } { second } ";
32107 }
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+ {
117+ if ( text . IsNullOrEmpty ( ) || text . Length < 2 )
118+ return text ;
119+
120+ var words = WordsExpression ( ) . Matches ( text ) ;
121+
122+ var builder = new StringBuilder ( ) ;
123+ foreach ( Match word in words )
124+ {
125+ if ( builder . Length > 0 )
126+ builder . Append ( ' ' ) ;
127+
128+ builder . Append ( word . Value ) ;
129+ }
130+
131+ return builder . ToString ( ) ;
132+ }
33133}
0 commit comments