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

Fix capitalizing words that start with numbers #346

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 packages/title-case/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ const TEST_CASES: [string, string, Options?][] = [
'An example. "I.e. test."',
{ sentenceCase: true },
],
["friday the 13th", "Friday the 13th"],
["21st century", "21st Century"],
];

describe("swap case", () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/title-case/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const IS_MANUAL_CASE = /\p{Ll}(?=[\p{Lu}])/u; // iPhone, iOS, etc.
const ALPHANUMERIC_PATTERN = /\p{Alphabetic}+/gu;
const IS_ACRONYM =
/^(\P{Alphabetic})*(?:\p{Alphabetic}\.){2,}(\P{Alphabetic})*$/u;
const IS_DIGIT = /\d/u;
Copy link
Owner

Choose a reason for hiding this comment

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

Can you change alphanumeric pattern instead? I think the bug is that it should consider numbers words too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@blakeembrey I've update the pattern and removed the check for a numeric digit I had added previously.

Thanks for taking the time to review this PR.


export const WORD_SEPARATORS = new Set(["—", "–", "-", "―", "/"]);

Expand Down Expand Up @@ -155,6 +156,12 @@ export function titleCase(
}
}

// Avoid capitalizing words that start with numbers.
// e.g.: 1st, 2nd
if (IS_DIGIT.test(token.charAt(wordIndex - 1))) {
continue;
}

value = upperAt(value, wordIndex, locale);
}

Expand Down