-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspinal-tap-case.js
39 lines (32 loc) · 1 KB
/
spinal-tap-case.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
32
33
34
35
36
37
38
39
// My Solution
// it has to lower case all words and put a tap case in the middle.
// there are test cases like: "AllThe moments", "All_The_Moments", "All the Moments", etc.
function spinalCase(str) {
let string = str
let arr = []
let id = string.search(/[a-z][A-Z]/)
while (id != -1){
arr = string.split('')
arr.splice(id+1,0," ")
string = arr.join("")
id = string.search(/[a-z][A-Z]/)
}
return string.toLowerCase().replaceAll(string.match(/_|\s/),"-");
}
// Other FCC Fancy Solutions
function spinalCase(str) {
// Create a variable for the white space and underscores.
var regex = /\s+|_+/g;
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
// Replace space and underscore with -
return str.replace(regex, "-").toLowerCase();
}
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str
.split(/\s|_|(?=[A-Z])/)
.join("-")
.toLowerCase();
}