-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from BenjaminAbt/feature/more-functions
add more functionality
- Loading branch information
Showing
16 changed files
with
317 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace StrongOf; | ||
|
||
/// <summary> | ||
/// Provides utility methods for strong types. | ||
/// </summary> | ||
public static class Strong | ||
{ | ||
/// <summary> | ||
/// Checks if the given strong type is null. | ||
/// </summary> | ||
/// <typeparam name="TStrong">The type of the strong object.</typeparam> | ||
/// <param name="strong">The strong object to check.</param> | ||
/// <returns>true if the strong object is null; otherwise, false.</returns> | ||
public static bool IsNull<TStrong>(TStrong? strong) | ||
where TStrong : IStrongOf | ||
{ | ||
return strong is null; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the given strong type is not null. | ||
/// </summary> | ||
/// <typeparam name="TStrong">The type of the strong object.</typeparam> | ||
/// <param name="strong">The strong object to check.</param> | ||
/// <returns>true if the strong object is not null; otherwise, false.</returns> | ||
public static bool IsNotNull<TStrong>(TStrong? strong) | ||
where TStrong : IStrongOf | ||
{ | ||
return strong is not null; | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the given strong string is null or empty. | ||
/// </summary> | ||
/// <typeparam name="TStrong">The type of the strong string.</typeparam> | ||
/// <param name="strong">The strong string to check.</param> | ||
/// <returns>true if the strong string is null or empty; otherwise, false.</returns> | ||
public static bool IsNullOrEmpty<TStrong>(TStrong? strong) | ||
where TStrong : StrongString<TStrong> | ||
{ | ||
return strong is null || strong.IsEmpty(); | ||
} | ||
|
||
/// <summary> | ||
/// Checks if the given strong string is not null or empty. | ||
/// </summary> | ||
/// <typeparam name="TStrong">The type of the strong string.</typeparam> | ||
/// <param name="strong">The strong string to check.</param> | ||
/// <returns>true if the strong string is not null or empty; otherwise, false.</returns> | ||
public static bool IsNotNullOrEmpty<TStrong>(TStrong? strong) | ||
where TStrong : StrongString<TStrong> | ||
{ | ||
return strong is not null && strong.IsEmpty() is false; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
namespace StrongOf; | ||
|
||
/// <summary> | ||
/// Represents a strong string of a specific type. | ||
/// </summary> | ||
/// <typeparam name="TStrong">The type of the strong string.</typeparam> | ||
public abstract partial class StrongString<TStrong> | ||
{ | ||
/// <summary> | ||
/// Trims all leading and trailing white-space characters from the current string. | ||
/// </summary> | ||
/// <returns>A new strong string equivalent to the current string but without leading and trailing white-space characters.</returns> | ||
public TStrong Trim() | ||
=> From(Value.Trim()); | ||
|
||
/// <summary> | ||
/// Trims all leading white-space characters from the current string. | ||
/// </summary> | ||
/// <returns>A new strong string equivalent to the current string but without leading white-space characters.</returns> | ||
public TStrong TrimStart() | ||
=> From(Value.TrimStart()); | ||
|
||
/// <summary> | ||
/// Trims all trailing white-space characters from the current string. | ||
/// </summary> | ||
/// <returns>A new strong string equivalent to the current string but without trailing white-space characters.</returns> | ||
public TStrong TrimEnd() | ||
=> From(Value.TrimEnd()); | ||
|
||
/// <summary> | ||
/// Determines whether this string and a specified string have the same value. | ||
/// </summary> | ||
/// <param name="value">The string to compare to this instance.</param> | ||
/// <param name="stringComparison">One of the enumeration values that specifies the rules for the comparison.</param> | ||
/// <returns>true if the value of the value parameter is the same as this string; otherwise, false.</returns> | ||
public bool Equals(string value, StringComparison stringComparison) | ||
=> Value.Equals(value, stringComparison); | ||
|
||
/// <summary> | ||
/// Determines whether this string and a specified StrongString object have the same value. | ||
/// </summary> | ||
/// <param name="other">The StrongString object to compare to this instance.</param> | ||
/// <param name="stringComparison">One of the enumeration values that specifies the rules for the comparison.</param> | ||
/// <returns>true if the value of the other parameter is the same as this string; otherwise, false.</returns> | ||
public bool Equals<T>(T other, StringComparison stringComparison) where T : TStrong | ||
=> Value.Equals(other.Value, stringComparison); | ||
|
||
/// <summary> | ||
/// Returns a copy of this string converted to lowercase. | ||
/// </summary> | ||
/// <returns>A string in lowercase.</returns> | ||
public TStrong ToLower() | ||
=> From(Value.ToLower()); | ||
|
||
/// <summary> | ||
/// Returns a copy of this string converted to lowercase, using the casing rules of the invariant culture. | ||
/// </summary> | ||
/// <returns>A string in lowercase.</returns> | ||
public TStrong ToLowerInvariant() | ||
=> From(Value.ToLowerInvariant()); | ||
|
||
/// <summary> | ||
/// Returns a copy of this string converted to uppercase. | ||
/// </summary> | ||
/// <returns>A string in uppercase.</returns> | ||
public TStrong ToUpper() | ||
=> From(Value.ToUpper()); | ||
|
||
/// <summary> | ||
/// Returns a copy of this string converted to uppercase, using the casing rules of the invariant culture. | ||
/// </summary> | ||
/// <returns>A string in uppercase.</returns> | ||
public TStrong ToUpperInvariant() | ||
=> From(Value.ToUpperInvariant()); | ||
|
||
/// <summary> | ||
/// Returns the first character of the current string. | ||
/// </summary> | ||
/// <returns>The first character of the current string.</returns> | ||
public char FirstChar() | ||
=> Value.First(); | ||
|
||
/// <summary> | ||
/// Returns the first character of the current string converted to uppercase, using the casing rules of the invariant culture. | ||
/// </summary> | ||
/// <returns>The first character of the current string in uppercase.</returns> | ||
public char FirstCharUpperInvariant() | ||
=> char.ToUpperInvariant(FirstChar()); | ||
} |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Xunit; | ||
|
||
namespace StrongOf.UnitTests; | ||
|
||
public class StrongStringMethodsTests | ||
{ | ||
private sealed class TestStringOf(string Value) : StrongString<TestStringOf>(Value) { } | ||
|
||
|
||
[Fact] | ||
public void Trim_ShouldRemoveLeadingAndTrailingWhiteSpace() | ||
{ | ||
TestStringOf strongString = new(" Test "); | ||
TestStringOf result = strongString.Trim(); | ||
Assert.Equal("Test", result.Value); | ||
} | ||
|
||
[Fact] | ||
public void TrimStart_ShouldRemoveLeadingWhiteSpace() | ||
{ | ||
TestStringOf strongString = new(" Test"); | ||
StrongString<TestStringOf> result = strongString.TrimStart(); | ||
Assert.Equal("Test", result.Value); | ||
} | ||
|
||
[Fact] | ||
public void TrimEnd_ShouldRemoveTrailingWhiteSpace() | ||
{ | ||
TestStringOf strongString = new("Test "); | ||
StrongString<TestStringOf> result = strongString.TrimEnd(); | ||
Assert.Equal("Test", result.Value); | ||
} | ||
|
||
[Fact] | ||
public void Equals_ShouldReturnTrueWhenValuesAreEqual() | ||
{ | ||
TestStringOf strongString = new("Test"); | ||
|
||
Assert.True(strongString.Equals("Test", StringComparison.Ordinal)); | ||
Assert.False(strongString.Equals("test", StringComparison.Ordinal)); | ||
} | ||
|
||
[Fact] | ||
public void ToLower_ShouldReturnLowerCaseString() | ||
{ | ||
TestStringOf strongString = new("TEST"); | ||
TestStringOf result = strongString.ToLower(); | ||
Assert.Equal("test", result.Value); | ||
} | ||
|
||
[Fact] | ||
public void ToUpper_ShouldReturnUpperCaseString() | ||
{ | ||
TestStringOf strongString = new("test"); | ||
TestStringOf result = strongString.ToUpper(); | ||
Assert.Equal("TEST", result.Value); | ||
} | ||
|
||
[Fact] | ||
public void FirstChar_ShouldReturnFirstCharacterOfString() | ||
{ | ||
TestStringOf strongString = new("Test"); | ||
char result = strongString.FirstChar(); | ||
Assert.Equal('T', result); | ||
} | ||
|
||
[Fact] | ||
public void FirstCharUpperInvariant_ShouldReturnFirstCharacterOfStringInUpperCase() | ||
{ | ||
TestStringOf strongString = new("test"); | ||
char result = strongString.FirstCharUpperInvariant(); | ||
Assert.Equal('T', result); | ||
} | ||
} |
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
Oops, something went wrong.