Skip to content

Commit

Permalink
Always captilize last word in title case
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayyy committed Mar 22, 2024
1 parent b1f6fb1 commit c64528d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/titles/titleFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ export async function toTitleCase(str: string, isCustom: boolean): Promise<strin
// For non-custom, allow any that isn't all caps
result += word + " ";
} else if ((!Config.config?.onlyTitleCaseInEnglish || isEnglish)
&& !startOfSentence(index, words) && listHasWord(titleCaseNotCapitalized, word.toLowerCase())) {
&& !startOfSentence(index, words) && !endOfSentence(index, words)
&& listHasWord(titleCaseNotCapitalized, word.toLowerCase())) {
// Skip lowercase check for the first word
result += await toLowerCase(word, isTurkiq) + " ";
} else if (isFirstLetterCapital(word) &&
Expand Down Expand Up @@ -447,8 +448,18 @@ function startOfSentence(index: number, words: string[]): boolean {
return index === 0 || isDelimeter(words[index - 1]);
}

function endOfSentence(index: number, words: string[]): boolean {
return index === words.length - 1
|| isDelimeter(words[index]) // "word!" counts as delimeter
|| (!!words[index + 1] && isEntirelyDelimeter(words[index + 1]));
}

function isEntirelyDelimeter(word: string): boolean {
return word.match(/^[-:;~|]$/) !== null;
}

function isDelimeter(word: string): boolean {
return (word.match(/^[-:;~|]$/) !== null
return (isEntirelyDelimeter(word)
|| word.match(/[:?.!\]]$/) !== null)
&& !listHasWord(allowlistedWords, word)
&& !listHasWord(notStartOfSentence, word)
Expand Down
5 changes: 5 additions & 0 deletions test/titleFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ describe("toTitleCase", () => {
["Some title #lowercasething", "Some Title #lowercasething"],
["TWO WORLDS II (Zero Punctuation)", "Two Worlds II (Zero Punctuation)"],
["Riding a VIA train", "Riding a VIA Train"],
["Pandora Hearts pocket watch of real life with lacie melody edited of", "Pandora Hearts Pocket Watch of Real Life with Lacie Melody Edited Of"],
["Pandora Hearts pocket watch of real life with lacie melody edited of!!!!", "Pandora Hearts Pocket Watch of Real Life with Lacie Melody Edited Of"],
["Pandora Hearts pocket watch of real life with lacie melody edited of - an important thing", "Pandora Hearts Pocket Watch of Real Life with Lacie Melody Edited Of - An Important Thing"],
// in is inch so is allowlisted unless manually capitalized
["Pandora Hearts pocket watch in real life with lacie melody edited In", "Pandora Hearts Pocket Watch in Real Life with Lacie Melody Edited In"],
];
for (const testCase of titleCases) {
const [input, expected] = testCase;
Expand Down

0 comments on commit c64528d

Please sign in to comment.