Skip to content

Commit

Permalink
fix: do not lowercase camel cased string (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan-Strahovsky committed Feb 13, 2021
1 parent 637690d commit 5f4da1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions 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 {
Expand All @@ -14,7 +21,6 @@ export function camelCase (str: string): string {
}
if (i !== 0 && (chr === '-' || chr === '_')) {
nextChrUpper = true
continue
} else if (chr !== '-' && chr !== '_') {
camelcase += chr
}
Expand Down
6 changes: 6 additions & 0 deletions test/string-utils.cjs
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 5f4da1f

Please sign in to comment.