Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform with Culture via ICulturedStringTransformer #1063

Merged
merged 2 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Humanizer/Transformer/ICulturedStringTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Globalization;

namespace Humanizer
{
/// <summary>
/// Can transform a string with the given culture
/// </summary>
public interface ICulturedStringTransformer : IStringTransformer
{
/// <summary>
/// Transform the input
/// </summary>
/// <param name="input">String to be transformed</param>
/// <param name="culture">The culture</param>
/// <returns></returns>
string Transform(string input, CultureInfo culture);
}
}
2 changes: 1 addition & 1 deletion src/Humanizer/Transformer/IStringTransformer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Humanizer
{
/// <summary>
/// Can tranform a string
/// Can transform a string
/// </summary>
public interface IStringTransformer
{
Expand Down
23 changes: 18 additions & 5 deletions src/Humanizer/Transformer/To.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Globalization;
using System.Linq;

namespace Humanizer
{
Expand All @@ -18,13 +19,25 @@ public static string Transform(this string input, params IStringTransformer[] tr
return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current));
}

/// <summary>
/// Transforms a string using the provided transformers. Transformations are applied in the provided order.
/// </summary>
/// <param name="input"></param>
/// <param name="culture"></param>
/// <param name="transformers"></param>
/// <returns></returns>
public static string Transform(this string input, CultureInfo culture, params ICulturedStringTransformer[] transformers)
{
return transformers.Aggregate(input, (current, stringTransformer) => stringTransformer.Transform(current, culture));
}

/// <summary>
/// Changes string to title case
/// </summary>
/// <example>
/// "INvalid caSEs arE corrected" -> "Invalid Cases Are Corrected"
/// </example>
public static IStringTransformer TitleCase
public static ICulturedStringTransformer TitleCase
{
get
{
Expand All @@ -38,7 +51,7 @@ public static IStringTransformer TitleCase
/// <example>
/// "Sentence casing" -> "sentence casing"
/// </example>
public static IStringTransformer LowerCase
public static ICulturedStringTransformer LowerCase
{
get
{
Expand All @@ -52,7 +65,7 @@ public static IStringTransformer LowerCase
/// <example>
/// "lower case statement" -> "LOWER CASE STATEMENT"
/// </example>
public static IStringTransformer UpperCase
public static ICulturedStringTransformer UpperCase
{
get
{
Expand All @@ -66,7 +79,7 @@ public static IStringTransformer UpperCase
/// <example>
/// "lower case statement" -> "Lower case statement"
/// </example>
public static IStringTransformer SentenceCase
public static ICulturedStringTransformer SentenceCase
{
get
{
Expand Down
11 changes: 9 additions & 2 deletions src/Humanizer/Transformer/ToLowerCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Humanizer
{
internal class ToLowerCase : IStringTransformer
internal class ToLowerCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return CultureInfo.CurrentCulture.TextInfo.ToLower(input);
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

return culture.TextInfo.ToLower(input);
}
}
}
15 changes: 12 additions & 3 deletions src/Humanizer/Transformer/ToSentenceCase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
using System.Globalization;

namespace Humanizer
{
internal class ToSentenceCase : IStringTransformer
internal class ToSentenceCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

if (input.Length >= 1)
{
return string.Concat(input.Substring(0, 1).ToUpper(), input.Substring(1));
return culture.TextInfo.ToUpper(input[0]) + input.Substring(1);
}

return input.ToUpper();
return culture.TextInfo.ToUpper(input);
}
}
}
18 changes: 13 additions & 5 deletions src/Humanizer/Transformer/ToTitleCase.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
using System.Linq;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

namespace Humanizer
{
internal class ToTitleCase : IStringTransformer
internal class ToTitleCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

var result = input;
var matches = Regex.Matches(input, @"(\w|[^\u0000-\u007F])+'?\w*");
foreach (Match word in matches)
{
if (!AllCapitals(word.Value))
{
result = ReplaceWithTitleCase(word, result);
result = ReplaceWithTitleCase(word, result, culture);
}
}

Expand All @@ -25,10 +33,10 @@ private static bool AllCapitals(string input)
return input.ToCharArray().All(char.IsUpper);
}

private static string ReplaceWithTitleCase(Match word, string source)
private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture)
{
var wordToConvert = word.Value;
var replacement = char.ToUpper(wordToConvert[0]) + wordToConvert.Remove(0, 1).ToLower();
var replacement = culture.TextInfo.ToUpper(wordToConvert[0]) + culture.TextInfo.ToLower(wordToConvert.Remove(0, 1));
return source.Substring(0, word.Index) + replacement + source.Substring(word.Index + word.Length);
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/Humanizer/Transformer/ToUpperCase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using System.Globalization;

namespace Humanizer
{
internal class ToUpperCase : IStringTransformer
internal class ToUpperCase : ICulturedStringTransformer
{
public string Transform(string input)
{
return input.ToUpper();
return Transform(input, null);
}

public string Transform(string input, CultureInfo culture)
{
culture ??= CultureInfo.CurrentCulture;

return culture.TextInfo.ToUpper(input);
}
}
}