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

Added logic to properly treat underscores, dashes and spaces. Fixes #318 #341

Merged
merged 7 commits into from
Oct 29, 2014
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###In Development
- [#341](https://github.com/MehdiK/Humanizer/pull/341): Added logic to properly treat underscores, dashes and spaces.

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.29.0...master)

Expand Down
10 changes: 10 additions & 0 deletions src/Humanizer.Tests/StringHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class StringHumanizeTests
[InlineData("10IsInTheBegining", "10 is in the begining")]
[InlineData("NumberIsAtTheEnd100", "Number is at the end 100")]
[InlineData("XIsFirstWordInTheSentence", "X is first word in the sentence")]
[InlineData("XIsFirstWordInTheSentence ThenThereIsASpace", "X is first word in the sentence then there is a space")]
public void CanHumanizeStringInPascalCase(string input, string expectedResult)
{
Assert.Equal(expectedResult, input.Humanize());
Expand All @@ -19,6 +20,14 @@ public void CanHumanizeStringInPascalCase(string input, string expectedResult)
[Theory]
[InlineData("Underscored_input_string_is_turned_into_sentence", "Underscored input string is turned into sentence")]
[InlineData("Underscored_input_String_is_turned_INTO_sentence", "Underscored input String is turned INTO sentence")]
[InlineData("TEST 1 - THIS IS A TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1 -THIS IS A TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1- THIS IS A TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1_ THIS IS A TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1 _THIS IS A TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1 _ THIS IS A TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1 - THIS_IS_A_TEST", "TEST 1 THIS IS A TEST")]
[InlineData("TEST 1 - THIS is A Test", "TEST 1 THIS is A test")]
public void CanHumanizeStringWithUnderscores(string input, string expectedReseult)
{
Assert.Equal(expectedReseult, input.Humanize());
Expand All @@ -28,6 +37,7 @@ public void CanHumanizeStringWithUnderscores(string input, string expectedReseul
[InlineData("HTML", "HTML")]
[InlineData("TheHTMLLanguage", "The HTML language")]
[InlineData("HTMLIsTheLanguage", "HTML is the language")]
[InlineData("TheLanguage IsHTML", "The language is HTML")]
[InlineData("TheLanguageIsHTML", "The language is HTML")]
[InlineData("HTML5", "HTML 5")]
[InlineData("1HTML", "1 HTML")]
Expand Down
14 changes: 11 additions & 3 deletions src/Humanizer/StringHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ static string FromPascalCase(string input)
(?# acronym to number)
(?<=[A-Z])(?=[0-9])|
(?# acronym to word)
(?<=[A-Z])(?=[A-Z][a-z])
(?<=[A-Z])(?=[A-Z][a-z])|
(?# words/acronyms/numbers separated by space)
(?<=[^\s])(?=[\s])
", RegexOptions.IgnorePatternWhitespace);

var result = pascalCaseWordBoundaryRegex
.Split(input)
.Select(word =>
word.ToCharArray().All(Char.IsUpper) && word.Length > 1
word.Trim().ToCharArray().All(Char.IsUpper) && word.Length > 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In consideration of performance, can you do the Trim() once.

I know you didn't introduced it, but can you also remove ToCharArray(), it should work without it anyway.

? word
: word.ToLower())
.Aggregate((res, word) => res + " " + word);
.Aggregate((res, word) => res + " " + word.Trim());

result = Char.ToUpper(result[0]) +
result.Substring(1, result.Length - 1);
Expand All @@ -51,6 +53,12 @@ public static string Humanize(this string input)
if (input.ToCharArray().All(Char.IsUpper))
return input;

// if input contains a dash or underscore which preceeds or follows a space (or both, i.g. free-standing)
// remove the dash/underscore and run it through FromPascalCase
Regex r = new Regex(@"[\s]{1}[-_]|[-_][\s]{1}", RegexOptions.IgnoreCase);
if (r.IsMatch(input))
return FromPascalCase(FromUnderscoreDashSeparatedWords(input));

if (input.Contains("_") || input.Contains("-"))
return FromUnderscoreDashSeparatedWords(input);

Expand Down