Skip to content

Commit 786ee97

Browse files
committed
feat(utilities): add extensions
1 parent f76db12 commit 786ee97

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2024 Maintainers of NUKE.
2+
// Distributed under the MIT License.
3+
// https://github.com/nuke-build/nuke/blob/master/LICENSE
4+
5+
using System.Collections.Generic;
6+
7+
namespace Nuke.Common.Utilities.Collections;
8+
9+
public static partial class EnumerableExtensions
10+
{
11+
public static IEnumerable<T> ToEmptyIfNull<T>(this IEnumerable<T> enumerable)
12+
{
13+
return enumerable ?? [];
14+
}
15+
16+
public static T[] ToEmptyIfNull<T>(this T[] array)
17+
{
18+
return array ?? [];
19+
}
20+
21+
public static IList<T> ToEmptyIfNull<T>(this IList<T> list)
22+
{
23+
return list ?? [];
24+
}
25+
}

source/Nuke.Utilities/Text/String.Emptiness.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,32 @@ public static bool IsNullOrEmpty(this string str)
1919
}
2020

2121
/// <summary>
22-
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
22+
/// Indicates whether a specified string is null, empty, or only white-space.
2323
/// </summary>
2424
[Pure]
2525
[ContractAnnotation("null => halt")]
2626
public static bool IsNullOrWhiteSpace(this string str)
2727
{
2828
return string.IsNullOrWhiteSpace(str);
2929
}
30+
31+
/// <summary>
32+
/// Returns <value>null</value> if the specified string is empty.
33+
/// </summary>
34+
[Pure]
35+
[ContractAnnotation("null => null")]
36+
public static string ToNullIfEmpty(this string str)
37+
{
38+
return str.IsNullOrEmpty() ? null : str;
39+
}
40+
41+
/// <summary>
42+
/// Returns <value>null</value> if the specified string is empty or only white-space.
43+
/// </summary>
44+
[Pure]
45+
[ContractAnnotation("null => null")]
46+
public static string ToNullIfWhiteSpace(this string str)
47+
{
48+
return str.IsNullOrWhiteSpace() ? null : str;
49+
}
3050
}

source/Nuke.Utilities/Text/String.Split.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,18 @@ public static IEnumerable<string> SplitCamelHumps(this string str, params string
6464
/// Splits a given string by new-lines with empty entries preserved.
6565
/// </summary>
6666
[Pure]
67-
public static string[] SplitLineBreaks(this string str)
67+
public static string[] SplitLineBreaks(this string str, StringSplitOptions options = StringSplitOptions.None)
6868
{
69-
return str.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
69+
return str.Split(new[] { "\r\n", "\n" }, options);
70+
}
71+
72+
/// <summary>
73+
/// Splits a given string by paragraphs (double new-line) with empty entries preserved.
74+
/// </summary>
75+
[Pure]
76+
public static string[] SplitParagraphs(this string str, StringSplitOptions options = StringSplitOptions.None)
77+
{
78+
return str.Split(new[] { "\r\n\r\n", "\n\n" }, options);
7079
}
7180

7281
/// <summary>

source/Nuke.Utilities/Text/String.StartsEndsContains.cs

+9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public static bool ContainsOrdinalIgnoreCase(this string str, string other)
2020
return str.IndexOf(other, StringComparison.OrdinalIgnoreCase) >= 0;
2121
}
2222

23+
/// <summary>
24+
/// Indicates whether a collection of strings contains any other string under <see cref="StringComparison.OrdinalIgnoreCase"/> comparison.
25+
/// </summary>
26+
[Pure]
27+
public static bool ContainsOrdinalIgnoreCase(IEnumerable<string> str, params string[] others)
28+
{
29+
return others.Any(x => str.Contains(x, StringComparer.OrdinalIgnoreCase));
30+
}
31+
2332
/// <summary>
2433
/// Indicates whether a string equals another string under <see cref="StringComparison.OrdinalIgnoreCase"/> comparison.
2534
/// </summary>

0 commit comments

Comments
 (0)