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

Fixed issue #1102. Dehumanize() by pascalizing the words correctly #1122

Merged
merged 1 commit into from Sep 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/Humanizer.Tests.Shared/StringDehumanizeTests.cs
Expand Up @@ -5,15 +5,26 @@ namespace Humanizer.Tests
public class StringDehumanizeTests
{
[Theory]
[InlineData("Pascal case sentence is camelized", "PascalCaseSentenceIsCamelized")]
[InlineData("Title Case Sentence Is Camelized", "TitleCaseSentenceIsCamelized")]
[InlineData("Mixed case sentence Is Camelized", "MixedCaseSentenceIsCamelized")]
[InlineData("lower case sentence is camelized", "LowerCaseSentenceIsCamelized")]
[InlineData("AlreadyDehumanizedStringIsUntouched", "AlreadyDehumanizedStringIsUntouched")]
[InlineData("", "")]
[InlineData("Pascal case sentence is pascalized", "PascalCaseSentenceIsPascalized")]
[InlineData("Title Case Sentence Is Pascalized", "TitleCaseSentenceIsPascalized")]
[InlineData("Mixed case sentence Is Pascalized", "MixedCaseSentenceIsPascalized")]
[InlineData("lower case sentence is pascalized", "LowerCaseSentenceIsPascalized")]
[InlineData("A special character is removed?", "ASpecialCharacterIsRemoved")]
[InlineData("A special character is removed after a space ?", "ASpecialCharacterIsRemovedAfterASpace")]
[InlineData("Internal special characters ?)@ are removed", "InternalSpecialCharactersAreRemoved")]
[InlineData("AlreadyDehumanizedStringIsUntouched", "AlreadyDehumanizedStringIsUntouched")]
[InlineData("CanDehumanizeIntoAPascalCaseWord", "CanDehumanizeIntoAPascalCaseWord")]
[InlineData("CanDehumanizeIntoAPascalCaseWord AndAnother", "CanDehumanizeIntoAPascalCaseWordAndAnother")]
[InlineData("OneAndTwo", "OneAndTwo")]
[InlineData("OneOrTwo", "OneOrTwo")]
[InlineData("OneOfTwo", "OneOfTwo")]
[InlineData("OneButTwo", "OneButTwo")]
[InlineData("OneATwo", "OneATwo")]
[InlineData("OneAsTwo", "OneAsTwo")]
[InlineData("OneYetTwo", "OneYetTwo")]
[InlineData("OneNorTwo", "OneNorTwo")]
[InlineData("WordSoTwo", "WordSoTwo")]
public void CanDehumanizeIntoAPascalCaseWord(string input, string expectedResult)
{
Assert.Equal(expectedResult, input.Dehumanize());
Expand Down
10 changes: 8 additions & 2 deletions src/Humanizer/StringDehumanizeExtensions.cs
@@ -1,5 +1,7 @@
using System.Linq;

using Humanizer;

namespace Humanizer
{
/// <summary>
Expand All @@ -9,13 +11,17 @@ public static class StringDehumanizeExtensions
{
/// <summary>
/// Dehumanizes a string; e.g. 'some string', 'Some String', 'Some string' -> 'SomeString'
/// If a string is already dehumanized then it leaves it alone 'SomeStringAndAnotherString' -> 'SomeStringAndAnotherString'
/// </summary>
/// <param name="input">The string to be dehumanized</param>
/// <returns></returns>
public static string Dehumanize(this string input)
{
var titlizedWords = input.Split(' ').Select(word => word.Humanize(LetterCasing.Title));
return string.Join("", titlizedWords).Replace(" ", "");
var pascalizedWords = input.Split(' ').Select(word => word.Humanize().Pascalize());
return string.Join("", pascalizedWords).Replace(" ", "");
}
}
}