-
Notifications
You must be signed in to change notification settings - Fork 966
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
Conversation
The regex can be refined to exclude leading and trailing spaces from the results of pascalCaseWordBoundary.Split(). For now, word.Trim() is added to discard the unneeded space.
word.ToCharArray().All(Char.IsUpper) && word.Length > 1 | ||
? word | ||
: word.ToLower()) | ||
word.Trim().ToCharArray().All(Char.IsUpper) && word.Length > 1 |
There was a problem hiding this comment.
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.
Thanks for the PR. Please add it to the release notes as mentioned in CONTRIBUTING.md. It would be cool if you would address the comments I've made. |
Thanks for the feedback Max. I will address your comments today. |
@mexx As requested, reduced the number of Trim()s, simplified the regex and updated the release_notes.md. However, I was not able to remove ToCharArray(). MSBuild spat out the following error:
|
Thanks for the contribution @nzubair. I think dash shouldn't be removed as part of humanization. For example "left-handed people".Humanize() should still return "left-handed people" (I understand there is no real rule around this and it could be quite subjective but I think leaving it in makes more sense). The reason underscore is being removed is that underscore normally doesn't make sense in a human readable sentence and removing it normally results into a more human readable sentence. This behavior is highlighted by the name of the covering test (CanHumanizeStringWithUnderscores). You've added a few test cases to that test that are checking for dashes which don't fit there, firstly because of the method name and more importantly because I don't think we should remove dashes. As per #318, as I mentioned in the comments, "Humanize should leave the text alone (because as far as it can see it's already human-readable)" regardless of how many times it's called on it. |
@MehdiK We already remove dashes, look at the implementation. |
Hi @MehdiK, Thank you for taking time to review the PR and for providing feedback. However, I'm a bit confused as what you are describing contradicts the behavior of Humanizer at present. Dashes Spaces (and impact on all uppercase strings) Can you please let me know if my interpretation of the expected behavior is incorrect? Thanks again for your time. |
This is now released to NuGet as v1.30.0. Thanks for the great contribution. |
This PR addresses behavior observed in issues #318.
Primarily, handling of underscores and dashes which do not connect words. They either have a space before or after them, or both, as was the case for this bug.
Additionally, the regex in FromPascalCase was not handling spaces in a string properly. Amended the regex to handle the spaces. It can be improved, given my lack of mastery of .Net regular expression.
Finally, a few test cases were added to test different combinations of spaces, dashes and underscores.