Merge pull request #190 from gnipbao/dev
add camelize and decamelize function
This commit is contained in:
13
snippets/convert-string-from-camelcase.md
Normal file
13
snippets/convert-string-from-camelcase.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
### Convert string from camelcase
|
||||||
|
|
||||||
|
Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase.
|
||||||
|
Omit the scond 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();
|
||||||
|
// decamelize('someDatabaseFieldName', ' ') -> 'some database field name'
|
||||||
|
// decamelize('someLabelThatNeedsToBeCamelized', '-') -> 'some-label-that-needs-to-be-camelized'
|
||||||
|
// decamelize('someJavascriptProperty', '_') -> 'some_javascript_property'
|
||||||
|
```
|
||||||
12
snippets/convert-string-to-camelcase.md
Normal file
12
snippets/convert-string-to-camelcase.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
### Convert string to camelcase
|
||||||
|
|
||||||
|
Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase.
|
||||||
|
|
||||||
|
```js
|
||||||
|
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'
|
||||||
|
// camelize("some-mixed_string with spaces_underscores-and-hyphens") -> 'someMixedStringWithSpacesUnderscoresAndHyphens'
|
||||||
|
```
|
||||||
@ -10,6 +10,7 @@ array-without:array
|
|||||||
array-zip:array
|
array-zip:array
|
||||||
average-of-array-of-numbers:array
|
average-of-array-of-numbers:array
|
||||||
bottom-visible:browser
|
bottom-visible:browser
|
||||||
|
convert-string-to-camelcase: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
|
||||||
@ -22,6 +23,7 @@ compose-functions:function
|
|||||||
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
|
||||||
|
convert-string-from-camelcase: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