From b46d5a310cf20fd68953b2aef4baa7e9c76dab84 Mon Sep 17 00:00:00 2001 From: Jeremy Butler Date: Fri, 19 Jan 2024 15:37:46 +1000 Subject: [PATCH] Update formatters and tests --- src/2.formatters.ts | 46 +++++++++++++---------------------------- test/formatters.test.ts | 9 +++++--- 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/src/2.formatters.ts b/src/2.formatters.ts index d3a0083d..239689a1 100644 --- a/src/2.formatters.ts +++ b/src/2.formatters.ts @@ -110,34 +110,23 @@ export function formatUnixTime(timestamp: number): string { * @example formatList(['one', 'two', 'three']) */ export function formatList(items: any[], limit: number, conjunction: string = 'and'): string { - if (items.length === 1) { - return items[0] - } - - if (items.length === 2) { - return items.join(' ' + conjunction + ' ') - } - - if (items.length === 3) { - return items.slice(0, -1).join(', ') + ' ' + conjunction + ' ' + items.slice(-1) - } - - if (items.length > 3) { - return items.slice(0, limit).join(', ') + ' ' + conjunction + ' ' + (items.length - limit) + ' more' - } - + if (items.length === 1) return items[0] + if (items.length === 2) return items.join(' ' + conjunction + ' ') + if (items.length === 3) return items.slice(0, -1).join(', ') + ' ' + conjunction + ' ' + items.slice(-1) + if (items.length > 3) return items.slice(0, limit).join(', ') + ' ' + conjunction + ' ' + (items.length - limit) + ' more' return '' } /** * Converts a string to title case following the Chicago Manual of Style rules. * @reference https://www.chicagomanualofstyle.org/book/ed17/frontmatter/toc.html - * @example title('the quick brown fox jumps over the lazy dog') + */ export function formatTitle(text: string): string { - const exceptions = [ + const exceptions = new Set([ 'a', 'an', + 'to', 'the', 'for', 'and', @@ -147,6 +136,8 @@ export function formatTitle(text: string): string { 'yet', 'so', 'in', + 'is', + 'than', 'on', 'at', 'with', @@ -158,25 +149,16 @@ export function formatTitle(text: string): string { 'because', 'since', 'unless' - ] - - const capitalize = (word: string) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() + ]) return text .split(' ') .map((word, index, wordsArray) => { - const lowercaseWord = word.toLowerCase() - if (index > 0 && word.includes('-')) { - const [firstPart, secondPart] = word.split('-') - return capitalize(firstPart) + '-' + secondPart.toLowerCase() - } - if (index === 0 || index === wordsArray.length - 1 || !exceptions.includes(lowercaseWord)) { - return capitalize(lowercaseWord) - } - if (lowercaseWord === 'to' && index > 0 && wordsArray[index - 1].toLowerCase() !== 'to') { - return lowercaseWord + const lowerWord = word.toLowerCase() + if (index === 0 || index === wordsArray.length - 1 || !exceptions.has(lowerWord)) { + return word.charAt(0).toUpperCase() + word.slice(1) } - return exceptions.includes(lowercaseWord) ? lowercaseWord : capitalize(lowercaseWord) + return lowerWord }) .join(' ') } diff --git a/test/formatters.test.ts b/test/formatters.test.ts index 189d006e..09e52b34 100644 --- a/test/formatters.test.ts +++ b/test/formatters.test.ts @@ -30,13 +30,16 @@ test('formatUnixTime', () => { test('formatList', () => { expect(formatList(['Apple', 'Oranges'], 2)).toBe('Apple and Oranges') - expect(formatList(['Apple', 'Oranges', 'Bananas'], 2)).toBe('Apple, Oranges, and 1 more') - expect(formatList(['Apple', 'Oranges', 'Bananas', 'Pears'], 2)).toBe('Apple, Oranges, and 2 more') + expect(formatList(['Apple', 'Oranges', 'Bananas'], 2)).toBe('Apple, Oranges and Bananas') + expect(formatList(['Apple', 'Oranges', 'Bananas'], 0, 'or')).toBe('Apple, Oranges or Bananas') + expect(formatList(['Apple', 'Oranges', 'Bananas', 'Pears'], 2)).toBe('Apple, Oranges and 2 more') + expect(formatList(['Apple', 'Oranges', 'Bananas', 'Pears', 'Grapes'], 2)).toBe('Apple, Oranges and 3 more') + expect(formatList(['Apple', 'Oranges', 'Bananas', 'Pears', 'Grapes'], 2, 'or')).toBe('Apple, Oranges or 3 more') }) test('formatTitle', () => { expect(formatTitle('hello world')).toBe('Hello World') expect(formatTitle('welcome to the jungle')).toBe('Welcome to the Jungle') expect(formatTitle('the quick brown fox jumps over the lazy dog')).toBe('The Quick Brown Fox Jumps Over the Lazy Dog') - // expect(formatTitle('UseMods is cooler than a vegan leather jacket, it's offical')).toBe('UseMods Is Cooler than a Vegan Leather Jacket, it's Offical') + expect(formatTitle('UseMods is cooler than a vegan leather jacket')).toBe('UseMods is Cooler than a Vegan Leather Jacket') })