diff --git a/snippets/camelize.md b/snippets/convert-string-to-camelcase.md similarity index 52% rename from snippets/camelize.md rename to snippets/convert-string-to-camelcase.md index 937a974e8..ba34a6835 100644 --- a/snippets/camelize.md +++ b/snippets/convert-string-to-camelcase.md @@ -1,15 +1,10 @@ -### Camelize +### Convert string to camelcase -Camelize a string, cutting the string by multiple separators like - hyphens, underscores and spaces. +Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase. ```js -/** - * @param {str} string str to camelize - * @return string Camelized text - */ -const camelize = str => str.replace(/^([A-Z])|[\s-_]+(\w)/g, - (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase()); +const toCamelCase = str => + str.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase()); // camelize("some_database_field_name") -> 'someDatabaseFieldName' // camelize("Some label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized' // camelize("some-javascript-property") -> 'someJavascriptProperty'