diff --git a/src/lib/titleize.spec.ts b/src/lib/titleize.spec.ts index 7f6787c..77e165c 100644 --- a/src/lib/titleize.spec.ts +++ b/src/lib/titleize.spec.ts @@ -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 }) => { diff --git a/src/lib/titlelize.ts b/src/lib/titlelize.ts index c3bc01a..8aad46b 100644 --- a/src/lib/titlelize.ts +++ b/src/lib/titlelize.ts @@ -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)} `; }