mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 14:37:49 +00:00
31 lines
769 B
JavaScript
31 lines
769 B
JavaScript
import upperFirst from './upperFirst.js'
|
|
import words from './words.js'
|
|
|
|
/**
|
|
* Converts `string` to [camel case](https://en.wikipedia.org/wiki/CamelCase).
|
|
*
|
|
* @since 3.0.0
|
|
* @category String
|
|
* @param {string} [string=''] The string to convert.
|
|
* @returns {string} Returns the camel cased string.
|
|
* @see lowerCase, kebabCase, snakeCase, startCase, upperCase, upperFirst
|
|
* @example
|
|
*
|
|
* camelCase('Foo Bar')
|
|
* // => 'fooBar'
|
|
*
|
|
* camelCase('--foo-bar--')
|
|
* // => 'fooBar'
|
|
*
|
|
* camelCase('__FOO_BAR__')
|
|
* // => 'fooBar'
|
|
*/
|
|
const camelCase = (string) => (
|
|
words(`${string}`.replace(/['\u2019]/g, '')).reduce((result, word, index) => {
|
|
word = word.toLowerCase()
|
|
return result + (index ? upperFirst(word) : word)
|
|
}, '')
|
|
)
|
|
|
|
export default camelCase
|