Skip to content

Commit 0bffb1e

Browse files
committed
Fix toCamel issue
1 parent e56e403 commit 0bffb1e

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/utils/__tests__/to-camel.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ describe('toCamel', () => {
55
expect(toCamel('to-camel')).toBe('toCamel')
66
expect(toCamel('to/camel')).toBe('toCamel')
77
expect(toCamel('toCamel')).toBe('toCamel')
8+
expect(toCamel('to/toCamel')).toBe('toToCamel')
9+
expect(toCamel('to-came/toCamel/ToCamel')).toBe('toCameToCamelToCamel')
810
expect(toCamel('TO_CAMEL')).toBe('toCamel')
911
expect(toCamel('TO-CAMEL')).toBe('toCamel')
1012
expect(toCamel('ToCamel')).toBe('toCamel')

src/utils/to-camel.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
export function toCamel(str: string): string {
2-
if (/[-_ /]/g.test(str)) {
3-
return str
4-
.split(/[-_ /]/)
5-
.map((word, index) => {
6-
if (index === 0) return word.toLowerCase()
7-
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
8-
})
9-
.join('')
10-
}
112
return str
12-
.split(/([A-Z][a-z]+)/)
13-
.filter(Boolean)
3+
.split(/([A-Z][a-z]+|[-_ /])/)
4+
.filter((e) => {
5+
if (!e || e === '-' || e === '_' || e === '/' || e === ' ') return false
6+
return true
7+
})
148
.map((word, index) => {
159
if (index === 0) return word.toLowerCase()
1610
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()

0 commit comments

Comments
 (0)