@queso/camel-case
Version:
Converts a string value into camel case.
29 lines (28 loc) • 733 B
JavaScript
const allCapitalLetterGroups = /[A-ZÀ-ÖÙ-Ý]+/g
const allWords = /[A-ZÀ-ÖÙ-Ýa-zà-öù-ý]+/g
/**
* Converts a string value into [camel case](https://en.wikipedia.org/wiki/Camel_case).
* @param value The string to convert.
* @category String
* @returns The camel-cased string.
* @example
camelCase('foo-bar-baz')
// => 'fooBarBaz'
*/
export default function camelCase(value) {
const words = value
.replace(allCapitalLetterGroups, valleyCase)
.match(allWords)
return words
? words[0][0].toLowerCase() +
words
.map(x => x[0].toUpperCase() + x.slice(1))
.join('')
.slice(1)
: ''
function valleyCase(s) {
return s.length > 2
? s[0] + s.slice(1, -1).toLowerCase() + s.slice(-1)
: s
}
}