File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11export function camelCase ( str : string ) : string {
2- str = str . toLocaleLowerCase ( )
2+ // Handle the case where an argument is provided as camel case, e.g., fooBar.
3+ // by ensuring that the string isn't already mixed case:
4+ const isCamelCase = str !== str . toLowerCase ( ) && str !== str . toUpperCase ( )
5+
6+ if ( ! isCamelCase ) {
7+ str = str . toLocaleLowerCase ( )
8+ }
9+
310 if ( str . indexOf ( '-' ) === - 1 && str . indexOf ( '_' ) === - 1 ) {
411 return str
512 } else {
@@ -14,7 +21,6 @@ export function camelCase (str: string): string {
1421 }
1522 if ( i !== 0 && ( chr === '-' || chr === '_' ) ) {
1623 nextChrUpper = true
17- continue
1824 } else if ( chr !== '-' && chr !== '_' ) {
1925 camelcase += chr
2026 }
Original file line number Diff line number Diff line change @@ -11,6 +11,12 @@ describe('string-utils', function () {
1111 it ( 'removes leading hyphens' , ( ) => {
1212 strictEqual ( camelCase ( '-goodnight-moon' ) , 'goodnightMoon' )
1313 } )
14+ it ( 'camelCase string stays as is' , ( ) => {
15+ strictEqual ( camelCase ( 'iAmCamelCase' ) , 'iAmCamelCase' )
16+ } )
17+ it ( 'uppercase string with underscore to camel case' , ( ) => {
18+ strictEqual ( camelCase ( 'NODE_VERSION' ) , 'nodeVersion' )
19+ } )
1420 } )
1521 describe ( 'decamelize' , ( ) => {
1622 it ( 'adds hyphens back to camelcase string' , ( ) => {
You can’t perform that action at this time.
0 commit comments