-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add more extensions methods to bool and bool?
- Loading branch information
1 parent
6cf33cf
commit fcb40d5
Showing
4 changed files
with
315 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,131 @@ | ||
// ReSharper disable once CheckNamespace | ||
// ReSharper disable GrammarMistakeInComment | ||
namespace System; | ||
|
||
/// <summary> | ||
/// Extensions for the <see cref="bool"/> class. | ||
/// </summary> | ||
[SuppressMessage("Major Code Smell", "S3358:Ternary operators should not be nested", Justification = "OK.")] | ||
public static class BooleanExtensions | ||
{ | ||
/// <summary> | ||
/// Determines whether the specified a is equal. | ||
/// Determines if the nullable boolean has a value and it is true. | ||
/// </summary> | ||
/// <param name="a">a.</param> | ||
/// <param name="b">The b.</param> | ||
/// <returns> | ||
/// <c>true</c> if the specified a is equal; otherwise, <c>false</c>. | ||
/// </returns> | ||
public static bool IsEqual(this bool? a, bool? b) | ||
{ | ||
if (a is null || b is null) | ||
{ | ||
return a is null && b is null; | ||
} | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns><c>true</c> if the source has a value and it is true; otherwise, <c>false</c>.</returns> | ||
public static bool HasValueAndTrue( | ||
this bool? source) | ||
=> source.HasValue && | ||
source.Value; | ||
|
||
/// <summary> | ||
/// Determines if the nullable boolean has a value and it is false. | ||
/// </summary> | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns><c>true</c> if the source has a value and it is false; otherwise, <c>false</c>.</returns> | ||
public static bool HasValueAndFalse( | ||
this bool? source) | ||
=> source.HasValue && | ||
!source.Value; | ||
|
||
/// <summary> | ||
/// Determines if the nullable boolean does not have a value. | ||
/// </summary> | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns><c>true</c> if the source does not have a value; otherwise, <c>false</c>.</returns> | ||
public static bool HasNoValue( | ||
this bool? source) | ||
=> !source.HasValue; | ||
|
||
return a == b; | ||
} | ||
/// <summary> | ||
/// Determines if the nullable boolean does not have a value or is true. | ||
/// </summary> | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns><c>true</c> if the source does not have a value or is true; otherwise, <c>false</c>.</returns> | ||
public static bool HasNoValueOrTrue( | ||
this bool? source) | ||
=> !source.HasValue || | ||
source.Value; | ||
|
||
/// <summary> | ||
/// Converts the string representation of a number to an integer. | ||
/// Determines if the nullable boolean does not have a value or is false. | ||
/// </summary> | ||
/// <param name="source">if set to <c>true</c> [source].</param> | ||
public static int ToInt(this bool source) | ||
{ | ||
return source ? 1 : 0; | ||
} | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns><c>true</c> if the source does not have a value or is false; otherwise, <c>false</c>.</returns> | ||
public static bool HasNoValueOrFalse( | ||
this bool? source) | ||
=> !source.HasValue || | ||
!source.Value; | ||
|
||
/// <summary> | ||
/// Converts the string representation of a number to an integer. | ||
/// Determines whether the specified nullable booleans are equal. | ||
/// </summary> | ||
/// <param name="source">The source.</param> | ||
public static int ToInt(this bool? source) | ||
{ | ||
return source?.ToInt() ?? 0; | ||
} | ||
/// <param name="a">The first nullable boolean.</param> | ||
/// <param name="b">The second nullable boolean.</param> | ||
/// <returns><c>true</c> if both nullable booleans are equal; otherwise, <c>false</c>.</returns> | ||
public static bool IsEqual( | ||
this bool? a, | ||
bool? b) | ||
=> a is null || b is null | ||
? a is null && b is null | ||
: a == b; | ||
|
||
/// <summary> | ||
/// Converts the boolean to an integer. | ||
/// </summary> | ||
/// <param name="source">The boolean source.</param> | ||
/// <returns>1 if the source is true; otherwise, 0.</returns> | ||
public static int ToInt( | ||
this bool source) | ||
=> source | ||
? 1 | ||
: 0; | ||
|
||
/// <summary> | ||
/// Converts the nullable boolean to an integer. | ||
/// </summary> | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns>1 if the source is true; otherwise, 0.</returns> | ||
public static int ToInt( | ||
this bool? source) | ||
=> source?.ToInt() ?? 0; | ||
|
||
/// <summary> | ||
/// Converts the boolean to a "Yes" or "No" string. | ||
/// </summary> | ||
/// <param name="source">The boolean source.</param> | ||
/// <returns>"Yes" if the source is true; otherwise, "No".</returns> | ||
public static string ToYesNoString( | ||
this bool source) | ||
=> source | ||
? nameof(YesNoType.Yes) | ||
: nameof(YesNoType.No); | ||
|
||
/// <summary> | ||
/// Converts the boolean to a <see cref="YesNoType"/>. | ||
/// </summary> | ||
/// <param name="source">The boolean source.</param> | ||
/// <returns><see cref="YesNoType.Yes"/> if the source is true; otherwise, <see cref="YesNoType.No"/>.</returns> | ||
public static YesNoType ToYesNoType( | ||
this bool source) | ||
=> source | ||
? YesNoType.Yes | ||
: YesNoType.No; | ||
|
||
/// <summary> | ||
/// Converts the nullable boolean to a <see cref="YesNoType"/>. | ||
/// </summary> | ||
/// <param name="source">The nullable boolean source.</param> | ||
/// <returns> | ||
/// <see cref="YesNoType.Yes"/> if the source is true; | ||
/// <see cref="YesNoType.No"/> if the source is false; | ||
/// <see cref="YesNoType.None"/> if the source is null. | ||
/// </returns> | ||
public static YesNoType ToYesNoType( | ||
this bool? source) | ||
=> source is null | ||
? YesNoType.None | ||
: source.Value | ||
? YesNoType.Yes | ||
: YesNoType.No; | ||
} |
Oops, something went wrong.