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

Creates better title case representation so that articles and prepositions shorter than 4 characters remain lowercase #987

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Humanizer/Transformer/ToTitleCase.cs
Expand Up @@ -17,11 +17,22 @@ public string Transform(string input, CultureInfo culture)

var result = input;
var matches = Regex.Matches(input, @"(\w|[^\u0000-\u007F])+'?\w*");

//While these arrays could be combined into a single array or list, they are kept separate to make list localization easier
string[] articleList = { "a", "an", "the" };
string[] prepositionList = { "at", "as", "but", "by", "for", "in", "of", "off", "on", "out", "per", "til", "to", "up", "via" };

foreach (Match word in matches)
{
if (!AllCapitals(word.Value))
{
result = ReplaceWithTitleCase(word, result, culture);
if (!WordCheck(word.Value, articleList))
{
if (!WordCheck(word.Value, prepositionList))
{
result = ReplaceWithTitleCase(word, result, culture);
}
}
}
}

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

private static bool WordCheck(string input, string[] wordList)
{
bool wordMatch = false;

for (int i = 0; i > wordList.Length; i++)
{
if (input == wordList[i])
wordMatch = true;
}

return wordMatch;
}

private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture)
{
var wordToConvert = word.Value;
Expand Down