All files / string trainCase.ts

100% Statements 14/14
50% Branches 1/2
100% Functions 3/3
100% Lines 13/13

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                    17x     1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x      
import { splitByCase } from './splitByCase'
import { upperFirst } from './upperFirst'
 
/**
 * split string and joins by Train-Case (a.k.a. HTTP-Header-Case) convention
 *
 * @param text string to format
 * @returns train case string
 */
export function trainCase(text: string) {
  return splitByCase(text).map(word => upperFirst(word.toLowerCase())).join('-')
}
 
Eif (import.meta.vitest) {
  const { test, expect } = import.meta.vitest
 
  test('trainCase', () => {
    expect(trainCase('useXx')).toBe('Use-Xx')
    expect(trainCase('usexx')).toBe('Usexx')
    expect(trainCase('use-xx')).toBe('Use-Xx')
    expect(trainCase('use xx')).toBe('Use-Xx')
    expect(trainCase('UseXx')).toBe('Use-Xx')
    expect(trainCase('use_xx')).toBe('Use-Xx')
    expect(trainCase('Use Xx')).toBe('Use-Xx')
    expect(trainCase('Use-Xx')).toBe('Use-Xx')
    expect(trainCase('USE XX')).toBe('Use-Xx')
  })
}