Skip to content

Commit

Permalink
feat: Add more extensions methods to bool and bool?
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkallesen committed Jul 30, 2024
1 parent 6cf33cf commit fcb40d5
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 34 deletions.
8 changes: 8 additions & 0 deletions docs/CodeDoc/Atc/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -4967,9 +4967,17 @@
- ToList(this Array array, SortDirectionType sortDirectionType = None, bool removeDuplicates = False)
- [BooleanExtensions](System.md#booleanextensions)
- Static Methods
- HasNoValue(this bool? source)
- HasNoValueOrFalse(this bool? source)
- HasNoValueOrTrue(this bool? source)
- HasValueAndFalse(this bool? source)
- HasValueAndTrue(this bool? source)
- IsEqual(this bool? a, bool? b)
- ToInt(this bool source)
- ToInt(this bool? source)
- ToYesNoString(this bool source)
- ToYesNoType(this bool source)
- ToYesNoType(this bool? source)
- [ByteExtensions](System.md#byteextensions)
- Static Methods
- Split(this IEnumerable<byte> source, byte splitByte)
Expand Down
100 changes: 92 additions & 8 deletions docs/CodeDoc/Atc/System.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,33 +199,117 @@ Extensions for the `System.Boolean` class.
### Static Methods
#### HasNoValue
>```csharp
>bool HasNoValue(this bool? source)
>```
><b>Summary:</b> Determines if the nullable boolean does not have a value.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The nullable boolean source.<br />
>
><b>Returns:</b> `true` if the source does not have a value; otherwise, `false`.
#### HasNoValueOrFalse
>```csharp
>bool HasNoValueOrFalse(this bool? source)
>```
><b>Summary:</b> Determines if the nullable boolean does not have a value or is false.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The nullable boolean source.<br />
>
><b>Returns:</b> `true` if the source does not have a value or is false; otherwise, `false`.
#### HasNoValueOrTrue
>```csharp
>bool HasNoValueOrTrue(this bool? source)
>```
><b>Summary:</b> Determines if the nullable boolean does not have a value or is true.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The nullable boolean source.<br />
>
><b>Returns:</b> `true` if the source does not have a value or is true; otherwise, `false`.
#### HasValueAndFalse
>```csharp
>bool HasValueAndFalse(this bool? source)
>```
><b>Summary:</b> Determines if the nullable boolean has a value and it is false.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The nullable boolean source.<br />
>
><b>Returns:</b> `true` if the source has a value and it is false; otherwise, `false`.
#### HasValueAndTrue
>```csharp
>bool HasValueAndTrue(this bool? source)
>```
><b>Summary:</b> Determines if the nullable boolean has a value and it is true.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The nullable boolean source.<br />
>
><b>Returns:</b> `true` if the source has a value and it is true; otherwise, `false`.
#### IsEqual
>```csharp
>bool IsEqual(this bool? a, bool? b)
>```
><b>Summary:</b> Determines whether the specified a is equal.
><b>Summary:</b> Determines whether the specified nullable booleans are equal.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`a`&nbsp;&nbsp;-&nbsp;&nbsp;a.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`b`&nbsp;&nbsp;-&nbsp;&nbsp;The b.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`a`&nbsp;&nbsp;-&nbsp;&nbsp;The first nullable boolean.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`b`&nbsp;&nbsp;-&nbsp;&nbsp;The second nullable boolean.<br />
>
><b>Returns:</b> `true` if the specified a is equal; otherwise, `false`.
><b>Returns:</b> `true` if both nullable booleans are equal; otherwise, `false`.
#### ToInt
>```csharp
>int ToInt(this bool source)
>```
><b>Summary:</b> Converts the string representation of a number to an integer.
><b>Summary:</b> Converts the boolean to an integer.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;if set to true [source].<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The boolean source.<br />
>
><b>Returns:</b> 1 if the source is true; otherwise, 0.
#### ToInt
>```csharp
>int ToInt(this bool? source)
>```
><b>Summary:</b> Converts the string representation of a number to an integer.
><b>Summary:</b> Converts the boolean to an integer.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The boolean source.<br />
>
><b>Returns:</b> 1 if the source is true; otherwise, 0.
#### ToYesNoString
>```csharp
>string ToYesNoString(this bool source)
>```
><b>Summary:</b> Converts the boolean to a "Yes" or "No" string.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;if set to true [source].<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The boolean source.<br />
>
><b>Returns:</b> "Yes" if the source is true; otherwise, "No".
#### ToYesNoType
>```csharp
>YesNoType ToYesNoType(this bool source)
>```
><b>Summary:</b> Converts the boolean to a `Atc.YesNoType`.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The boolean source.<br />
>
><b>Returns:</b> `Atc.YesNoType.Yes` if the source is true; otherwise, `Atc.YesNoType.No`.
#### ToYesNoType
>```csharp
>YesNoType ToYesNoType(this bool? source)
>```
><b>Summary:</b> Converts the boolean to a `Atc.YesNoType`.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`source`&nbsp;&nbsp;-&nbsp;&nbsp;The boolean source.<br />
>
><b>Returns:</b> `Atc.YesNoType.Yes` if the source is true; otherwise, `Atc.YesNoType.No`.
<br />
Expand Down
139 changes: 113 additions & 26 deletions src/Atc/Extensions/BaseTypes/BooleanExtensions.cs
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;
}
Loading

0 comments on commit fcb40d5

Please sign in to comment.