Skip to content

Commit

Permalink
feat(utilities): add extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
matkoch committed Mar 19, 2024
1 parent f76db12 commit 0719711
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
25 changes: 25 additions & 0 deletions source/Nuke.Utilities/Collections/Enumerable.ToEmptyIfNull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System.Collections.Generic;

namespace Nuke.Common.Utilities.Collections;

public static partial class EnumerableExtensions
{
public static IEnumerable<T> ToEmptyIfNull<T>(this IEnumerable<T> enumerable)
{
return enumerable ?? [];
}

public static T[] ToEmptyIfNull<T>(this T[] array)
{
return array ?? [];
}

public static IList<T> ToEmptyIfNull<T>(this IList<T> list)
{
return list ?? [];
}
}
22 changes: 21 additions & 1 deletion source/Nuke.Utilities/Text/String.Emptiness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@ public static bool IsNullOrEmpty(this string str)
}

/// <summary>
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
/// Indicates whether a specified string is null, empty, or only white-space.
/// </summary>
[Pure]
[ContractAnnotation("null => halt")]
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}

/// <summary>
/// Returns <value>null</value> if the specified string is empty.
/// </summary>
[Pure]
[ContractAnnotation("null => null")]
public static string ToNullIfEmpty(this string str)
{
return str.IsNullOrEmpty() ? null : str;
}

/// <summary>
/// Returns <value>null</value> if the specified string is empty or only white-space.
/// </summary>
[Pure]
[ContractAnnotation("null => null")]
public static string ToNullIfWhiteSpace(this string str)
{
return str.IsNullOrWhiteSpace() ? null : str;
}
}
13 changes: 11 additions & 2 deletions source/Nuke.Utilities/Text/String.Split.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ public static IEnumerable<string> SplitCamelHumps(this string str, params string
/// Splits a given string by new-lines with empty entries preserved.
/// </summary>
[Pure]
public static string[] SplitLineBreaks(this string str)
public static string[] SplitLineBreaks(this string str, StringSplitOptions options = StringSplitOptions.None)
{
return str.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
return str.Split(new[] { "\r\n", "\n" }, options);
}

/// <summary>
/// Splits a given string by paragraphs (double new-line) with empty entries preserved.
/// </summary>
[Pure]
public static string[] SplitParagraphs(this string str, StringSplitOptions options = StringSplitOptions.None)
{
return str.Split(new[] { "\r\n\r\n", "\n\n" }, options);
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions source/Nuke.Utilities/Text/String.StartsEndsContains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public static bool ContainsOrdinalIgnoreCase(this string str, string other)
return str.IndexOf(other, StringComparison.OrdinalIgnoreCase) >= 0;
}

/// <summary>
/// Indicates whether a collection of strings contains any other string under <see cref="StringComparison.OrdinalIgnoreCase"/> comparison.
/// </summary>
[Pure]
public static bool ContainsAnyOrdinalIgnoreCase(this IEnumerable<string> str, params string[] others)
{
return others.Any(x => str.Contains(x, StringComparer.OrdinalIgnoreCase));
}

/// <summary>
/// Indicates whether a string equals another string under <see cref="StringComparison.OrdinalIgnoreCase"/> comparison.
/// </summary>
Expand Down

0 comments on commit 0719711

Please sign in to comment.