Prepare repository for merge
This commit is contained in:
28
javascript/snippets/from-camel-case.md
Normal file
28
javascript/snippets/from-camel-case.md
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
title: String from camelcase
|
||||
type: snippet
|
||||
tags: [string]
|
||||
cover: mountain-lake-cottage
|
||||
dateModified: 2020-10-22T20:23:47+03:00
|
||||
---
|
||||
|
||||
Converts a string from camelcase.
|
||||
|
||||
- Use `String.prototype.replace()` to break the string into words and add a `separator` between them.
|
||||
- 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('someLabelThatNeedsToBeDecamelized', '-');
|
||||
// 'some-label-that-needs-to-be-decamelized'
|
||||
fromCamelCase('someJavascriptProperty', '_'); // 'some_javascript_property'
|
||||
fromCamelCase('JSONToCSV', '.'); // 'json.to.csv'
|
||||
```
|
||||
Reference in New Issue
Block a user