add camelize and decamelize.
This commit is contained in:
17
snippets/camelize.md
Normal file
17
snippets/camelize.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
### Camelize
|
||||||
|
|
||||||
|
Camelize a string, cutting the string by multiple separators like
|
||||||
|
hyphens, underscores and spaces.
|
||||||
|
|
||||||
|
```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());
|
||||||
|
// camelize("some_database_field_name") -> 'someDatabaseFieldName'
|
||||||
|
// camelize("Some label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized'
|
||||||
|
// camelize("some-javascript-property") -> 'someJavascriptProperty'
|
||||||
|
// camelize("some-mixed_string with spaces_underscores-and-hyphens") -> 'someMixedStringWithSpacesUnderscoresAndHyphens'
|
||||||
|
```
|
||||||
16
snippets/decamelize.md
Normal file
16
snippets/decamelize.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
### decamelize
|
||||||
|
|
||||||
|
Decamelizes a string with/without a custom separator (underscore by default).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const decamelize = (str, separator) => {
|
||||||
|
separator = typeof separator === 'undefined' ? '_' : separator;
|
||||||
|
return str
|
||||||
|
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
|
||||||
|
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
|
||||||
|
.toLowerCase();
|
||||||
|
}
|
||||||
|
// decamelize('someDatabaseFieldName', ' ') -> 'some database field name'
|
||||||
|
// decamelize('someLabelThatNeedsToBeCamelized', '-') -> 'some-label-that-needs-to-be-camelized'
|
||||||
|
// decamelize('someJavascriptProperty', '_') -> 'some_javascript_property'
|
||||||
|
```
|
||||||
@ -5,6 +5,7 @@ array-intersection:array
|
|||||||
array-union:array
|
array-union:array
|
||||||
average-of-array-of-numbers:array
|
average-of-array-of-numbers:array
|
||||||
bottom-visible:browser
|
bottom-visible:browser
|
||||||
|
camelize:string
|
||||||
capitalize-first-letter-of-every-word:string
|
capitalize-first-letter-of-every-word:string
|
||||||
capitalize-first-letter:string
|
capitalize-first-letter:string
|
||||||
chain-asynchronous-functions:function
|
chain-asynchronous-functions:function
|
||||||
@ -15,6 +16,7 @@ compact:array
|
|||||||
count-occurrences-of-a-value-in-array:array
|
count-occurrences-of-a-value-in-array:array
|
||||||
current-URL:browser
|
current-URL:browser
|
||||||
curry:function
|
curry:function
|
||||||
|
decamelize:string
|
||||||
deep-flatten-array:array
|
deep-flatten-array:array
|
||||||
distance-between-two-points:math
|
distance-between-two-points:math
|
||||||
divisible-by-number:math
|
divisible-by-number:math
|
||||||
|
|||||||
Reference in New Issue
Block a user