From 6f3a667e378eb77b87abffb820e51a55f66d603f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 14 Apr 2023 13:55:52 +0100 Subject: [PATCH] feat: add string.ConcatIf --- CHANGELOG.md | 1 + X10D.Tests/src/Text/StringTests.cs | 38 +++++++++++++++ X10D/src/Text/StringExtensions.cs | 74 ++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9482e2e..61bb729 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - X10D: Added `TextWriter.WriteLineNoAlloc(uint[, ReadOnlySpan[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan[, IFormatProvider]])`. +- X10D: Added `string.ConcatIf`. - X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`. - X10D.Unity: Added `DebugUtility.DrawFunction`, and `DebugUtility.DrawUnjoinedPolyhedron` on which it relies. diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs index 99fca98..d462186 100644 --- a/X10D.Tests/src/Text/StringTests.cs +++ b/X10D.Tests/src/Text/StringTests.cs @@ -105,6 +105,44 @@ public class StringTests Assert.Throws(() => _ = "Hello World".ChangeEncoding(Encoding.UTF8, null!)); } + [Test] + public void ConcatIf_ShouldConcatenateString_GivenTrueCondition() + { + Assert.Multiple(() => + { + 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")); + }); + } + + [Test] + public void ConcatIf_ShouldNotConcatenateString_GivenFalseCondition() + { + Assert.Multiple(() => + { + 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")); + } + + [Test] + public void ConcatIf_ShouldThrowArgumentNullException_GivenNullValueFactory() + { + Assert.Throws(() => _ = "".ConcatIf(true, (Func?)null!)); + Assert.Throws(() => _ = "".ConcatIf(() => true, (Func?)null!)); + } + [Test] public void CountSubstring_ShouldHonor_StringComparison() { diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs index 44db6bd..4600931 100644 --- a/X10D/src/Text/StringExtensions.cs +++ b/X10D/src/Text/StringExtensions.cs @@ -125,6 +125,80 @@ public static class StringExtensions return value.GetBytes(sourceEncoding).ToString(destinationEncoding); } + /// + /// Appends a string to the current string if the specified condition evaluates to . + /// + /// The current string. + /// The condition to evaluate. + /// The string to append if the condition is true. + /// The concatenated string. + [Pure] + public static string? ConcatIf(this string? value, bool condition, string? appendValue) + { + return condition ? 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. + /// 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 + appendValue : 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. + /// The concatenated string. + [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; + } + + /// + /// 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. + /// The concatenated string. + /// 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; + } + /// /// Counts the occurrences of a character within the current character span. ///