NaturalCycles/js-lib

View on GitHub
src/string/case.ts

Summary

Maintainability
A
2 hrs
Test Coverage
A
100%
import { words } from './lodash/words'
import { _upperFirst } from './string.util'

export function _camelCase(s: string): string {
  // return s.replace(/(_\w)/g, m => m[1]!.toUpperCase())
  return words(s.replaceAll(/['\u2019]/g, '')).reduce((result, word, index) => {
    word = word.toLowerCase()
    return result + (index ? _upperFirst(word) : word)
  }, '')
}

export function _snakeCase(s: string): string {
  return words(s.replaceAll(/['\u2019]/g, '')).reduce(
    (result, word, index) => result + (index ? '_' : '') + word.toLowerCase(),
    '',
  )
}

export function _kebabCase(s: string): string {
  return words(s.replaceAll(/['\u2019]/g, '')).reduce(
    (result, word, index) => result + (index ? '-' : '') + word.toLowerCase(),
    '',
  )
}