JSFiddle - React, Tailwind, and code Playground

JavaScript

function spinalCase(str) {
var str0=' ';
var str1;
var arr0=[];
var x = str.indexOf(' ');
var y = str.indexOf('_');
arr0=str.split('');
arr0.forEach(
function(item, index){
if (item==item.toUpperCase()&&item!==' '&&index!==0&&item!=='_'&&item!=='-'&&arr0[index-1]!==' '&&arr0[index-1]!=='_'){arr0.splice(index, 1, '-'+item);}});
arr0.forEach(
function(item, index){
if (item===' '||item==='_'){
arr0.splice(index, 1, '-');
}
}
);


console.log(
arr0.join('').toLowerCase()
);
}

spinalCase("allTheSmallThings");