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

Fixes trailing question mark reported in #378. #381

Merged
merged 2 commits into from
Feb 12, 2015
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
2 changes: 1 addition & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###In Development

- [#381](https://github.com/MehdiK/Humanizer/pull/381): Fixes trailing question mark reported in #378.

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

Expand Down
3 changes: 3 additions & 0 deletions src/Humanizer.Tests/StringDehumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class StringDehumanizeTests
[InlineData("lower case sentence is camelized", "LowerCaseSentenceIsCamelized")]
[InlineData("AlreadyDehumanizedStringIsUntouched", "AlreadyDehumanizedStringIsUntouched")]
[InlineData("", "")]
[InlineData("A special character is removed?", "ASpecialCharacterIsRemoved")]
[InlineData("A special character is removed after a space ?", "ASpecialCharacterIsRemovedAfterASpace")]
[InlineData("Internal special characters ?)@ are removed", "InternalSpecialCharactersAreRemoved")]
public void CanDehumanizeIntoAPascalCaseWord(string input, string expectedResult)
{
Assert.Equal(expectedResult, input.Dehumanize());
Expand Down
6 changes: 6 additions & 0 deletions src/Humanizer.Tests/StringHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class StringHumanizeTests
[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")]
[InlineData("ContainsSpecial?)@Characters", "Contains special characters")]
[InlineData("a", "A")]
[InlineData("A", "A")]
[InlineData("?)@", "")]
[InlineData("?", "")]
[InlineData("", "")]
public void CanHumanizeStringInPascalCase(string input, string expectedResult)
{
Assert.Equal(expectedResult, input.Humanize());
Expand Down
15 changes: 5 additions & 10 deletions src/Humanizer/StringHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,15 @@ static string FromUnderscoreDashSeparatedWords (string input)

static string FromPascalCase(string input)
{
if (input.Length == 1)
return Char.ToUpper(input[0]).ToString();

var result = PascalCaseWordPartsRegex
var result = String.Join(" ", PascalCaseWordPartsRegex
.Matches(input).Cast<Match>()
.Select(match => match.Value.ToCharArray().All(Char.IsUpper) &&
(match.Value.Length > 1 || (match.Index > 0 && input[match.Index - 1] == ' ') || match.Value == "I")
? match.Value
: match.Value.ToLower())
.Aggregate((res, word) => res + " " + word);
: match.Value.ToLower()));

result = Char.ToUpper(result[0]) +
result.Substring(1, result.Length - 1);
return result;
return result.Length > 0 ? Char.ToUpper(result[0]) +
result.Substring(1, result.Length - 1) : result;
}

/// <summary>
Expand All @@ -53,7 +48,7 @@ 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)
// if input contains a dash or underscore which preceeds or follows a space (or both, e.g. free-standing)
// remove the dash/underscore and run it through FromPascalCase
if (FreestandingSpacingCharRegex.IsMatch(input))
return FromPascalCase(FromUnderscoreDashSeparatedWords(input));
Expand Down