Cleaned up the current snippets for consistency and minor problems, added multiple tags to most of them, archived a few.
21 lines
684 B
Markdown
21 lines
684 B
Markdown
### fromCamelCase
|
|
|
|
Converts a string from camelcase.
|
|
|
|
Use `String.replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
|
|
Omit the second argument to use a default `separator` of `_`.
|
|
|
|
```js
|
|
const fromCamelCase = (str, separator = '_') =>
|
|
str
|
|
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
|
|
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
|
|
.toLowerCase();
|
|
```
|
|
|
|
```js
|
|
fromCamelCase('someDatabaseFieldName', ' '); // 'some database field name'
|
|
fromCamelCase('someLabelThatNeedsToBeCamelized', '-'); // 'some-label-that-needs-to-be-camelized'
|
|
fromCamelCase('someJavascriptProperty', '_'); // 'some_javascript_property'
|
|
```
|