Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit da187fb

Browse files
committed
add extension methods
1 parent e321813 commit da187fb

File tree

4 files changed

+152
-2
lines changed

4 files changed

+152
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace MediatR.CommandQuery.Extensions;
2+
3+
/// <summary>
4+
/// Extension methods for <see cref="T:System.Collection.IEnumerable{T}"/>
5+
/// </summary>
6+
public static partial class EnumerableExtensions
7+
{
8+
/// <summary>
9+
/// Converts an IEnumerable of values to a delimited string.
10+
/// </summary>
11+
/// <typeparam name="T">The type of objects to delimit.</typeparam>
12+
/// <param name="values">The IEnumerable string values to convert.</param>
13+
/// <param name="delimiter">The delimiter.</param>
14+
/// <returns>A delimited string of the values.</returns>
15+
public static string ToDelimitedString<T>(this IEnumerable<T?> values, string? delimiter = ",")
16+
=> string.Join(delimiter ?? ",", values);
17+
18+
/// <summary>
19+
/// Converts an IEnumerable of values to a delimited string.
20+
/// </summary>
21+
/// <param name="values">The IEnumerable string values to convert.</param>
22+
/// <param name="delimiter">The delimiter.</param>
23+
/// <returns>A delimited string of the values.</returns>
24+
public static string ToDelimitedString(this IEnumerable<string?> values, string? delimiter = ",")
25+
=> string.Join(delimiter ?? ",", values);
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace MediatR.CommandQuery.Extensions;
2+
3+
/// <summary>
4+
/// <see cref="Random"/> type extension methods
5+
/// </summary>
6+
public static class RandomExtensions
7+
{
8+
private const string _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9+
10+
/// <summary>
11+
/// Generated random alphanumeric string with the specified length.
12+
/// </summary>
13+
/// <param name="random">The random instance to use.</param>
14+
/// <param name="length">The length of the alphanumeric string to generate.</param>
15+
/// <returns>A random alphanumeric string with the specified length.</returns>
16+
public static string Alphanumeric(this Random random, int length)
17+
{
18+
Span<char> result = stackalloc char[length];
19+
for (int i = 0; i < length; i++)
20+
result[i] = _chars[random.Next(_chars.Length)];
21+
22+
return new string(result);
23+
}
24+
}

src/MediatR.CommandQuery/Extensions/StringExtensions.cs

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,87 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using System.Text;
3+
using System.Text.RegularExpressions;
24

35
namespace 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
}

src/MediatR.CommandQuery/Extensions/TypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class TypeExtensions
1010
/// </summary>
1111
/// <typeparam name="TInterface">The type of the interface.</typeparam>
1212
/// <param name="type">The type to check.</param>
13-
/// <returns><c>true</c> if type implements the interface; otherwise <c>false</c></returns>
13+
/// <returns><see langword="true"/> if type implements the interface; otherwise <see langword="false"/></returns>
1414
/// <exception cref="InvalidOperationException">Only interfaces can be implemented.</exception>
1515
public static bool Implements<TInterface>(this Type type)
1616
where TInterface : class

0 commit comments

Comments
 (0)