From 23dee3d2b8b1190f054681442491f43f923a76bb Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 14 Apr 2023 14:17:33 +0100 Subject: [PATCH] feat: add value-passthru overloads for ConcatIf --- X10D.Tests/src/Text/StringTests.cs | 23 ++++- X10D/src/Text/StringExtensions.cs | 136 ++++++++++++++++++++++++++++- 2 files changed, 154 insertions(+), 5 deletions(-) diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs index d462186..6c94343 100644 --- a/X10D.Tests/src/Text/StringTests.cs +++ b/X10D.Tests/src/Text/StringTests.cs @@ -112,8 +112,10 @@ public class StringTests { Assert.That("Hello".ConcatIf(true, " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(true, () => " World"), Is.EqualTo("Hello World")); + Assert.That("Hello".ConcatIf(true, _ => " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(() => true, " World"), Is.EqualTo("Hello World")); Assert.That("Hello".ConcatIf(() => true, () => " World"), Is.EqualTo("Hello World")); + Assert.That("Hello".ConcatIf(() => true, _ => " World"), Is.EqualTo("Hello World")); }); } @@ -124,23 +126,36 @@ public class StringTests { Assert.That("Hello".ConcatIf(false, " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(false, () => " World"), Is.EqualTo("Hello")); + Assert.That("Hello".ConcatIf(false, _ => " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(() => false, " World"), Is.EqualTo("Hello")); Assert.That("Hello".ConcatIf(() => false, () => " World"), Is.EqualTo("Hello")); + Assert.That("Hello".ConcatIf(() => false, _ => " World"), Is.EqualTo("Hello")); + Assert.That("Hello".ConcatIf(_ => false, " World"), Is.EqualTo("Hello")); + Assert.That("Hello".ConcatIf(_ => false, () => " World"), Is.EqualTo("Hello")); + Assert.That("Hello".ConcatIf(_ => false, _ => " World"), Is.EqualTo("Hello")); }); } [Test] public void ConcatIf_ShouldThrowArgumentNullException_GivenNullConditionFactory() { - Assert.Throws(() => _ = "".ConcatIf(null!, "Hello World")); - Assert.Throws(() => _ = "".ConcatIf(null!, () => "Hello World")); + Assert.Throws(() => _ = "".ConcatIf((Func)null!, "Hello World")); + Assert.Throws(() => _ = "".ConcatIf((Func)null!, () => "Hello World")); + Assert.Throws(() => _ = "".ConcatIf((Func)null!, _ => "Hello World")); + Assert.Throws(() => _ = "".ConcatIf((Func)null!, "Hello World")); + Assert.Throws(() => _ = "".ConcatIf((Func)null!, () => "Hello World")); + Assert.Throws(() => _ = "".ConcatIf((Func)null!, _ => "Hello World")); } [Test] public void ConcatIf_ShouldThrowArgumentNullException_GivenNullValueFactory() { - Assert.Throws(() => _ = "".ConcatIf(true, (Func?)null!)); - Assert.Throws(() => _ = "".ConcatIf(() => true, (Func?)null!)); + Assert.Throws(() => _ = "".ConcatIf(true, (Func)null!)); + Assert.Throws(() => _ = "".ConcatIf(() => true, (Func)null!)); + Assert.Throws(() => _ = "".ConcatIf(_ => true, (Func)null!)); + Assert.Throws(() => _ = "".ConcatIf(true, (Func)null!)); + Assert.Throws(() => _ = "".ConcatIf(() => true, (Func)null!)); + Assert.Throws(() => _ = "".ConcatIf(_ => true, (Func)null!)); } [Test] diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs index 4600931..16183f9 100644 --- a/X10D/src/Text/StringExtensions.cs +++ b/X10D/src/Text/StringExtensions.cs @@ -157,6 +157,27 @@ public static class StringExtensions return conditionFactory() ? value + appendValue : value; } + /// + /// Appends a string to the current string if the specified condition evaluates to . + /// + /// The current string. + /// + /// The function that returns the condition to evaluate, with given as an argument. + /// + /// The string to append if the condition is true. + /// The concatenated string. + /// is . + [Pure] + public static string? ConcatIf(this string? value, Func conditionFactory, string? appendValue) + { + if (conditionFactory is null) + { + throw new ArgumentNullException(nameof(conditionFactory)); + } + + return conditionFactory(value) ? value + appendValue : value; + } + /// /// Appends a string to the current string if the specified condition evaluates to . /// @@ -164,6 +185,7 @@ public static class StringExtensions /// The condition to evaluate. /// The function that returns the string to append if the condition is true. /// The concatenated string. + /// is . [Pure] public static string? ConcatIf(this string? value, bool condition, Func valueFactory) { @@ -175,6 +197,28 @@ public static class StringExtensions return condition ? value + valueFactory() : value; } + /// + /// Appends a string to the current string if the specified condition evaluates to . + /// + /// The current string. + /// The condition to evaluate. + /// + /// The function that returns the string to append if the condition is true, with given as an + /// argument. + /// + /// The concatenated string. + /// is . + [Pure] + public static string? ConcatIf(this string? value, bool condition, Func valueFactory) + { + if (valueFactory is null) + { + throw new ArgumentNullException(nameof(valueFactory)); + } + + return condition ? value + valueFactory(value) : value; + } + /// /// Appends a string to the current string if the specified condition evaluates to . /// @@ -182,7 +226,9 @@ public static class StringExtensions /// The function that returns the condition to evaluate. /// The function that returns the string to append if the condition is true. /// The concatenated string. - /// is . + /// + /// or is . + /// [Pure] public static string? ConcatIf(this string? value, Func conditionFactory, Func valueFactory) { @@ -199,6 +245,94 @@ public static class StringExtensions return conditionFactory() ? value + valueFactory() : value; } + /// + /// Appends a string to the current string if the specified condition evaluates to . + /// + /// The current string. + /// The function that returns the condition to evaluate. + /// + /// The function that returns the string to append if the condition is true, with given as an + /// argument. + /// + /// The concatenated string. + /// + /// or is . + /// + [Pure] + public static string? ConcatIf(this string? value, Func conditionFactory, Func valueFactory) + { + if (conditionFactory is null) + { + throw new ArgumentNullException(nameof(conditionFactory)); + } + + if (valueFactory is null) + { + throw new ArgumentNullException(nameof(valueFactory)); + } + + return conditionFactory() ? value + valueFactory(value) : value; + } + + /// + /// Appends a string to the current string if the specified condition evaluates to . + /// + /// The current string. + /// + /// The function that returns the condition to evaluate, with given as an argument. + /// + /// The function that returns the string to append if the condition is true. + /// The concatenated string. + /// + /// or is . + /// + [Pure] + public static string? ConcatIf(this string? value, Func conditionFactory, Func valueFactory) + { + if (conditionFactory is null) + { + throw new ArgumentNullException(nameof(conditionFactory)); + } + + if (valueFactory is null) + { + throw new ArgumentNullException(nameof(valueFactory)); + } + + return conditionFactory(value) ? value + valueFactory() : value; + } + + /// + /// Appends a string to the current string if the specified condition evaluates to . + /// + /// The current string. + /// + /// The function that returns the condition to evaluate, with given as an argument. + /// + /// + /// The function that returns the string to append if the condition is true, with given as an + /// argument. + /// + /// The concatenated string. + /// + /// or is . + /// + [Pure] + public static string? ConcatIf(this string? value, Func conditionFactory, Func valueFactory) + { + if (conditionFactory is null) + { + throw new ArgumentNullException(nameof(conditionFactory)); + } + + if (valueFactory is null) + { + throw new ArgumentNullException(nameof(valueFactory)); + } + + return conditionFactory(value) ? value + valueFactory(value) : value; + } + /// /// Counts the occurrences of a character within the current character span. ///