Kebab-Casing for single characters

Benchmark created on


Setup

const a = 'a'
const b = 'ArrowUp'
const c = 'c'
const d = 'Ä'
const e = '-'
const f = 'Enter'

Test runner

Ready to run.

Testing in
TestOps/sec
'_' and ' ' special treatment
function kebabCase(subject) {
    if ([' ','_'].includes(subject
    )) return subject
    return subject.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[_\s]/, '-').toLowerCase()
}

kebabCase(a)
kebabCase(b)
kebabCase(c)
kebabCase(d)
kebabCase(e)
kebabCase(f)
ready
short circuit single letter inputs
function kebabCase(subject) {
    if (subject.length === 1) return subject.toLowerCase()
    return subject.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/[_\s]/, '-').toLowerCase()
}

kebabCase(a)
kebabCase(b)
kebabCase(c)
kebabCase(d)
kebabCase(e)
kebabCase(f)
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.