Skip to content

Fix TitleCase not capitalizing the first word when it is an article, conjunction, or preposition (#1658)#1663

Open
RockyR0ads wants to merge 2 commits intoHumanizr:mainfrom
RockyR0ads:Fix-ToTitleCase-first-word-not-capitalizing
Open

Fix TitleCase not capitalizing the first word when it is an article, conjunction, or preposition (#1658)#1663
RockyR0ads wants to merge 2 commits intoHumanizr:mainfrom
RockyR0ads:Fix-ToTitleCase-first-word-not-capitalizing

Conversation

@RockyR0ads
Copy link

This PR fixes an issue where ToTitleCase failed to capitalize the first word when that word was an article, conjunction, or preposition. This resulted in incorrect output. According to standard English title‑casing rules, the first word must always be capitalized, regardless of its part of speech.

Fixes #1658

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 736cad7eb4

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 32 to 36
if(AllCapitals(value))
{
continue;
}
if (!isFirstWord && IsArticleOrConjunctionOrPreposition(value))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Mark all-caps first word as handled before skipping

When the first word is all caps, the continue exits before isFirstWord is flipped, so the next word is still treated as the first word. That means a conjunction/article after an all‑caps first word (e.g., "NASA and the future") will be capitalized as if it were first, which regresses the prior behavior where "and" stayed lowercase. This only happens when the first token is all caps, but it breaks standard title‑case rules for those inputs; consider updating isFirstWord before the continue or otherwise marking the first word as consumed.

Useful? React with 👍 / 👎.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an issue where ToTitleCase failed to capitalize the first word when it was an article, conjunction, or preposition, which violates standard English title-casing rules requiring the first word to always be capitalized.

Key Changes:

  • Introduced isFirstWord tracking in the title case transformation logic to ensure the first word is always capitalized regardless of its part of speech
  • Split the conditional logic to separately handle all-capitals words and article/conjunction/preposition exceptions
  • Added test cases covering articles, conjunctions, and prepositions as first words

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Humanizer/Transformer/ToTitleCase.cs Modified the Transform method to track whether the current word is the first word and only skip article/conjunction/preposition capitalization for non-first words
src/Humanizer.Tests/TransformersTests.cs Added new test method TransformToTitleCase_FirstWordExceptions with test cases for articles, conjunctions, and prepositions as first words

var value = word.Value;
if (AllCapitals(value) || IsArticleOrConjunctionOrPreposition(value))

if(AllCapitals(value))
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after 'if' keyword. Should be 'if (' instead of 'if(' to follow the project's consistent spacing convention.

Suggested change
if(AllCapitals(value))
if (AllCapitals(value))

Copilot uses AI. Check for mistakes.
Comment on lines 31 to 42

if(AllCapitals(value))
{
continue;
}
if (!isFirstWord && IsArticleOrConjunctionOrPreposition(value))
{
continue;
}

isFirstWord = false;

Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'isFirstWord' flag should be set to false after encountering the first word, even if that word is skipped due to being all-capitals. Currently, if the first word is all-caps (like an acronym), the flag remains true, causing the second word to be incorrectly treated as the first word and capitalized even if it's an article, conjunction, or preposition. Move the 'isFirstWord = false' assignment before the conditional checks.

Suggested change
if(AllCapitals(value))
{
continue;
}
if (!isFirstWord && IsArticleOrConjunctionOrPreposition(value))
{
continue;
}
isFirstWord = false;
var wasFirstWord = isFirstWord;
isFirstWord = false;
if(AllCapitals(value))
{
continue;
}
if (!wasFirstWord && IsArticleOrConjunctionOrPreposition(value))
{
continue;
}

Copilot uses AI. Check for mistakes.
Comment on lines 38 to 43
[Theory]
[InlineData("a great article", "A Great Article")]
[InlineData("yet another conjunction", "Yet Another Conjunction")]
[InlineData("by this preposition", "By This Preposition")]
public void TransformToTitleCase_FirstWordExceptions(string input, string expectedOutput) =>
Assert.Equal(expectedOutput, input.Transform(To.TitleCase));
Copy link

Copilot AI Jan 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test coverage for the edge case where the first word is all-capitals (like an acronym) followed by an article, conjunction, or preposition. For example, test cases like 'NASA and the mission' or 'API for developers' to ensure the second word is handled correctly based on whether it should be treated as appearing after the first word.

Copilot uses AI. Check for mistakes.
Copy link
Member

@clairernovotny clairernovotny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the bots

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

Successfully merging this pull request may close these issues.

TitleCase does not capitalize the first word if the first word is an article, conjunction, or preposition.

2 participants