1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 03:25:41 +00:00

Allow null alternative

This commit is contained in:
Oliver Booth 2022-04-20 19:19:07 +01:00
parent d4e3c8ab50
commit 108390a415
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -19,7 +19,7 @@ public static class StringExtensions
[return: NotNullIfNotNull("value")]
public static string? AsNullIfEmpty(this string? value)
{
return string.IsNullOrEmpty(value) ? null : value;
return value.WithEmptyAlternative(null);
}
/// <summary>
@ -34,7 +34,7 @@ public static class StringExtensions
[return: NotNullIfNotNull("value")]
public static string? AsNullIfWhiteSpace(this string? value)
{
return string.IsNullOrWhiteSpace(value) ? null : value;
return value.WithWhiteSpaceAlternative(null);
}
/// <summary>
@ -492,14 +492,9 @@ public static class StringExtensions
/// <paramref name="alternative" /> if <paramref name="value" /> is <see langword="null" /> or empty; otherwise,
/// <paramref name="value" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="alternative" /> is <see langword="null" />.</exception>
public static string WithEmptyAlternative(this string? value, string alternative)
[return: NotNullIfNotNull("alternative")]
public static string? WithEmptyAlternative(this string? value, string? alternative)
{
if (alternative is null)
{
throw new ArgumentNullException(nameof(alternative));
}
return string.IsNullOrEmpty(value) ? alternative : value;
}
@ -513,14 +508,9 @@ public static class StringExtensions
/// <paramref name="alternative" /> if <paramref name="value" /> is <see langword="null" />, empty, or consists of only
/// whitespace; otherwise, <paramref name="value" />.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="alternative" /> is <see langword="null" />.</exception>
public static string WithWhiteSpaceAlternative(this string? value, string alternative)
[return: NotNullIfNotNull("alternative")]
public static string? WithWhiteSpaceAlternative(this string? value, string? alternative)
{
if (alternative is null)
{
throw new ArgumentNullException(nameof(alternative));
}
return string.IsNullOrWhiteSpace(value) ? alternative : value;
}
}