repl.it
@QuemuelVital/

String.toSpinalCase()

Nodejs

Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  let re = /[\s\_\-]/g;
  let newstr;
  newstr = str.replace(/([a-z])([A-Z])/g, '$1 $2').replace(re, '-').toLowerCase();

  return newstr;
}

spinalCase('This_Is_Spinal_Tap');

/* Note for the future: Improve this code, diminishing the number of lines and the length of the main line. 
    Tip: Place all the regex's in the first replace(), it will kill the first line and the long phrase. Also, make the return directly on the str parameter;) 
*/