From fcb40d5838735ca3e5ea762b94dc1913508af4c2 Mon Sep 17 00:00:00 2001 From: David Kallesen Date: Tue, 30 Jul 2024 08:02:14 +0200 Subject: [PATCH] feat: Add more extensions methods to bool and bool? --- docs/CodeDoc/Atc/IndexExtended.md | 8 + docs/CodeDoc/Atc/System.md | 100 ++++++++++++- .../Extensions/BaseTypes/BooleanExtensions.cs | 139 ++++++++++++++---- .../BaseTypes/BooleanExtensionsTests.cs | 102 +++++++++++++ 4 files changed, 315 insertions(+), 34 deletions(-) diff --git a/docs/CodeDoc/Atc/IndexExtended.md b/docs/CodeDoc/Atc/IndexExtended.md index c5e58d26..6f9151b7 100644 --- a/docs/CodeDoc/Atc/IndexExtended.md +++ b/docs/CodeDoc/Atc/IndexExtended.md @@ -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) diff --git a/docs/CodeDoc/Atc/System.md b/docs/CodeDoc/Atc/System.md index eb31f87f..1d692119 100644 --- a/docs/CodeDoc/Atc/System.md +++ b/docs/CodeDoc/Atc/System.md @@ -199,33 +199,117 @@ Extensions for the `System.Boolean` class. ### Static Methods +#### HasNoValue +>```csharp +>bool HasNoValue(this bool? source) +>``` +>Summary: Determines if the nullable boolean does not have a value. +> +>Parameters:
+>     `source`  -  The nullable boolean source.
+> +>Returns: `true` if the source does not have a value; otherwise, `false`. +#### HasNoValueOrFalse +>```csharp +>bool HasNoValueOrFalse(this bool? source) +>``` +>Summary: Determines if the nullable boolean does not have a value or is false. +> +>Parameters:
+>     `source`  -  The nullable boolean source.
+> +>Returns: `true` if the source does not have a value or is false; otherwise, `false`. +#### HasNoValueOrTrue +>```csharp +>bool HasNoValueOrTrue(this bool? source) +>``` +>Summary: Determines if the nullable boolean does not have a value or is true. +> +>Parameters:
+>     `source`  -  The nullable boolean source.
+> +>Returns: `true` if the source does not have a value or is true; otherwise, `false`. +#### HasValueAndFalse +>```csharp +>bool HasValueAndFalse(this bool? source) +>``` +>Summary: Determines if the nullable boolean has a value and it is false. +> +>Parameters:
+>     `source`  -  The nullable boolean source.
+> +>Returns: `true` if the source has a value and it is false; otherwise, `false`. +#### HasValueAndTrue +>```csharp +>bool HasValueAndTrue(this bool? source) +>``` +>Summary: Determines if the nullable boolean has a value and it is true. +> +>Parameters:
+>     `source`  -  The nullable boolean source.
+> +>Returns: `true` if the source has a value and it is true; otherwise, `false`. #### IsEqual >```csharp >bool IsEqual(this bool? a, bool? b) >``` ->Summary: Determines whether the specified a is equal. +>Summary: Determines whether the specified nullable booleans are equal. > >Parameters:
->     `a`  -  a.
->     `b`  -  The b.
+>     `a`  -  The first nullable boolean.
+>     `b`  -  The second nullable boolean.
> ->Returns: `true` if the specified a is equal; otherwise, `false`. +>Returns: `true` if both nullable booleans are equal; otherwise, `false`. #### ToInt >```csharp >int ToInt(this bool source) >``` ->Summary: Converts the string representation of a number to an integer. +>Summary: Converts the boolean to an integer. > >Parameters:
->     `source`  -  if set to true [source].
+>     `source`  -  The boolean source.
+> +>Returns: 1 if the source is true; otherwise, 0. #### ToInt >```csharp >int ToInt(this bool? source) >``` ->Summary: Converts the string representation of a number to an integer. +>Summary: Converts the boolean to an integer. +> +>Parameters:
+>     `source`  -  The boolean source.
+> +>Returns: 1 if the source is true; otherwise, 0. +#### ToYesNoString +>```csharp +>string ToYesNoString(this bool source) +>``` +>Summary: Converts the boolean to a "Yes" or "No" string. > >Parameters:
->     `source`  -  if set to true [source].
+>     `source`  -  The boolean source.
+> +>Returns: "Yes" if the source is true; otherwise, "No". +#### ToYesNoType +>```csharp +>YesNoType ToYesNoType(this bool source) +>``` +>Summary: Converts the boolean to a `Atc.YesNoType`. +> +>Parameters:
+>     `source`  -  The boolean source.
+> +>Returns: `Atc.YesNoType.Yes` if the source is true; otherwise, `Atc.YesNoType.No`. +#### ToYesNoType +>```csharp +>YesNoType ToYesNoType(this bool? source) +>``` +>Summary: Converts the boolean to a `Atc.YesNoType`. +> +>Parameters:
+>     `source`  -  The boolean source.
+> +>Returns: `Atc.YesNoType.Yes` if the source is true; otherwise, `Atc.YesNoType.No`.
diff --git a/src/Atc/Extensions/BaseTypes/BooleanExtensions.cs b/src/Atc/Extensions/BaseTypes/BooleanExtensions.cs index 5d9d56f6..511106c8 100644 --- a/src/Atc/Extensions/BaseTypes/BooleanExtensions.cs +++ b/src/Atc/Extensions/BaseTypes/BooleanExtensions.cs @@ -1,44 +1,131 @@ // ReSharper disable once CheckNamespace +// ReSharper disable GrammarMistakeInComment namespace System; /// /// Extensions for the class. /// +[SuppressMessage("Major Code Smell", "S3358:Ternary operators should not be nested", Justification = "OK.")] public static class BooleanExtensions { /// - /// Determines whether the specified a is equal. + /// Determines if the nullable boolean has a value and it is true. /// - /// a. - /// The b. - /// - /// true if the specified a is equal; otherwise, false. - /// - public static bool IsEqual(this bool? a, bool? b) - { - if (a is null || b is null) - { - return a is null && b is null; - } + /// The nullable boolean source. + /// true if the source has a value and it is true; otherwise, false. + public static bool HasValueAndTrue( + this bool? source) + => source.HasValue && + source.Value; + + /// + /// Determines if the nullable boolean has a value and it is false. + /// + /// The nullable boolean source. + /// true if the source has a value and it is false; otherwise, false. + public static bool HasValueAndFalse( + this bool? source) + => source.HasValue && + !source.Value; + + /// + /// Determines if the nullable boolean does not have a value. + /// + /// The nullable boolean source. + /// true if the source does not have a value; otherwise, false. + public static bool HasNoValue( + this bool? source) + => !source.HasValue; - return a == b; - } + /// + /// Determines if the nullable boolean does not have a value or is true. + /// + /// The nullable boolean source. + /// true if the source does not have a value or is true; otherwise, false. + public static bool HasNoValueOrTrue( + this bool? source) + => !source.HasValue || + source.Value; /// - /// Converts the string representation of a number to an integer. + /// Determines if the nullable boolean does not have a value or is false. /// - /// if set to true [source]. - public static int ToInt(this bool source) - { - return source ? 1 : 0; - } + /// The nullable boolean source. + /// true if the source does not have a value or is false; otherwise, false. + public static bool HasNoValueOrFalse( + this bool? source) + => !source.HasValue || + !source.Value; /// - /// Converts the string representation of a number to an integer. + /// Determines whether the specified nullable booleans are equal. /// - /// The source. - public static int ToInt(this bool? source) - { - return source?.ToInt() ?? 0; - } + /// The first nullable boolean. + /// The second nullable boolean. + /// true if both nullable booleans are equal; otherwise, false. + public static bool IsEqual( + this bool? a, + bool? b) + => a is null || b is null + ? a is null && b is null + : a == b; + + /// + /// Converts the boolean to an integer. + /// + /// The boolean source. + /// 1 if the source is true; otherwise, 0. + public static int ToInt( + this bool source) + => source + ? 1 + : 0; + + /// + /// Converts the nullable boolean to an integer. + /// + /// The nullable boolean source. + /// 1 if the source is true; otherwise, 0. + public static int ToInt( + this bool? source) + => source?.ToInt() ?? 0; + + /// + /// Converts the boolean to a "Yes" or "No" string. + /// + /// The boolean source. + /// "Yes" if the source is true; otherwise, "No". + public static string ToYesNoString( + this bool source) + => source + ? nameof(YesNoType.Yes) + : nameof(YesNoType.No); + + /// + /// Converts the boolean to a . + /// + /// The boolean source. + /// if the source is true; otherwise, . + public static YesNoType ToYesNoType( + this bool source) + => source + ? YesNoType.Yes + : YesNoType.No; + + /// + /// Converts the nullable boolean to a . + /// + /// The nullable boolean source. + /// + /// if the source is true; + /// if the source is false; + /// if the source is null. + /// + public static YesNoType ToYesNoType( + this bool? source) + => source is null + ? YesNoType.None + : source.Value + ? YesNoType.Yes + : YesNoType.No; } \ No newline at end of file diff --git a/test/Atc.Tests/Extensions/BaseTypes/BooleanExtensionsTests.cs b/test/Atc.Tests/Extensions/BaseTypes/BooleanExtensionsTests.cs index 7520be4f..91e68d3b 100644 --- a/test/Atc.Tests/Extensions/BaseTypes/BooleanExtensionsTests.cs +++ b/test/Atc.Tests/Extensions/BaseTypes/BooleanExtensionsTests.cs @@ -2,6 +2,71 @@ namespace Atc.Tests.Extensions.BaseTypes; public class BooleanExtensionsTests { + [Theory] + [InlineData(true, true)] + [InlineData(false, false)] + [InlineData(false, null)] + public void HasValueAndTrue(bool expected, bool? source) + { + // Act + var actual = source.HasValueAndTrue(); + + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(false, null)] + public void HasValueAndFalse(bool expected, bool? source) + { + // Act + var actual = source.HasValueAndFalse(); + + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(false, false)] + [InlineData(false, true)] + [InlineData(true, null)] + public void HasNoValue(bool expected, bool? source) + { + // Act + var actual = source.HasNoValue(); + + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(false, false)] + [InlineData(true, true)] + [InlineData(true, null)] + public void HasNoValueOrTrue(bool expected, bool? source) + { + // Act + var actual = source.HasNoValueOrTrue(); + + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(true, false)] + [InlineData(false, true)] + [InlineData(true, null)] + public void HasNoValueOrFalse(bool expected, bool? source) + { + // Act + var actual = source.HasNoValueOrFalse(); + + // Assert + Assert.Equal(expected, actual); + } + [Theory] [InlineData(true, true, true)] [InlineData(false, true, false)] @@ -40,4 +105,41 @@ public void ToInt_Nullable(int expected, bool? input) // Assert Assert.Equal(expected, actual); } + + [Theory] + [InlineData("Yes", true)] + [InlineData("No", false)] + public void ToYesNoString(string expected, bool source) + { + // Act + var actual = source.ToYesNoString(); + + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(YesNoType.Yes, true)] + [InlineData(YesNoType.No, false)] + public void ToYesNoType(YesNoType expected, bool source) + { + // Act + var actual = source.ToYesNoType(); + + // Assert + Assert.Equal(expected, actual); + } + + [Theory] + [InlineData(YesNoType.Yes, true)] + [InlineData(YesNoType.No, false)] + [InlineData(YesNoType.None, null)] + public void ToYesNoTypeForNullable(YesNoType expected, bool? source) + { + // Act + var actual = source.ToYesNoType(); + + // Assert + Assert.Equal(expected, actual); + } } \ No newline at end of file