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

Unexpected behaviour in certain cases for .Dehumanize() #1102

Open
GordonRudman opened this issue Jul 2, 2021 · 4 comments
Open

Unexpected behaviour in certain cases for .Dehumanize() #1102

GordonRudman opened this issue Jul 2, 2021 · 4 comments

Comments

@GordonRudman
Copy link
Contributor

As far as I understand it the string "ListOfProducts" is already Dehumanized, so calling .Dehumanize() should basically have no effect, i.e. the output should equal the input in these cases.

However "ListOfProducts".Dehumanize() currently returns "ListofProducts" - The "Of" part got changed to "of" for some reason.

For more info, I would expect the output of the following lines to be exactly the same (i.e "ListOfProducts"):
var output1 = "List of products".Dehumanize();
var output2 = "List of products".Dehumanize().Dehumanize();
var output3 = "List of products".Dehumanize().Dehumanize().Dehumanize();

This unexpected change of casing is currently causing my software to break, because sometimes I'm forced to combine and therefore reprocess parts of a string in another part of my software, and the change of casing causes equality errors.
I'm using it to lookup class Types so the casing really matters, and I'm not sure how to work around the problem just yet.

@hangy
Copy link
Contributor

hangy commented Jul 3, 2021

Could you check if the string contains any spaces to avoid dehumanizing this case?

The dehumanize method doesn't allow any customization (as far as I can see), and basically tries to title-case every word in the input string:

var titlizedWords = input.Split(' ').Select(word => word.Humanize(LetterCasing.Title));
return string.Join("", titlizedWords).Replace(" ", "");

@GordonRudman
Copy link
Contributor Author

The problem here is with conjunctions or similar words (like And, Or, Of, But, A, Yet, Nor, So, As, etc), which don't get capitalized correctly.

So the code should be changed to the following (or something faster):
var pascalizedWords = input.Split(' ').Select(word => word.Humanize().Pascalize());
return string.Join("", pascalizedWords).Replace(" ", "");

The test method (CanDehumanizeIntoAPascalCaseWord) in StringDehumanizeTests.cs show that the intention was to indeed pascalize the output, and not just use Titlecase and it's quirks. As it stands the method even fails to Dehumanize/Pascalize 'CanDehumanizeIntoAPascalCaseWord' correctly to 'CanDehumanizeIntoAPascalCaseWord', because the 'A' gets unexpectedly lowercased.

I would suggest adding the following tests (and fixing references to camalized):
[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")]

GordonRudman added a commit to GordonRudman/Humanizer that referenced this issue Sep 12, 2021
…o expanded the tests to catch these edge cases
@GordonRudman
Copy link
Contributor Author

I've submitted the following PR to fix this problem:
#1122

@GordonRudman
Copy link
Contributor Author

I've already completed my PR, but I'd suggest adding these two tests too:
[InlineData("SomeHTMLPage", "SomeHTMLPage")]
[InlineData("Some HTML page", "SomeHTMLPage")]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants