diff --git a/lib/string-utils.ts b/lib/string-utils.ts index 732e82a7..16e73223 100644 --- a/lib/string-utils.ts +++ b/lib/string-utils.ts @@ -1,5 +1,12 @@ export function camelCase (str: string): string { - str = str.toLocaleLowerCase() + // Handle the case where an argument is provided as camel case, e.g., fooBar. + // by ensuring that the string isn't already mixed case: + const isCamelCase = str !== str.toLowerCase() && str !== str.toUpperCase() + + if (!isCamelCase) { + str = str.toLocaleLowerCase() + } + if (str.indexOf('-') === -1 && str.indexOf('_') === -1) { return str } else { @@ -14,7 +21,6 @@ export function camelCase (str: string): string { } if (i !== 0 && (chr === '-' || chr === '_')) { nextChrUpper = true - continue } else if (chr !== '-' && chr !== '_') { camelcase += chr } diff --git a/test/string-utils.cjs b/test/string-utils.cjs index 1129777c..74d618d4 100644 --- a/test/string-utils.cjs +++ b/test/string-utils.cjs @@ -11,6 +11,12 @@ describe('string-utils', function () { it('removes leading hyphens', () => { strictEqual(camelCase('-goodnight-moon'), 'goodnightMoon') }) + it('camelCase string stays as is', () => { + strictEqual(camelCase('iAmCamelCase'), 'iAmCamelCase') + }) + it('uppercase string with underscore to camel case', () => { + strictEqual(camelCase('NODE_VERSION'), 'nodeVersion') + }) }) describe('decamelize', () => { it('adds hyphens back to camelcase string', () => {