Skip to content

Commit aadf8ce

Browse files
refactored without regex
1 parent a39512d commit aadf8ce

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

6kyus/js/convertStringToCamelCase.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,35 @@
55

66
function toCamelCase(str) {
77
// Determine if string includes - or underscore
8-
if (str.includes('-')) {
9-
return str
10-
.split('-')
11-
.map((word, idx) => {
12-
if (idx > 0) {
13-
return `${word.charAt(0).toUpperCase()}${word.slice(1)}`
14-
}
15-
return word
16-
})
17-
.join('')
18-
} else if (str.includes('_')) {
19-
return str
20-
.split('_')
21-
.map((word, idx) => {
22-
if (idx > 0) {
23-
return `${word.charAt(0).toUpperCase()}${word.slice(1)}`
24-
}
25-
return word
26-
})
27-
.join('')
8+
// if (str.includes('-')) {
9+
// return str
10+
// .split('-')
11+
// .map((word, idx) => {
12+
// if (idx > 0) {
13+
// return `${word.charAt(0).toUpperCase()}${word.slice(1)}`
14+
// }
15+
// return word
16+
// })
17+
// .join('')
18+
// } else if (str.includes('_')) {
19+
// return str
20+
// .split('_')
21+
// .map((word, idx) => {
22+
// if (idx > 0) {
23+
// return `${word.charAt(0).toUpperCase()}${word.slice(1)}`
24+
// }
25+
// return word
26+
// })
27+
// .join('')
28+
// }
29+
30+
const words = str.split('-').join('_').split('_')
31+
32+
for (let i = 1; i < words.length; i++) {
33+
words[i] = words[i][0].toUpperCase() + words[i].slice(1)
2834
}
35+
36+
return words.join('')
2937
}
3038

3139
// Split on dash or underscore

0 commit comments

Comments
 (0)