-
Notifications
You must be signed in to change notification settings - Fork 3
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 #304 from atc-net/feature/StringCaseFormatter
Add StringCaseFormatter
- Loading branch information
Showing
9 changed files
with
270 additions
and
0 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
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,92 @@ | ||
// ReSharper disable once CheckNamespace | ||
namespace Atc; | ||
|
||
/// <summary> | ||
/// Provides custom string formatting based on specified case formatting options.<br /><br /> | ||
/// Supported formats: | ||
/// <list type="table"> | ||
/// <item> | ||
/// <term>U</term> | ||
/// <description>Converts the entire string to uppercase.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>u</term> | ||
/// <description>Capitalizes the first character of the string.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>L</term> | ||
/// <description>Converts the entire string to lowercase.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>l</term> | ||
/// <description>Converts the first character of the string to lowercase.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>Ul</term> | ||
/// <description>Converts the string to uppercase with the first character in lowercase.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>Lu</term> | ||
/// <description>Converts the string to lowercase with the first character capitalized.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>[Format]:</term> | ||
/// <description>Applies the specified format and appends a colon ´<b>:</b>´ to the end.</description> | ||
/// </item> | ||
/// <item> | ||
/// <term>[Format].</term> | ||
/// <description>Applies the specified format and appends a period ´<b>.</b>´ to the end.</description> | ||
/// </item> | ||
/// </list> | ||
/// Examples: | ||
/// <code>string.Format(StringCaseFormatter.Default, "{0:U} {1:u} {2:L} {3:l}", .... 4 parameters );</code> | ||
/// <code>string.Format(StringCaseFormatter.Default, "{0:U.} {1:u.} {2:L:} {3:l:}", .... 4 parameters );</code> | ||
/// <code>string.Format(StringCaseFormatter.Default, "{0:Ul:} {1:Lu:}", ... 2 parameters );</code> | ||
/// <code>string.Format(new StringCaseFormatter(), "{0:U} {1:u} {2:L} {3:l}", ... 4 parameters );</code> | ||
/// </summary> | ||
public sealed class StringCaseFormatter : IFormatProvider, ICustomFormatter | ||
{ | ||
/// <summary> | ||
/// Static <see cref="StringCaseFormatter"/> using <see cref="CultureInfo.CurrentCulture"/>. | ||
/// </summary> | ||
public static readonly StringCaseFormatter Default = new(); | ||
|
||
public object? GetFormat(Type? formatType) | ||
=> formatType == typeof(ICustomFormatter) | ||
? this | ||
: null; | ||
|
||
public string Format( | ||
string? format, | ||
object? arg, | ||
IFormatProvider? formatProvider) | ||
{ | ||
var str = arg?.ToString() ?? string.Empty; | ||
|
||
return format switch | ||
{ | ||
"U" => str.ToUpper(CultureInfo.CurrentCulture), | ||
"Ul" => str.ToUpper(CultureInfo.CurrentCulture).EnsureFirstCharacterToLower(), | ||
"u" => str.EnsureFirstCharacterToUpper(), | ||
"L" => str.ToLower(CultureInfo.CurrentCulture), | ||
"Lu" => str.ToLower(CultureInfo.CurrentCulture).EnsureFirstCharacterToUpper(), | ||
"l" => str.EnsureFirstCharacterToLower(), | ||
|
||
"U." => str.ToUpper(CultureInfo.CurrentCulture).EnsureEndsWithDot(), | ||
"Ul." => str.ToUpper(CultureInfo.CurrentCulture).EnsureFirstCharacterToLower().EnsureEndsWithDot(), | ||
"u." => str.EnsureFirstCharacterToUpper().EnsureEndsWithDot(), | ||
"L." => str.ToLower(CultureInfo.CurrentCulture).EnsureEndsWithDot(), | ||
"Lu." => str.ToLower(CultureInfo.CurrentCulture).EnsureFirstCharacterToUpper().EnsureEndsWithDot(), | ||
"l." => str.EnsureFirstCharacterToLower().EnsureEndsWithDot(), | ||
|
||
"U:" => str.ToUpper(CultureInfo.CurrentCulture).EnsureEndsWithColon(), | ||
"Ul:" => str.ToUpper(CultureInfo.CurrentCulture).EnsureFirstCharacterToLower().EnsureEndsWithColon(), | ||
"u:" => str.EnsureFirstCharacterToUpper().EnsureEndsWithColon(), | ||
"L:" => str.ToLower(CultureInfo.CurrentCulture).EnsureEndsWithColon(), | ||
"Lu:" => str.ToLower(CultureInfo.CurrentCulture).EnsureFirstCharacterToUpper().EnsureEndsWithColon(), | ||
"l:" => str.EnsureFirstCharacterToLower().EnsureEndsWithColon(), | ||
|
||
_ => str, | ||
}; | ||
} | ||
} |
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,110 @@ | ||
// ReSharper disable StringLiteralTypo | ||
#pragma warning disable xUnit1012 | ||
namespace Atc.Tests.Formatters; | ||
|
||
public class StringCaseFormatterTests | ||
{ | ||
private readonly StringCaseFormatter formatter = StringCaseFormatter.Default; | ||
|
||
[Theory] | ||
[InlineData("test", "TesT", "L")] | ||
[InlineData("tesT", "TesT", "l")] | ||
[InlineData("TEST", "TesT", "U")] | ||
[InlineData("TesT", "TesT", "u")] | ||
[InlineData("test", "tESt", "L")] | ||
[InlineData("tESt", "tESt", "l")] | ||
[InlineData("TEST", "tESt", "U")] | ||
[InlineData("TESt", "tESt", "u")] | ||
[InlineData("test.", "TesT", "L.")] | ||
[InlineData("tesT.", "TesT", "l.")] | ||
[InlineData("TEST.", "TesT", "U.")] | ||
[InlineData("TesT.", "TesT", "u.")] | ||
[InlineData("test.", "tESt", "L.")] | ||
[InlineData("tESt.", "tESt", "l.")] | ||
[InlineData("TEST.", "tESt", "U.")] | ||
[InlineData("TESt.", "tESt", "u.")] | ||
[InlineData("test:", "TesT", "L:")] | ||
[InlineData("tesT:", "TesT", "l:")] | ||
[InlineData("TEST:", "TesT", "U:")] | ||
[InlineData("TesT:", "TesT", "u:")] | ||
[InlineData("test:", "tESt", "L:")] | ||
[InlineData("tESt:", "tESt", "l:")] | ||
[InlineData("TEST:", "tESt", "U:")] | ||
[InlineData("TESt:", "tESt", "u:")] | ||
public void FormatStringForDefault( | ||
string expected, | ||
string input, | ||
string formatSpecifier) | ||
=> Assert.Equal( | ||
expected, | ||
string.Format(StringCaseFormatter.Default, $"{{0:{formatSpecifier}}}", input)); | ||
|
||
[Theory] | ||
[InlineData("test", "test", "L")] | ||
[InlineData("test", "TEST", "L")] | ||
[InlineData("test", "test", "l")] | ||
[InlineData("tEST", "TEST", "l")] | ||
[InlineData("TEST", "test", "U")] | ||
[InlineData("TEST", "TEST", "U")] | ||
[InlineData("Test", "test", "u")] | ||
[InlineData("TEST", "TEST", "u")] | ||
[InlineData("test", "test", "x")] | ||
[InlineData("test", "test", "")] | ||
[InlineData("", null, "U")] | ||
[InlineData("john doe", "john doe", "L")] | ||
[InlineData("john doe", "john doe", "l")] | ||
[InlineData("JOHN DOE", "john doe", "U")] | ||
[InlineData("John doe", "john doe", "u")] | ||
[InlineData("john doe", "JOHN DOE", "L")] | ||
[InlineData("jOHN DOE", "JOHN DOE", "l")] | ||
[InlineData("JOHN DOE", "JOHN DOE", "U")] | ||
[InlineData("JOHN DOE", "JOHN DOE", "u")] | ||
[InlineData("John doe", "JOHN DOE", "Lu")] | ||
[InlineData("jOHN DOE", "JOHN DOE", "Ul")] | ||
[InlineData("John doe.", "john doe", "Lu.")] | ||
[InlineData("jOHN DOE.", "JOHN DOE", "Ul.")] | ||
[InlineData("John doe:", "john doe", "Lu:")] | ||
[InlineData("jOHN DOE:", "john doe", "Ul:")] | ||
public void FormatStringForOneParameter( | ||
string expected, | ||
string input, | ||
string formatSpecifier) | ||
=> Assert.Equal( | ||
expected, | ||
string.Format(formatter, $"{{0:{formatSpecifier}}}", input)); | ||
|
||
[Theory] | ||
[InlineData("hallo world", "Hallo", "World", "L", "L")] | ||
[InlineData("hallo world", "Hallo", "World", "L", "l")] | ||
[InlineData("hallo world", "Hallo", "World", "l", "l")] | ||
[InlineData("hallo world", "Hallo", "World", "l", "L")] | ||
[InlineData("hallo world", "HALLO", "WORLD", "L", "L")] | ||
[InlineData("hallo wORLD", "HALLO", "WORLD", "L", "l")] | ||
[InlineData("hALLO wORLD", "HALLO", "WORLD", "l", "l")] | ||
[InlineData("hALLO world", "HALLO", "WORLD", "l", "L")] | ||
[InlineData("hallo world", "hallo", "world", "L", "L")] | ||
[InlineData("hallo world", "hallo", "world", "L", "l")] | ||
[InlineData("hallo world", "hallo", "world", "l", "l")] | ||
[InlineData("hallo world", "hallo", "world", "l", "L")] | ||
[InlineData("HALLO WORLD", "Hallo", "World", "U", "U")] | ||
[InlineData("HALLO World", "Hallo", "World", "U", "u")] | ||
[InlineData("Hallo World", "Hallo", "World", "u", "u")] | ||
[InlineData("Hallo WORLD", "Hallo", "World", "u", "U")] | ||
[InlineData("HALLO WORLD", "HALLO", "WORLD", "U", "U")] | ||
[InlineData("HALLO WORLD", "HALLO", "WORLD", "U", "u")] | ||
[InlineData("HALLO WORLD", "HALLO", "WORLD", "u", "u")] | ||
[InlineData("HALLO WORLD", "HALLO", "WORLD", "u", "U")] | ||
[InlineData("HALLO WORLD", "hallo", "world", "U", "U")] | ||
[InlineData("HALLO World", "hallo", "world", "U", "u")] | ||
[InlineData("Hallo World", "hallo", "world", "u", "u")] | ||
[InlineData("Hallo WORLD", "hallo", "world", "u", "U")] | ||
public void FormatStringForTwoParameters( | ||
string expected, | ||
string input1, | ||
string input2, | ||
string formatSpecifier1, | ||
string formatSpecifier2) | ||
=> Assert.Equal( | ||
expected, | ||
string.Format(formatter, $"{{0:{formatSpecifier1}}} {{1:{formatSpecifier2}}}", input1, input2)); | ||
} |