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

Make sure Humanize(LetterCasing.Sentence) does not remove commas #1185

Merged
merged 1 commit into from
Feb 15, 2024
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: 2 additions & 0 deletions src/Humanizer.Tests.Shared/StringHumanizeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public void CanHumanizeIntoLowerCase(string input, string expectedResult)
[InlineData("CanReturnSentenceCase", "Can return sentence case")]
[InlineData("", "")]
[InlineData("égoïste", "Égoïste")]
[InlineData("Normal; Normal and PascalCase", "Normal; normal and pascal case")]
[InlineData("I,and No One else", "I, and no one else")]
public void CanHumanizeIntoSentenceCase(string input, string expectedResult)
{
Assert.Equal(expectedResult, input.Humanize(LetterCasing.Sentence));
Expand Down
9 changes: 8 additions & 1 deletion src/Humanizer/StringHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ public static class StringHumanizeExtensions
private static readonly Regex PascalCaseWordPartsRegex;
private static readonly Regex FreestandingSpacingCharRegex;

private const string OptionallyCapitalizedWord = @"\p{Lu}?\p{Ll}+";
private const string IntegerAndOptionalLowercaseLetters = @"[0-9]+\p{Ll}*";
private const string Acronym = @"\p{Lu}+(?=\p{Lu}|[0-9]|\b)";
private const string SequenceOfOtherLetters = @"\p{Lo}+";
private const string MidSentencePunctuation = "[,;]?";

static StringHumanizeExtensions()
{
PascalCaseWordPartsRegex = new Regex(@"[\p{Lu}]?[\p{Ll}]+|[0-9]+[\p{Ll}]*|[\p{Lu}]+(?=[\p{Lu}][\p{Ll}]|[0-9]|\b)|[\p{Lo}]+",
PascalCaseWordPartsRegex = new Regex(
$"({OptionallyCapitalizedWord}|{IntegerAndOptionalLowercaseLetters}|{Acronym}|{SequenceOfOtherLetters}){MidSentencePunctuation}",
RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture | RegexOptionsUtil.Compiled);
FreestandingSpacingCharRegex = new Regex(@"\s[-_]|[-_]\s", RegexOptionsUtil.Compiled);
}
Expand Down