Skip to content

Commit

Permalink
Add handling for emoji letters
Browse files Browse the repository at this point in the history
Resolves discussion in #78
  • Loading branch information
ajayyy committed Jul 20, 2023
1 parent 570d831 commit 64996f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/titles/titleFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ function isAllCaps(word: string): boolean {
export function capitalizeFirstLetter(word: string): string {
const result: string[] = [];

if (startsWithEmojiLetter(word)) {
// Emoji letter is already "capitalized"
return word.toLowerCase();
}

for (const char of word) {
if (char.match(/[\p{L}]/u)) {
// converts to an array in order to slice by Unicode code points
Expand All @@ -238,6 +243,10 @@ function isWordCapitalCase(word: string): boolean {
return !!word.match(/^[^\p{L}]*[\p{Lu}][^\p{Lu}]+$/u);
}

function startsWithEmojiLetter(word: string): boolean {
return !!word.match(/^[^\p{L}]*[🅰🆎🅱🆑🅾][^\p{Lu}]+$/u);
}

/**
* Not just capital at start
*/
Expand Down Expand Up @@ -377,9 +386,9 @@ export function cleanPunctuation(title: string): string {

export function cleanEmojis(title: string): string {
const cleaned = title
.replace(/ \p{Extended_Pictographic}+(?= )/ug, "") // Clear extra spaces between emoji "words"
.replace(/(\S)\p{Extended_Pictographic}(\S)/ug, "$1 $2") // Emojis in between letters should be spaces
.replace(/\p{Extended_Pictographic}/ug, "")
.replace(/ ((?=\p{Extended_Pictographic})(?=[^🅰🆎🅱🆑🅾])\S)+(?= )/ug, "") // Clear extra spaces between emoji "words"
.replace(/(\S)(?=\p{Extended_Pictographic})(?=[^🅰🆎🅱🆑🅾])\S(\S)/ug, "$1 $2") // Emojis in between letters should be spaces
.replace(/(?=\p{Extended_Pictographic})(?=[^🅰🆎🅱🆑🅾])\S/ug, "")
.trim();

if (cleaned.length > 0) {
Expand Down
6 changes: 5 additions & 1 deletion test/titleFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ describe("toTitleCase cleanEmojis", () => {
["5 Minute Timer [MOUSE 🐭 MAZE] 🐭", "5 Minute Timer [Mouse Maze]"],
["5 min countdown timer (roller coaster) 🎢", "5 Min Countdown Timer (Roller Coaster)"],
["5 min countdown timer (roller🎢coaster) 🎢", "5 Min Countdown Timer (Roller Coaster)"],
[" 🎢 🎢🎢 🎢🎢\t🎢", "🎢 🎢🎢 🎢🎢\t🎢"] // Leave emojis when there is no text
[" 🎢 🎢🎢 🎢🎢\t🎢", "🎢 🎢🎢 🎢🎢\t🎢"], // Leave emojis when there is no text
["Rush 🅱️", "Rush 🅱️"],
["5 min countdown timer (roller🎢🅱️oaster) 🎢", "5 Min Countdown Timer (Roller 🅱️oaster)"],
["5 min countdown 🎢🅱️🎢🎢 timer (roller coaster) 🎢", "5 Min Countdown 🅱️ Timer (Roller Coaster)"],
["🎢🅱️🎢🎢 5 min countdown timer (roller coaster) 🎢", "🅱️ 5 Min Countdown Timer (Roller Coaster)"],
];
for (const testCase of titleCases) {
const [input, expected] = testCase;
Expand Down

0 comments on commit 64996f0

Please sign in to comment.