Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a364e1f

Browse filesBrowse files
committed
[ADD] spinal case finished
1 parent 99c36c5 commit a364e1f
Copy full SHA for a364e1f

File tree

Expand file treeCollapse file tree

2 files changed

+37
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+37
-7
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,4 +745,23 @@ fearNotLetter("bcdf") should return the string e.
745745

746746
fearNotLetter("abcdefghijklmnopqrstuvwxyz") should return undefined.
747747

748+
```
749+
750+
751+
## Spinal Tap Case
752+
753+
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
754+
755+
756+
```javascript
757+
spinalCase("This Is Spinal Tap") should return the string this-is-spinal-tap.
758+
759+
spinalCase("thisIsSpinalTap") should return the string this-is-spinal-tap.
760+
761+
spinalCase("The_Andy_Griffith_Show") should return the string the-andy-griffith-show.
762+
763+
spinalCase("Teletubbies say Eh-oh") should return the string teletubbies-say-eh-oh.
764+
765+
spinalCase("AllThe-small Things") should return the string all-the-small-things.
766+
748767
```

‎src/spinalCase.js

Copy file name to clipboard
+18-7Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
function spinalCase(str) {
2-
const regex = new RegExp(/_|-|\s/,"g");
3-
if(regex.test(regex)){
4-
return str.split(regex).join("-").toLowerCase();
2+
let newStr = str;
3+
newStr = newStr.replace(/[\s|_]/g,"-");
4+
if(/[a-z]+[A-Z]/.test(newStr)){
5+
let copyStr = newStr[0];
6+
7+
for(let i = 1; i < newStr.length; ++i){
8+
if(newStr[i].toUpperCase() === newStr[i] && newStr[i] !== "-"){
9+
if(newStr[i -1] !== "-"){
10+
copyStr += "-";
11+
}
12+
}
13+
copyStr += newStr[i];
14+
15+
}
16+
return copyStr.toLowerCase();
517
}
6-
}
18+
return newStr.toLowerCase();
19+
}
720

8-
console.log(spinalCase('thisIsSpinalTap'));
9-
console.log(spinalCase('this IsSpinal Tap'));
10-
console.log(spinalCase('Teletubbies say Eh-oh"'));
21+
console.log(spinalCase('AllThe-small Things'));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.