File tree Expand file tree Collapse file tree 2 files changed +37
-7
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +37
-7
lines changed
Original file line number Diff line number Diff line change @@ -745,4 +745,23 @@ fearNotLetter("bcdf") should return the string e.
745
745
746
746
fearNotLetter (" abcdefghijklmnopqrstuvwxyz" ) should return undefined .
747
747
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
+
748
767
```
Original file line number Diff line number Diff line change 1
1
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 ( ) ;
5
17
}
6
- }
18
+ return newStr . toLowerCase ( ) ;
19
+ }
7
20
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' ) ) ;
You can’t perform that action at this time.
0 commit comments