panzerdp / voca

The ultimate JavaScript string library
https://vocajs.pages.dev
MIT License
3.6k stars 137 forks source link

Reverse camelcase? #58

Open jsodeman opened 3 years ago

jsodeman commented 3 years ago

I like Voca better than the alternatives, but I can't see how you would convert camelcase into words. In other libs it's called .humanize

For example:

"camelCase" -> "camel case"

darthf1 commented 2 years ago

Looking for the same. Found a solution on https://zellwk.com/blog/case-conversion/, first convert the string to kebabCase via voca, then applying the presented solution.

import { kebabCase } from 'voca'

const humanize = (string: string) => {
  const interim = kebabCase(string).replace(/-/g, ' ')

  return interim.slice(0, 1).toUpperCase() + interim.slice(1)
}