Skip to content

Commit

Permalink
Merge pull request #1074 from seikanz/1006
Browse files Browse the repository at this point in the history
Fixes #1006: conjunctions etc lower case in Title Case
  • Loading branch information
clairernovotny authored Jun 7, 2021
2 parents e037d6e + a19323a commit 896ded1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Humanizer.Tests.Shared/InflectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public void SingularizeSkipSimpleWords(string singular, string plural)
[InlineData("some title", "Some Title")]
[InlineData("some-title", "Some Title")]
[InlineData("sometitle", "Sometitle")]
[InlineData("some-title: The begining", "Some Title: The Begining")]
[InlineData("some_title:_the_begining", "Some Title: The Begining")]
[InlineData("some title: The_begining", "Some Title: The Begining")]
[InlineData("some-title: The beginning", "Some Title: The Beginning")]
[InlineData("some_title:_the_beginning", "Some Title: the Beginning")]
[InlineData("some title: The_beginning", "Some Title: The Beginning")]
public void Titleize(string input, string expectedOuput)
{
Assert.Equal(expectedOuput, input.Titleize());
Expand Down
1 change: 1 addition & 0 deletions src/Humanizer.Tests.Shared/StringHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void CanHumanizeStringWithAcronyms(string input, string expectedValue)
[Theory]
[InlineData("CanReturnTitleCase", "Can Return Title Case")]
[InlineData("Can_return_title_Case", "Can Return Title Case")]
[InlineData("In titles use lower case for prepositions an article or conjunctions", "In Titles Use Lower Case for Prepositions an Article or Conjunctions")]
[InlineData("Title_humanization_Honors_ALLCAPS", "Title Humanization Honors ALLCAPS")]
[InlineData("MühldorferStraße23", "Mühldorfer Straße 23")]
[InlineData("mühldorfer_STRAẞE_23", "Mühldorfer STRAẞE 23")]
Expand Down
2 changes: 1 addition & 1 deletion src/Humanizer.Tests.Shared/TransformersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TransformersTests
[InlineData("Sentence casing", "Sentence Casing")]
[InlineData("honors UPPER case", "Honors UPPER Case")]
[InlineData("INvalid caSEs arE corrected", "Invalid Cases Are Corrected")]
[InlineData("Can deal w 1 letter words as i do", "Can Deal W 1 Letter Words As I Do")]
[InlineData("Can deal w 1 letter words as i do", "Can Deal W 1 Letter Words as I Do")]
[InlineData(" random spaces are HONORED too ", " Random Spaces Are HONORED Too ")]
[InlineData("Title Case", "Title Case")]
[InlineData("apostrophe's aren't capitalized", "Apostrophe's Aren't Capitalized")]
Expand Down
29 changes: 25 additions & 4 deletions src/Humanizer/Transformer/ToTitleCase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;

Expand All @@ -17,12 +18,14 @@ public string Transform(string input, CultureInfo culture)

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

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

private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture)
private static string ReplaceWithTitleCase(Match word, string source, CultureInfo culture, bool firstWord)
{
var articles = new List<string> { "a", "an", "the" };
var conjunctions = new List<string> { "and", "as", "but", "if", "nor", "or", "so", "yet" };
var prepositions = new List<string> { "as", "at", "by", "for", "in", "of", "off", "on", "to", "up", "via" };

var wordToConvert = word.Value;
var replacement = culture.TextInfo.ToUpper(wordToConvert[0]) + culture.TextInfo.ToLower(wordToConvert.Remove(0, 1));
string replacement;


if (firstWord ||
(!articles.Contains(wordToConvert) &&
!conjunctions.Contains(wordToConvert) &&
!prepositions.Contains(wordToConvert)))
{
replacement = culture.TextInfo.ToUpper(wordToConvert[0]) + culture.TextInfo.ToLower(wordToConvert.Remove(0, 1));

}
else
{
replacement = culture.TextInfo.ToLower(wordToConvert);
}
return source.Substring(0, word.Index) + replacement + source.Substring(word.Index + word.Length);
}
}
Expand Down

0 comments on commit 896ded1

Please sign in to comment.