Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 2x 2x 6x 1x 5x 1x 1x 1x 1x 1x | import { splitByCase } from './splitByCase'
import { upperFirst } from './upperFirst'
/**
* format string to title case
*
* @param text string to format
* @param ignore regexp to ignore format
* @returns title case string
*/
export function titleCase(text: string, ignore?: RegExp) {
const prepositions
// eslint-disable-next-line regexp/no-unused-capturing-group
= /^(a|an|and|as|at|but|by|for|if|in|is|nor|of|on|or|the|to|with)$/i
return splitByCase(text).map((word) => {
if (ignore?.test(word)) {
return word
}
return prepositions.test(word) ? word.toLowerCase() : upperFirst(word.toLocaleLowerCase())
}).join(' ')
}
Eif (import.meta.vitest) {
const { test, expect } = import.meta.vitest
test('titleCase', () => {
expect(titleCase('hello world')).toBe('Hello World')
expect(titleCase('install "usexx" with PNPM', /^PNPM$/)).toBe('Install "usexx" with PNPM')
})
}
|