File tree 2 files changed +14
-2
lines changed
2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change 1
1
export 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
+
3
10
if ( str . indexOf ( '-' ) === - 1 && str . indexOf ( '_' ) === - 1 ) {
4
11
return str
5
12
} else {
@@ -14,7 +21,6 @@ export function camelCase (str: string): string {
14
21
}
15
22
if ( i !== 0 && ( chr === '-' || chr === '_' ) ) {
16
23
nextChrUpper = true
17
- continue
18
24
} else if ( chr !== '-' && chr !== '_' ) {
19
25
camelcase += chr
20
26
}
Original file line number Diff line number Diff line change @@ -11,6 +11,12 @@ describe('string-utils', function () {
11
11
it ( 'removes leading hyphens' , ( ) => {
12
12
strictEqual ( camelCase ( '-goodnight-moon' ) , 'goodnightMoon' )
13
13
} )
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
+ } )
14
20
} )
15
21
describe ( 'decamelize' , ( ) => {
16
22
it ( 'adds hyphens back to camelcase string' , ( ) => {
You can’t perform that action at this time.
0 commit comments