@queso/camel-case
Version:
Converts a string value into camel case.
34 lines (31 loc) • 800 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'
*/
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
}
}
module.exports = camelCase;
camelCase.default = module.exports