Skip to content

Commit 5f4da1f

Browse files
fix: do not lowercase camel cased string (#348)
1 parent 637690d commit 5f4da1f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/string-utils.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
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+
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
}

test/string-utils.cjs

+6
Original file line numberDiff line numberDiff 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', () => {

0 commit comments

Comments
 (0)