-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkebabize.js
31 lines (26 loc) · 966 Bytes
/
kebabize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
Description:
Modify the kebabize function so that it converts a camel case string into a kebab case.
kebabize('camelsHaveThreeHumps') // camels-have-three-humps
kebabize('camelsHave3Humps') // camels-have-humps
Notes: - the returned string should only contain lowercase letters
*/function kebabize(str) {
return str
.replace(/\d/g, '')
.replace(/([A-Z])/g, '-$1')
.toLowerCase()
.replace(/^-/, '')
}
//return the passed `str` after: (1)replacing any digits with no space (.replace(/\d/g, ''))
//(2)placing a space in front of any capital letter;
//(3) converting everything to lower case
//(4) -- for some reason this was necessary to pass testing: check if the string starts
//with a '-' and if so remove it
//earlier solution:
function kebabize(str) {
var newCase = str.replace(/([A-Z])/g, '-$1').replace(/\d/g,'').toLowerCase();
while(newCase.charAt(0) === '-'){
newCase=newCase.substr(1)
}
return newCase;
}