Skip to content

Commit

Permalink
feat: better titles
Browse files Browse the repository at this point in the history
  • Loading branch information
asartalo committed Jun 13, 2022
1 parent 68a42e6 commit 709596b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib/titleize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ describe('titlelize', () => {
input: 'MightMakesRight',
expected: 'Might Makes Right',
},
{
input: 'and-it-begins',
expected: 'And It Begins',
},
{
input: 'jack_and_jill',
expected: 'Jack and Jill',
},
{
input: 'the goat and the goatee',
expected: 'The Goat and the Goatee',
},
{
input: 'This is preposterous',
expected: 'This Is Preposterous',
},
{
input: 'What is the best thing to do in an emergency?',
expected: 'What Is the Best Thing to Do in an Emergency?',
},
];

testData.forEach(({ input, expected }) => {
Expand Down
7 changes: 7 additions & 0 deletions src/lib/titlelize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const replaceRegexp = /[\s_-]/g;
const splitRegexp = /(?=[A-Z])/;
const innerWordsNoCap = new Set([
'and', 'the', 'to', 'in', 'an', 'a',
]);

function joinWords(newString: string, word: string) {
if (newString !== '' && innerWordsNoCap.has(word.toLowerCase())) {
return `${newString}${word.toLowerCase()} `;
}
return `${newString + word.charAt(0).toUpperCase() + word.slice(1)} `;
}

Expand Down

0 comments on commit 709596b

Please sign in to comment.